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                 
var emission = system.emission;
45                 emission.enabled =
false;
46             }
47             BroadcastMessage(
"Extinguish", SendMessageOptions.DontRequireReceiver);
48
49             
// wait for any remaining particles to expire
50             
yield return new WaitForSeconds(m_MaxLifetime);
51
52             Destroy(gameObject);
53         }
54
55
56         
public void Stop()
57         {
58             
// stops the particle system early
59             m_EarlyStop =
true;
60         }
61     }
62 }


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