yyl
昨天 5d3366f2e0f687995eb7ad2107c4379fe7acd4e8
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
 
using UnityEngine;
using UnityEngine.Events;
 
public class SFXController : MonoBehaviour
{
    public struct ParticleProperties
    {
        public float simulationSpeed;
    }
 
    public float duration = 2;
    [System.NonSerialized]
    public UnityAction<SFXController> m_OnFinished;
 
    [HideInInspector]
    public int effectId;
 
    [HideInInspector]
    public float startTime;
 
    public int DelayFrameCount
    {
        get; set;
    }
 
    [HideInInspector]
    public EffectConfig config;
 
    public bool releaseHideChildren = true;
 
    private float m_AnimatorSpeed = 1;
    private ParticleSystem[] m_CacheParticleSystem;
    private ParticleProperties[] m_ParticleProperties;
    private TrailRenderer[] m_CacheTrailRenderer;
    private Animator[] m_CacheAnimator;
    private Renderer[] m_Renderers;
 
    private bool m_Active;
 
    private void Awake()
    {
        if (m_CacheParticleSystem == null)
        {
            m_CacheParticleSystem = gameObject.GetComponentsInChildren<ParticleSystem>(true);
            if (m_CacheParticleSystem != null)
            {
                m_ParticleProperties = new ParticleProperties[m_CacheParticleSystem.Length];
                for (int i = 0; i < m_CacheParticleSystem.Length; ++i)
                {
                    m_ParticleProperties[i] = new ParticleProperties()
                    {
                        simulationSpeed = m_CacheParticleSystem[i].main.simulationSpeed
                    };
                }
            }
        }
 
        if (m_CacheTrailRenderer == null)
        {
            m_CacheTrailRenderer = gameObject.GetComponentsInChildren<TrailRenderer>(true);
        }
 
        if (m_CacheAnimator == null)
        {
            m_CacheAnimator = gameObject.GetComponentsInChildren<Animator>(true);
        }
 
        if (m_Renderers == null)
        {
            m_Renderers = gameObject.GetComponentsInChildren<Renderer>(true);
        }
    }
 
    void Update()
    {
        if (m_AnimatorSpeed == 0)
        {
            return;
        }
 
        startTime += Time.deltaTime;
 
        if (duration > 0
         && startTime >= duration)
        {
            m_AnimatorSpeed = 1;
            SFXPlayUtility.Instance.Release(this);
 
            if (m_OnFinished != null)
            {
                m_OnFinished(this);
                m_OnFinished = null;
            }
        }
    }
 
    public void SetActive(bool active)
    {
        m_Active = active;
 
        if (m_CacheParticleSystem != null && m_CacheParticleSystem.Length > 0)
        {
            for (int i = 0; i < m_CacheParticleSystem.Length; ++i)
            {
                if (active)
                {
                    m_CacheParticleSystem[i].Play();
                }
                else
                {
                    m_CacheParticleSystem[i].Stop();
                }
            }
        }
 
        if (m_CacheAnimator != null && m_CacheAnimator.Length > 0)
        {
            for (int i = 0; i < m_CacheAnimator.Length; ++i)
            {
                if (active)
                {
                    m_CacheAnimator[i].enabled = true;
                    if (m_CacheAnimator[i].runtimeAnimatorController)
                    {
                        m_CacheAnimator[i].Play(m_CacheAnimator[i].GetCurrentAnimatorStateInfo(0).shortNameHash, 0, 0);
                        m_CacheAnimator[i].Update(0);
                    }
                }
                else
                {
                    m_CacheAnimator[i].enabled = false;
                }
            }
 
            //if (!active && releaseHideChildren)
            //{
            //    if (m_Renderers != null && m_Renderers.Length > 0)
            //    {
            //        for (int i = 0; i < m_Renderers.Length; ++i)
            //        {
            //            if (m_Renderers[i].gameObject.activeSelf)
            //            {
            //                m_Renderers[i].SetActive(active);
            //            }
            //        }
            //    }
 
            //    if (m_CacheParticleSystem != null && m_CacheParticleSystem.Length > 0)
            //    {
            //        for (int i = 0; i < m_CacheParticleSystem.Length; ++i)
            //        {
            //            if (m_CacheParticleSystem[i].gameObject.activeSelf)
            //            {
            //                m_CacheParticleSystem[i].SetActive(active);
            //            }
            //        }
            //    }
            //}
        }
    }
 
    public void SetAnimatorSpeed(float speed)
    {
        m_AnimatorSpeed = speed;
        if (m_CacheAnimator != null)
        {
            for (int i = 0; i < m_CacheAnimator.Length; ++i)
            {
                var animator = m_CacheAnimator[i];
                if (animator != null)
                {
                    m_CacheAnimator[i].speed = m_AnimatorSpeed;
                }
            }
        }
    }
 
    public void SetPaticleSystemSpeed(float speed)
    {
        if (m_CacheParticleSystem != null)
        {
            ParticleSystem.MainModule _mainModule;
            for (int i = 0; i < m_CacheParticleSystem.Length; ++i)
            {
                if (m_CacheParticleSystem[i])
                {
                    _mainModule = m_CacheParticleSystem[i].main;
                    _mainModule.simulationSpeed = m_ParticleProperties[i].simulationSpeed * speed;
                }
            }
        }
    }
 
    public void SetParticleEmissionRateOverTime(float value)
    {
        if (m_CacheParticleSystem != null)
        {
            ParticleSystem.EmissionModule _emissioinModule;
            for (int i = 0; i < m_CacheParticleSystem.Length; ++i)
            {
                if (m_CacheParticleSystem[i])
                {
                    _emissioinModule = m_CacheParticleSystem[i].emission;
                    _emissioinModule.rateOverTime = value;
                }
            }
        }
    }
 
    private void SetChildActive(Transform transforms, bool active)
    {
        for (int i = 0; i < transforms.childCount; ++i)
        {
            transforms.GetChild(i).SetActive(active);
 
            if (transforms.GetChild(i).childCount > 0)
            {
                SetChildActive(transforms.GetChild(i), active);
            }
        }
    }
}