|
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);
|
}
|
}
|
}
|
}
|