ParticleAndAnimation.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. [CustomEditor(typeof(ParticleAndAnimation))]
  5. public class ParticleAndAnimation : Editor
  6. {
  7. private static GameObject SelectObj;
  8. #if UNITY_EDITOR
  9. void Start ()
  10. {
  11. PlayOnce();
  12. }
  13. #endif
  14. [MenuItem("Tools/Play Loop %l")]
  15. //[ContextMenu("Play Loop")]
  16. public static void PlayLoop()
  17. {
  18. Object[] objs = Selection.objects;
  19. if(objs.Length > 0)
  20. {
  21. ParticleAndAnimation.SelectObj = objs[0] as GameObject;
  22. }
  23. //PlaySelf(gameObject, true);
  24. Transform[] trans = SelectObj.GetComponentsInChildren<Transform>(true);
  25. foreach(Transform t in trans)
  26. {
  27. DelayPlay d = t.GetComponent<DelayPlay>();
  28. if (d != null)
  29. {
  30. if (d.delayTime >= 0)
  31. {
  32. d.InvokePlay(true, false);
  33. }
  34. }
  35. else
  36. {
  37. PlaySelf(t.gameObject, true);
  38. }
  39. }
  40. }
  41. //[ContextMenu("Play Once")]
  42. [MenuItem("Tools/Play Once %;")]
  43. public static void PlayOnce ()
  44. {
  45. //PlaySelf(gameObject, false);
  46. Object[] objs = Selection.objects;
  47. if (objs.Length > 0)
  48. {
  49. ParticleAndAnimation.SelectObj = objs[0] as GameObject;
  50. }
  51. Transform[] trans = SelectObj.GetComponentsInChildren<Transform>(true);
  52. foreach(Transform t in trans)
  53. {
  54. DelayPlay d = t.GetComponent<DelayPlay>();
  55. if (d != null)
  56. {
  57. if (d.delayTime >= 0)
  58. {
  59. d.InvokePlay(false, false);
  60. }
  61. }
  62. else
  63. {
  64. PlaySelf(t.gameObject, false);
  65. }
  66. }
  67. }
  68. void PlaySelfAndAllChildren(GameObject obj, bool loop)
  69. {
  70. if (obj == null)
  71. return;
  72. //EnableChildrenAll(obj, true);
  73. ParticleSystem[] pss = obj.GetComponentsInChildren<ParticleSystem>(true);
  74. foreach(ParticleSystem ps in pss)
  75. {
  76. ps.loop = loop;
  77. ps.Clear(true);
  78. ps.Play();
  79. }
  80. Animation[] anis = obj.GetComponentsInChildren<Animation>(true);
  81. foreach(Animation an in anis)
  82. {
  83. an.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  84. an.Play();
  85. }
  86. Animator[] amts = obj.GetComponentsInChildren<Animator>();
  87. foreach(Animator amt in amts)
  88. {
  89. if (null != amt)
  90. {
  91. #if UNITY_5
  92. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  93. foreach (AnimatorClipInfo info in infs)
  94. {
  95. info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  96. amt.Play(info.clip.name, -1, 0);
  97. break;
  98. }
  99. #else
  100. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  101. foreach (AnimatorClipInfo info in infs)
  102. {
  103. info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  104. amt.Play(info.clip.name, -1, 0);
  105. break;
  106. }
  107. #endif
  108. }
  109. }
  110. }
  111. static void PlaySelf(GameObject obj, bool loop)
  112. {
  113. if (obj == null)
  114. return;
  115. ParticleSystem ps = obj.GetComponent<ParticleSystem>();
  116. if (null != ps)
  117. {
  118. ps.loop = loop;
  119. ps.Clear(true);
  120. ps.time = 0f;
  121. ps.Play();
  122. }
  123. Animation anim = obj.GetComponent<Animation>();
  124. if (null != anim)
  125. {
  126. anim.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  127. anim.Play();
  128. }
  129. Animator amt = obj.GetComponent<Animator>();
  130. if (null != amt)
  131. {
  132. #if UNITY_5
  133. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  134. foreach (AnimatorClipInfo info in infs)
  135. {
  136. info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  137. amt.Play(info.clip.name, -1, 0);
  138. break;
  139. }
  140. #else
  141. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  142. foreach (AnimatorClipInfo info in infs)
  143. {
  144. info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  145. amt.Play(info.clip.name, -1, 0);
  146. break;
  147. }
  148. #endif
  149. }
  150. //EnableChildrenAll(obj, true);
  151. }
  152. public void EnableChildrenAll(GameObject obj, bool enable)
  153. {
  154. if (obj == null)
  155. return;
  156. for (int i = obj.transform.childCount - 1; i >= 0; i--)
  157. {
  158. obj.transform.GetChild(i).gameObject.SetActive(enable);
  159. }
  160. }
  161. }