DelayPlay.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*************************
  2. * Delay play function
  3. * LiuYu. 153250945@qq.com
  4. ************************/
  5. using UnityEngine;
  6. using System.Collections;
  7. public class DelayPlay : MonoBehaviour {
  8. public float delayTime = 1.0f;
  9. void Awake()
  10. {
  11. EnableSelfAll(false);
  12. EnableChildrenAll(false);
  13. }
  14. void Start ()
  15. {
  16. EnableSelfAll(false);
  17. EnableChildrenAll(false);
  18. InvokePlay();
  19. }
  20. public void InvokePlay(bool loop = false, bool includeChildren = true)
  21. {
  22. EnableChildrenAll(false);
  23. if (loop)
  24. {
  25. if (includeChildren)
  26. Invoke("DelayPlayAllLoop", delayTime);
  27. else
  28. Invoke("DelayPlaySelfLoop", delayTime);
  29. }
  30. else
  31. {
  32. if (includeChildren)
  33. Invoke("DelayPlayAllOnce", delayTime);
  34. else
  35. Invoke("DelayPlaySelfOnce", delayTime);
  36. }
  37. }
  38. void DelayPlayAllLoop()
  39. {
  40. EnableSelfAll(true);
  41. EnableChildrenAll(true);
  42. PlayAll (true);
  43. }
  44. void DelayPlaySelfLoop()
  45. {
  46. EnableSelfAll(true);
  47. EnableChildrenAll(true);
  48. PlaySelf (true);
  49. }
  50. void DelayPlayAllOnce()
  51. {
  52. EnableSelfAll(true);
  53. EnableChildrenAll(true);
  54. PlayAll (false);
  55. }
  56. void DelayPlaySelfOnce()
  57. {
  58. EnableSelfAll(true);
  59. EnableChildrenAll(true);
  60. PlaySelf (false);
  61. }
  62. public void EnableSelfAll (bool enable)
  63. {
  64. ParticleSystem ps = GetComponent<ParticleSystem>();
  65. if (ps != null)
  66. {
  67. if (enable)
  68. ps.Play();
  69. else
  70. ps.Stop();
  71. }
  72. Animator amt = GetComponent<Animator>();
  73. if (amt != null)
  74. {
  75. amt.enabled = enable;
  76. }
  77. Animation an = GetComponent<Animation>();
  78. if (an != null)
  79. {
  80. an.enabled = enable;
  81. }
  82. }
  83. public void EnableChildrenAll(bool enable)
  84. {
  85. for (int i = transform.childCount - 1; i >= 0; i--)
  86. {
  87. transform.GetChild(i).gameObject.SetActive(enable);
  88. }
  89. }
  90. public void PlayAll (bool loop)
  91. {
  92. ParticleSystem[] pss = GetComponentsInChildren<ParticleSystem>(true);
  93. foreach(ParticleSystem ps in pss)
  94. {
  95. ps.loop = loop;
  96. ps.Clear(true);
  97. ps.Play();
  98. }
  99. Animation[] anis = GetComponentsInChildren<Animation>(true);
  100. foreach(Animation an in anis)
  101. {
  102. an.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  103. an.Play();
  104. }
  105. Animator[] amts = GetComponentsInChildren<Animator>();
  106. foreach(Animator amt in amts)
  107. {
  108. if (null != amt)
  109. {
  110. #if UNITY_5
  111. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  112. foreach (AnimatorClipInfo info in infs)
  113. {
  114. info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  115. amt.Play(info.clip.name, -1, 0);
  116. break;
  117. }
  118. #else
  119. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  120. foreach (AnimatorClipInfo info in infs)
  121. {
  122. info.clip.wrapMode = loop? WrapMode.Loop : WrapMode.Once;
  123. amt.Play(info.clip.name, -1, 0);
  124. break;
  125. }
  126. #endif
  127. }
  128. }
  129. }
  130. void PlaySelf(bool loop)
  131. {
  132. ParticleSystem ps = GetComponent<ParticleSystem>();
  133. if (null != ps)
  134. {
  135. ps.loop = false;
  136. ps.Clear(true);
  137. ps.time = 0f;
  138. ps.Play();
  139. }
  140. Animation anim = GetComponent<Animation>();
  141. if (null != anim)
  142. {
  143. anim.wrapMode = WrapMode.Once;
  144. anim.Play();
  145. }
  146. Animator amt = GetComponent<Animator>();
  147. if (null != amt)
  148. {
  149. #if UNITY_5
  150. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  151. foreach (AnimatorClipInfo info in infs)
  152. {
  153. info.clip.wrapMode = WrapMode.Once;
  154. amt.Play(info.clip.name, -1, 0);
  155. break;
  156. }
  157. #else
  158. AnimatorClipInfo[] infs = amt.GetCurrentAnimatorClipInfo(0);
  159. foreach (AnimatorClipInfo info in infs)
  160. {
  161. info.clip.wrapMode = WrapMode.Once;
  162. amt.Play(info.clip.name, -1, 0);
  163. break;
  164. }
  165. #endif
  166. }
  167. }
  168. }