Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // Controls intensity and direction of rain.
  6. public class RainBehavior : MonoBehaviour {
  7.  
  8.     public float debugIntensity = -1;
  9.     public float debugAngle = -9999;
  10.  
  11.     public float curIntensity { get; private set; } = 0;
  12.     public float curAngle { get; private set; } = 0;
  13.  
  14.     // Adjust position in front of the camera.
  15.     public float zDist = 40;
  16.     public float yDist = 20;
  17.  
  18.     public ParticleSystem rain;
  19.     private Dictionary<ParticleSystem, float> psToDefaultEmissionRate = new Dictionary<ParticleSystem, float>();
  20.  
  21.     void Awake() {
  22.         RecursiveApply(rain, (ParticleSystem ps) => {
  23.             psToDefaultEmissionRate[ps] = ps.emission.rateOverTime.constant;
  24.         });
  25.     }
  26.  
  27.     // Set the intensity of the rain.
  28.     // 0 = off, 1 = downpour
  29.     public void SetIntensity(float i) {
  30.         curIntensity = i;
  31.         RecursiveApply(rain, (ParticleSystem ps) => {
  32.             var emission = ps.emission;
  33.             if (!psToDefaultEmissionRate.ContainsKey(ps)) {
  34.                 Debug.LogWarning($"Unable to find default emission rate for {ps}; have {string.Join(",", psToDefaultEmissionRate.Keys)}");
  35.             }
  36.             emission.rateOverTime = psToDefaultEmissionRate[ps] * i;
  37.         });
  38.     }
  39.  
  40.     // Recursively apply f to all particle systems under `ps`
  41.     private void RecursiveApply(ParticleSystem ps, Action<ParticleSystem> f) {
  42.         f(ps);
  43.         foreach (Transform t in ps.transform) {
  44.             var child = t.gameObject.GetComponent<ParticleSystem>();
  45.             if (child != null) {
  46.                 RecursiveApply(child, f);
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement