-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAutoDestructParticle.cs
64 lines (52 loc) · 1.8 KB
/
AutoDestructParticle.cs
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
63
64
using UnityEngine;
using State.Event;
using System.Collections;
[RequireComponent(typeof(ParticleSystem))]
public class AutoDestructParticle : EventManager {
public bool OnlyDeactivate;
public bool addToPool;
[EnableCondition("addToPool", "true")]
public string poolType;
[Tooltip("Default to particle duration when set false")]
public bool onTime;
[EnableCondition("onTime", "true")]
public float duration;
protected ObjectPool pool;
public event EventDelegator OnDeactive;
protected float counter;
private void Start() {
pool = ObjectPool.instance;
}
public override void OnEnable() {
base.OnEnable();
StartCoroutine("CheckIfAlive");
}
public override void OnDisable() {
base.OnDisable();
if (this.gameObject.activeInHierarchy) {
this.gameObject.SetActive(false);
}
}
IEnumerator CheckIfAlive() {
ParticleSystem ps = this.GetComponent<ParticleSystem>();
while (true && ps != null) {
yield return new WaitForSeconds(0.5f);
counter += 0.5f;
if (!ps.IsAlive(true) || (onTime && counter >= duration)) {
if (OnlyDeactivate) {
this.gameObject.SetActive(false);
if (OnDeactive != null) {
OnDeactive();
}
} else if (addToPool) {
this.gameObject.transform.parent = null;
pool.PutObject(poolType, this.gameObject.name, ps);
ps.gameObject.SetActive(false);
} else {
GameObject.Destroy(this.gameObject);
}
break;
}
}
}
}