using UnityEngine; using UnityEditor; using System.Collections; [CustomEditor(typeof(ParticleAndAnimation))] public class ParticleAndAnimation : Editor { private static GameObject SelectObj; #if UNITY_EDITOR void Start () { PlayOnce(); } #endif [MenuItem("Tools/Play Loop %l")] //[ContextMenu("Play Loop")] public static void PlayLoop() { Object[] objs = Selection.objects; if(objs.Length > 0) { ParticleAndAnimation.SelectObj = objs[0] as GameObject; } //PlaySelf(gameObject, true); Transform[] trans = SelectObj.GetComponentsInChildren(true); foreach(Transform t in trans) { DelayPlay d = t.GetComponent(); if (d != null) { if (d.delayTime >= 0) { d.InvokePlay(true, false); } } else { PlaySelf(t.gameObject, true); } } } //[ContextMenu("Play Once")] [MenuItem("Tools/Play Once %;")] public static void PlayOnce () { //PlaySelf(gameObject, false); Object[] objs = Selection.objects; if (objs.Length > 0) { ParticleAndAnimation.SelectObj = objs[0] as GameObject; } Transform[] trans = SelectObj.GetComponentsInChildren(true); foreach(Transform t in trans) { DelayPlay d = t.GetComponent(); if (d != null) { if (d.delayTime >= 0) { d.InvokePlay(false, false); } } else { PlaySelf(t.gameObject, false); } } } void PlaySelfAndAllChildren(GameObject obj, bool loop) { if (obj == null) return; //EnableChildrenAll(obj, true); ParticleSystem[] pss = obj.GetComponentsInChildren(true); foreach(ParticleSystem ps in pss) { ps.loop = loop; ps.Clear(true); ps.Play(); } Animation[] anis = obj.GetComponentsInChildren(true); foreach(Animation an in anis) { an.wrapMode = loop? WrapMode.Loop : WrapMode.Once; an.Play(); } Animator[] amts = obj.GetComponentsInChildren(); foreach(Animator amt in amts) { if (null != amt) { #if UNITY_5 AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0); foreach (AnimatorClipInfo info in infs) { info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once; amt.Play(info.clip.name, -1, 0); break; } #else AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0); foreach (AnimatorClipInfo info in infs) { info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once; amt.Play(info.clip.name, -1, 0); break; } #endif } } } static void PlaySelf(GameObject obj, bool loop) { if (obj == null) return; ParticleSystem ps = obj.GetComponent(); if (null != ps) { ps.loop = loop; ps.Clear(true); ps.time = 0f; ps.Play(); } Animation anim = obj.GetComponent(); if (null != anim) { anim.wrapMode = loop? WrapMode.Loop : WrapMode.Once; anim.Play(); } Animator amt = obj.GetComponent(); if (null != amt) { #if UNITY_5 AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0); foreach (AnimatorClipInfo info in infs) { info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once; amt.Play(info.clip.name, -1, 0); break; } #else AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0); foreach (AnimatorClipInfo info in infs) { info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once; amt.Play(info.clip.name, -1, 0); break; } #endif } //EnableChildrenAll(obj, true); } public void EnableChildrenAll(GameObject obj, bool enable) { if (obj == null) return; for (int i = obj.transform.childCount - 1; i >= 0; i--) { obj.transform.GetChild(i).gameObject.SetActive(enable); } } }