River Jiang
2020-10-27 79e652bedf62f0842f59a2323f139f2e90efe819
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using Core.Utilities;
using UnityEngine;
 
namespace TowerDefense.Effects
{
    /// <summary>
    /// Simple effect support script to reset trails and particles on enable, and also
    /// stops and starts reused emitters (to prevent them emitting when moving after being repooled)
    /// </summary>
    public class PoolableEffect : Poolable
    {
        protected List<ParticleSystem> m_Systems;
        protected List<TrailRenderer> m_Trails;
 
        bool m_EffectsEnabled;
 
        // TEST CODE TO DEBUG:
        protected static int ID_START = 0;
        protected int mGuid = 0;
        
        /// <summary>
        /// Stop emitting all particles
        /// </summary>
        public void StopAll()
        {
            foreach (var particleSystem in m_Systems)
            {
                particleSystem.Stop();
            }
        }
        
        /// <summary>
        /// Turn off all known systems
        /// </summary>
        public void TurnOffAllSystems()
        {
            if (!m_EffectsEnabled)
            {
                return;
            }
            
            // Reset all systems and trails
            foreach (var particleSystem in m_Systems)
            {
                particleSystem.Clear();
                var emission = particleSystem.emission;
                emission.enabled = false;
            }
 
            foreach (var trailRenderer in m_Trails)
            {
                int tid = mGuid;
                try
                {
                    trailRenderer.Clear();
                    trailRenderer.enabled = false;
                }catch( SystemException e)
                {
                    Debug.Log("SystemExcept:" + e.ToString());
                }
 
            }
 
            m_EffectsEnabled = false;
        }
 
        /// <summary>
        /// Turn on all known systems
        /// </summary>
        public void TurnOnAllSystems()
        {
            if (m_EffectsEnabled)
            {
                return;
            }
            
            // Re-enable all systems and trails
            foreach (var particleSystem in m_Systems)
            {
                particleSystem.Clear();
                var emission = particleSystem.emission;
                emission.enabled = true;
            }
 
            foreach (var trailRenderer in m_Trails)
            {
                trailRenderer.Clear();
                trailRenderer.enabled = true;
            }
 
            m_EffectsEnabled = true;
        }
 
        protected override void Repool()
        {
            base.Repool();
            TurnOffAllSystems();
        }
 
        protected virtual void Awake()
        {
            m_EffectsEnabled = true;
 
            mGuid = ID_START++;
 
            // Cache systems and trails, but only active and emitting ones
            m_Systems = new List<ParticleSystem>();
            m_Trails = new List<TrailRenderer>();
 
            foreach (var system in GetComponentsInChildren<ParticleSystem>())
            {
                if (system.emission.enabled && system.gameObject.activeSelf)
                {
                    m_Systems.Add(system);
                }
            }
            
            foreach (var trail in GetComponentsInChildren<TrailRenderer>())
            {
                if (trail.enabled && trail.gameObject.activeSelf)
                {
                    m_Trails.Add(trail);
                }
            }
 
            TurnOffAllSystems();
        }
    }
}