AMAnimationTrack.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [Serializable]
  5. public class AMAnimationTrack : AMTrack
  6. {
  7. public override string getTrackType()
  8. {
  9. return "Animation";
  10. }
  11. public bool setObject(GameObject obj)
  12. {
  13. if (this.obj != obj)
  14. {
  15. this.obj = obj;
  16. return true;
  17. }
  18. return false;
  19. }
  20. public void addKey(int _frame, AnimationClip _clip, WrapMode _wrapMode)
  21. {
  22. foreach (AMKey amkey in this.keys)
  23. {
  24. AMAnimationKey amanimationKey = (AMAnimationKey)amkey;
  25. if (amanimationKey.frame == _frame)
  26. {
  27. amanimationKey.amClip = _clip;
  28. amanimationKey.wrapMode = _wrapMode;
  29. this.updateCache();
  30. return;
  31. }
  32. }
  33. AMAnimationKey amanimationKey2 = ScriptableObject.CreateInstance<AMAnimationKey>();
  34. amanimationKey2.frame = _frame;
  35. amanimationKey2.amClip = _clip;
  36. amanimationKey2.wrapMode = _wrapMode;
  37. this.keys.Add(amanimationKey2);
  38. this.updateCache();
  39. }
  40. public override void updateCache()
  41. {
  42. base.destroyCache();
  43. this.cache = new List<AMAction>();
  44. base.sortKeys();
  45. for (int i = 0; i < this.keys.Count; i++)
  46. {
  47. AMAnimationAction amanimationAction = ScriptableObject.CreateInstance<AMAnimationAction>();
  48. amanimationAction.startFrame = this.keys[i].frame;
  49. amanimationAction.obj = this.obj;
  50. amanimationAction.amClip = (this.keys[i] as AMAnimationKey).amClip;
  51. amanimationAction.wrapMode = (this.keys[i] as AMAnimationKey).wrapMode;
  52. amanimationAction.crossfade = (this.keys[i] as AMAnimationKey).crossfade;
  53. amanimationAction.crossfadeTime = (this.keys[i] as AMAnimationKey).crossfadeTime;
  54. this.cache.Add(amanimationAction);
  55. }
  56. base.updateCache();
  57. }
  58. public void previewFrame(float frame, float frameRate)
  59. {
  60. if (!this.obj)
  61. {
  62. return;
  63. }
  64. if (this.cache.Count <= 0)
  65. {
  66. return;
  67. }
  68. bool flag = false;
  69. for (int i = this.cache.Count - 1; i >= 0; i--)
  70. {
  71. if ((float)this.cache[i].startFrame <= frame)
  72. {
  73. AnimationClip amClip = (this.cache[i] as AMAnimationAction).amClip;
  74. if (amClip)
  75. {
  76. amClip.wrapMode = (this.cache[i] as AMAnimationAction).wrapMode;
  77. amClip.SampleAnimation(this.obj, this.getTime(frameRate, frame - (float)this.cache[i].startFrame));
  78. }
  79. flag = true;
  80. break;
  81. }
  82. }
  83. Animation component = this.obj.GetComponent<Animation>();
  84. if (!flag && component.clip)
  85. {
  86. component.clip.SampleAnimation(this.obj, 0f);
  87. }
  88. }
  89. public float getTime(float frameRate, float numberOfFrames)
  90. {
  91. return numberOfFrames / frameRate;
  92. }
  93. public override AnimatorTimeline.JSONInit getJSONInit()
  94. {
  95. return null;
  96. }
  97. public override List<GameObject> getDependencies()
  98. {
  99. List<GameObject> list = new List<GameObject>();
  100. if (this.obj)
  101. {
  102. list.Add(this.obj);
  103. }
  104. return list;
  105. }
  106. public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  107. {
  108. List<GameObject> list = new List<GameObject>();
  109. if (!this.obj)
  110. {
  111. return list;
  112. }
  113. int i = 0;
  114. while (i < oldReferences.Count)
  115. {
  116. if (oldReferences[i] == this.obj)
  117. {
  118. if (!newReferences[i].GetComponent(typeof(Animation)))
  119. {
  120. Debug.LogWarning("Animator: Animation Track component 'Animation' not found on new reference for GameObject '" + this.obj.name + "'. Duplicate not replaced.");
  121. list.Add(oldReferences[i]);
  122. return list;
  123. }
  124. this.obj = newReferences[i];
  125. break;
  126. }
  127. else
  128. {
  129. i++;
  130. }
  131. }
  132. return list;
  133. }
  134. public GameObject obj;
  135. }