Unity 粒子ParticleSystem正确的关闭方式
using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;
public class ParticleSystemDestroyer : MonoBehaviour
{
//允许一个粒子系统在指定的时间存在,
//然后关闭发射,并等待所有粒子到期
//破坏前的游戏对象
public float minDuration = 8;
public float maxDuration = 10;
private float m_MaxLifetime;
private bool m_EarlyStop;
private IEnumerator Start()
{
var systems = GetComponentsInChildren<ParticleSystem>();
// find out the maximum lifetime of any particles in this effect
foreach (var system in systems)
{
m_MaxLifetime = Mathf.Max(system.main.startLifetime.constant, m_MaxLifetime);
}
// wait for random duration
float stopTime = Time.time + Random.Range(minDuration, maxDuration);
while (Time.time < stopTime || m_EarlyStop)
{
yield return null;
}
Debug.Log("stopping " + name);
// turn off emission
foreach (var system in systems)
{
var emission = system.emission;
emission.enabled = false;
}
BroadcastMessage("Extinguish", SendMessageOptions.DontRequireReceiver);
// wait for any remaining particles to expire
yield return new WaitForSeconds(m_MaxLifetime);
Destroy(gameObject);
}
public void Stop()
{
// stops the particle system early
m_EarlyStop = true;
}
}
我是李本心明
首先谢谢大家的支持,其次如果你碰到什么其他问题的话,欢迎来 我自己的一个 讨论群559666429
来(扫扫下面二维码或者点击群链接 Unity3D[ 交流] ),大家一起找答案,共同进步。
由于工作生活太忙了,对于大家的帮助时间已经没有之前那么充裕了。如果有志同道合的朋友,可以接受无偿的帮助别人,可以加我QQ单独联系我,一块经营一下。