Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- // Controls intensity and direction of rain.
- public class RainBehavior : MonoBehaviour {
- public float debugIntensity = -1;
- public float debugAngle = -9999;
- public float curIntensity { get; private set; } = 0;
- public float curAngle { get; private set; } = 0;
- // Adjust position in front of the camera.
- public float zDist = 40;
- public float yDist = 20;
- public ParticleSystem rain;
- private Dictionary<ParticleSystem, float> psToDefaultEmissionRate = new Dictionary<ParticleSystem, float>();
- void Awake() {
- RecursiveApply(rain, (ParticleSystem ps) => {
- psToDefaultEmissionRate[ps] = ps.emission.rateOverTime.constant;
- });
- }
- // Set the intensity of the rain.
- // 0 = off, 1 = downpour
- public void SetIntensity(float i) {
- curIntensity = i;
- RecursiveApply(rain, (ParticleSystem ps) => {
- var emission = ps.emission;
- if (!psToDefaultEmissionRate.ContainsKey(ps)) {
- Debug.LogWarning($"Unable to find default emission rate for {ps}; have {string.Join(",", psToDefaultEmissionRate.Keys)}");
- }
- emission.rateOverTime = psToDefaultEmissionRate[ps] * i;
- });
- }
- // Recursively apply f to all particle systems under `ps`
- private void RecursiveApply(ParticleSystem ps, Action<ParticleSystem> f) {
- f(ps);
- foreach (Transform t in ps.transform) {
- var child = t.gameObject.GetComponent<ParticleSystem>();
- if (child != null) {
- RecursiveApply(child, f);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement