Unity 編輯器下控制播放粒子
阿新 • • 發佈:2018-12-21
using UnityEditor;using UnityEngine;[CustomEditor(typeof(EditParticleSystem))]public class EditParticleSystemInspector : Editor{ /// <summary> /// 滑動杆的當前時間 /// </summary> private float m_CurTime; /// <summary> /// 當前是否是預覽播放狀態 /// </summary> private bool m_Playing; /// <summary> /// 當前執行時間 /// </summary> private float m_RunningTime; /// <summary> /// 上一次系統時間 /// </summary> private double m_PreviousTime; /// <summary> /// 滑動杆總長度 /// </summary> private const float kDuration = 30f; private ParticleSystem m_ParticleSystem; private EditParticleSystem editAnimator { get { return target as EditParticleSystem; } } private ParticleSystem particleSystem { get { return m_ParticleSystem ?? (m_ParticleSystem = editAnimator.GetComponentInChildren<ParticleSystem>()); } } void OnEnable() { m_PreviousTime = EditorApplication.timeSinceStartup; EditorApplication.update += inspectorUpdate; } void OnDisable() { EditorApplication.update -= inspectorUpdate; } public override void OnInspectorGUI() { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Play")) { play(); } if (GUILayout.Button("Stop")) { stop(); } EditorGUILayout.EndHorizontal(); m_CurTime = EditorGUILayout.Slider("Time:", m_CurTime, 0f, kDuration); manualUpdate(); } /// <summary> /// 進行預覽播放 /// </summary> private void play() { if (Application.isPlaying || particleSystem == null) { return; } m_RunningTime = 0f; m_Playing = true; } /// <summary> /// 停止預覽播放 /// </summary> private void stop() { if (Application.isPlaying || particleSystem == null) { return; } m_Playing = false; m_CurTime = 0f; } /// <summary> /// 預覽播放狀態下的更新 /// </summary> private void update() { if (Application.isPlaying || particleSystem == null) { return; } if (m_RunningTime >= kDuration) { m_Playing = false; return; } particleSystem.Simulate(m_RunningTime, true); SceneView.RepaintAll(); Repaint(); m_CurTime = m_RunningTime; } /// <summary> /// 非預覽播放狀態下,通過滑桿來播放當前動畫幀 /// </summary> private void manualUpdate() { if (particleSystem && !m_Playing) { particleSystem.Simulate(m_CurTime, true); SceneView.RepaintAll(); } } private void inspectorUpdate() { var delta = EditorApplication.timeSinceStartup - m_PreviousTime; m_PreviousTime = EditorApplication.timeSinceStartup; if (!Application.isPlaying && m_Playing) { m_RunningTime = Mathf.Clamp(m_RunningTime + (float)delta, 0f, kDuration); update(); } }}