Files
Fishing2/Assets/UnityStandardAssets/Utility/ParticleSystemDestroyer.cs
2025-05-10 12:49:47 +08:00

47 lines
1.1 KiB
C#

using System.Collections;
using UnityEngine;
namespace UnityStandardAssets.Utility
{
public class ParticleSystemDestroyer : MonoBehaviour
{
public float minDuration = 8f;
public float maxDuration = 10f;
private float m_MaxLifetime;
private bool m_EarlyStop;
private IEnumerator Start()
{
ParticleSystem[] systems = GetComponentsInChildren<ParticleSystem>();
ParticleSystem[] array = systems;
foreach (ParticleSystem particleSystem in array)
{
m_MaxLifetime = Mathf.Max(particleSystem.main.startLifetime.constant, m_MaxLifetime);
}
float stopTime = Time.time + Random.Range(minDuration, maxDuration);
while (Time.time < stopTime && !m_EarlyStop)
{
yield return null;
}
Debug.Log("stopping " + name);
array = systems;
for (int i = 0; i < array.Length; i++)
{
ParticleSystem.EmissionModule emission = array[i].emission;
emission.enabled = false;
}
BroadcastMessage("Extinguish", SendMessageOptions.DontRequireReceiver);
yield return new WaitForSeconds(m_MaxLifetime);
Destroy(gameObject);
}
public void Stop()
{
m_EarlyStop = true;
}
}
}