ParticleSystemDestroyer.cs
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Utility
{
public class ParticleSystemDestroyer : MonoBehaviour
{
// allows a particle system to exist for a specified duration,
// then shuts off emission, and waits for all particles to expire
// before destroying the gameObject
public float minDuration = 8;
public float maxDuration = 10;
private float m_MaxLifetime;
private bool m_EarlyStop;
private IEnumerator Start()
{
var systems = GetComponentsInChildren<ParticleSystem>();
// find out the maximum lifetime of any particles in this effect
foreach (var system in systems)
{
m_MaxLifetime = Mathf.Max(system.main.startLifetime.constant, m_MaxLifetime);
}
// wait for random duration
float stopTime = Time.time + Random.Range(minDuration, maxDuration);
while (Time.time < stopTime && !m_EarlyStop)
{
yield return null;
}
Debug.Log("stopping " + name);
// turn off emission
foreach (var system in systems)
{
var emission = system.emission;
emission.enabled = false;
}
BroadcastMessage("Extinguish", SendMessageOptions.DontRequireReceiver);
// wait for any remaining particles to expire
yield return new WaitForSeconds(m_MaxLifetime);
Destroy(gameObject);
}
public void Stop()
{
// stops the particle system early
m_EarlyStop = true;
}
}
}