using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
//请继承Editor
public class PlayAllParticleSystem : Editor
{
public List<GameObject> childs=new List<GameObject>();
private double m_PreviousTime;
private double nowTime;
bool IsPlay;
float time ;
float CTime;
float PauseTime;
float NextTime;
public enum PlayState
{
None,
Play,
Pause,
Continue,
Stop,
}
public PlayState _PlayState=PlayState.None;
//播放粒子特效
public void PlayParticleSystems()
{
_PlayState = PlayState.Play;
EditorApplication.update += Play;
nowTime=EditorApplication.timeSinceStartup;
}
//暂停粒子特效
public void PauseParticleSystems()
{
if(_PlayState == PlayState.Continue)
{
PauseTime=NextTime;
}
else if(_PlayState == PlayState.Play)
{
PauseTime = time;
}
_PlayState = PlayState.Pause;
nowTime=EditorApplication.timeSinceStartup;
Pause ();
}
public void ContinueParticleSystems()
{
_PlayState = PlayState.Continue;
m_PreviousTime = EditorApplication.timeSinceStartup;
CTime=(float)m_PreviousTime-PauseTime;
EditorApplication.update +=Contiue;
PauseTime =NextTime ;
}
//停止粒子特效
public void StopParticleSystems()
{
_PlayState = PlayState.Stop;
Stop ();
}
private void Play()
{
if(_PlayState==PlayState.Play)
{
m_PreviousTime = EditorApplication.timeSinceStartup;
time = (float)(m_PreviousTime - nowTime);
for(int i=0;i<childs.Count;i++)
{
if(childs[i]!=null)
childs[i].GetComponent<ParticleSystem>().Simulate(time);
}
}
}
private void Pause()
{
if(_PlayState==PlayState.Pause)
{
for(int i=0;i<childs.Count;i++)
{
if(childs[i]!=null)
childs[i].GetComponent<ParticleSystem>().Pause(true);
}
}
}
private void Contiue()
{
if(_PlayState==PlayState.Continue)
{
m_PreviousTime = EditorApplication.timeSinceStartup;
NextTime=(float)m_PreviousTime-CTime;
for(int i=0;i<childs.Count;i++)
{
if(childs[i]!=null)
childs[i].GetComponent<ParticleSystem>().Simulate(NextTime);
}
}
}
private void Stop()
{
if(_PlayState==PlayState.Stop)
{
for(int i=0;i<childs.Count;i++)
{
childs[i].GetComponent<ParticleSystem>().Simulate(0);
}
}
}
}