Unity 粒子ParticleSystem正确的关闭方式

来源:互联网 发布:全面战争优化9圣物 编辑:程序博客网 时间:2024/06/08 11:15

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;        }    }
阅读全文
0 0