MegaPointCacheAnimator.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. [System.Serializable]
  4. public class MegaPointCacheAnimClip
  5. {
  6. public string name;
  7. public float start;
  8. public float end;
  9. public MegaRepeatMode loop;
  10. public float speed = 1.0f;
  11. public MegaPointCacheAnimClip(string _name, float _start, float _end, MegaRepeatMode _loop)
  12. {
  13. name = _name;
  14. start = _start;
  15. end = _end;
  16. loop = _loop;
  17. }
  18. }
  19. [AddComponentMenu("Modifiers/Point Cache Animator")]
  20. [ExecuteInEditMode]
  21. public class MegaPointCacheAnimator : MonoBehaviour
  22. {
  23. public MegaPointCache mod;
  24. public MegaPointCacheRef modref;
  25. public List<MegaPointCacheAnimClip> clips = new List<MegaPointCacheAnimClip>();
  26. public int current = 0;
  27. public float t = -1.0f; // Current clip time
  28. public float at = 0.0f;
  29. public int sourceFPS = 30;
  30. public bool useFrames = true;
  31. [ContextMenu("Help")]
  32. public void Help()
  33. {
  34. Application.OpenURL("http://www.west-racing.com/mf/?page_id=1802");
  35. }
  36. public bool IsPlaying()
  37. {
  38. if ( t >= 0.0f )
  39. return true;
  40. return false;
  41. }
  42. public void SetTime(float time)
  43. {
  44. t = time;
  45. }
  46. public float GetTime()
  47. {
  48. return at;
  49. }
  50. public void PlayClipEvent(int i)
  51. {
  52. PlayClip(i);
  53. }
  54. public void PlayClipNameEvent(string name)
  55. {
  56. PlayClip(name);
  57. }
  58. public void PlayClip(int i)
  59. {
  60. if ( i < clips.Count )
  61. {
  62. current = i;
  63. t = 0.0f;
  64. }
  65. }
  66. public void PlayClip(string name)
  67. {
  68. for ( int i = 0; i < clips.Count; i++ )
  69. {
  70. if ( clips[i].name == name )
  71. {
  72. current = i;
  73. t = 0.0f;
  74. return;
  75. }
  76. }
  77. }
  78. public void Stop()
  79. {
  80. t = -1.0f;
  81. }
  82. public int AddClip(string name, float start, float end, MegaRepeatMode loop)
  83. {
  84. MegaPointCacheAnimClip clip = new MegaPointCacheAnimClip(name, start, end, loop);
  85. clips.Add(clip);
  86. return clips.Count - 1;
  87. }
  88. public string[] GetClipNames()
  89. {
  90. string[] names = new string[clips.Count];
  91. for ( int i = 0; i < clips.Count; i++ )
  92. {
  93. names[i] = clips[i].name;
  94. }
  95. return names;
  96. }
  97. void Start()
  98. {
  99. if ( PlayOnStart )
  100. {
  101. t = 0.0f;
  102. }
  103. else
  104. t = -1.0f;
  105. }
  106. void Update()
  107. {
  108. DoUpdate();
  109. }
  110. void DoUpdate()
  111. {
  112. if ( mod == null && modref == null )
  113. {
  114. mod = (MegaPointCache)GetComponent<MegaPointCache>();
  115. modref = (MegaPointCacheRef)GetComponent<MegaPointCacheRef>();
  116. }
  117. if ( mod != null || modref != null )
  118. {
  119. if ( LinkedUpdate )
  120. {
  121. DoLinkedUpdate();
  122. }
  123. else
  124. {
  125. if ( clips.Count > 0 && current < clips.Count )
  126. {
  127. if ( t >= 0.0f )
  128. {
  129. t += Time.deltaTime * clips[current].speed;
  130. float dt = clips[current].end - clips[current].start;
  131. switch ( clips[current].loop )
  132. {
  133. //case MegaRepeatMode.Loop: at = Mathf.Repeat(t, dt); break;
  134. case MegaRepeatMode.Loop:
  135. at = Mathf.Repeat(t, Mathf.Abs(dt));
  136. if ( dt < 0.0f )
  137. at = clips[current].start - at;
  138. break;
  139. case MegaRepeatMode.PingPong: at = Mathf.PingPong(t, dt); break;
  140. case MegaRepeatMode.Clamp: at = Mathf.Clamp(t, 0.0f, dt); break;
  141. }
  142. at += clips[current].start;
  143. if ( mod )
  144. mod.SetAnim(at);
  145. else
  146. modref.SetAnim(at);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. public bool LinkedUpdate = false;
  153. public bool PlayOnStart = true;
  154. Animation myanim;
  155. void DoLinkedUpdate()
  156. {
  157. if ( myanim == null )
  158. myanim = GetComponent<Animation>();
  159. if ( myanim != null )
  160. {
  161. foreach ( AnimationState state in myanim )
  162. {
  163. if ( state.enabled )
  164. {
  165. AnimationClip clip = state.clip;
  166. if ( clip != null )
  167. {
  168. for ( int i = 0; i < clips.Count; i++ )
  169. {
  170. if ( clips[i].name == clip.name )
  171. {
  172. current = i;
  173. float ct = state.time;
  174. WrapMode wm = clip.wrapMode;
  175. if ( wm == WrapMode.Default )
  176. {
  177. wm = myanim.wrapMode;
  178. }
  179. switch ( clip.wrapMode )
  180. {
  181. case WrapMode.Loop:
  182. ct = Mathf.Repeat(ct, clip.length);
  183. break;
  184. case WrapMode.PingPong:
  185. ct = Mathf.PingPong(ct, clip.length);
  186. break;
  187. case WrapMode.ClampForever:
  188. ct = Mathf.Clamp(ct, 0.0f, clip.length);
  189. break;
  190. case WrapMode.Once:
  191. if ( ct > clip.length )
  192. ct = 0.0f;
  193. break;
  194. }
  195. ct += clips[current].start;
  196. if ( mod )
  197. mod.SetAnim(ct); //state.time + clips[current].start);
  198. else
  199. modref.SetAnim(ct);
  200. return;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }