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


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

find out the maximum lifetime of any particles in this effect

wait for random duration

turn off emission

wait for any remaining particles to expire

stops the particle system early



Gõ tìm kiếm nhanh...