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);
}
}
}
}
这是一个Unity编辑器扩展脚本,用于控制粒子系统的播放、暂停、继续和停止。通过`PlayAllParticleSystem`类,可以批量操作粒子系统组件,实现时间同步的模拟播放效果。该脚本使用了`EditorApplication.update`委托来更新粒子状态,并根据枚举类型`PlayState`切换不同操作。

5730

被折叠的 条评论
为什么被折叠?



