AMRotationTrack.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using UnityEngine;
  5. [Serializable]
  6. public class AMRotationTrack : AMTrack
  7. {
  8. public Transform obj
  9. {
  10. get
  11. {
  12. return this._obj;
  13. }
  14. set
  15. {
  16. if (value != null && this.cache.Count <= 0)
  17. {
  18. this.cachedInitialRotation = value.rotation;
  19. }
  20. this._obj = value;
  21. if (this._obj != null)
  22. {
  23. this.objName = this._obj.name;
  24. }
  25. else
  26. {
  27. this.objName = string.Empty;
  28. }
  29. }
  30. }
  31. public override string getTrackType()
  32. {
  33. return "Rotation";
  34. }
  35. public void addKey(int _frame, Quaternion _rotation)
  36. {
  37. foreach (AMKey amkey in this.keys)
  38. {
  39. AMRotationKey amrotationKey = (AMRotationKey)amkey;
  40. if (amrotationKey.frame == _frame)
  41. {
  42. amrotationKey.rotation = _rotation;
  43. this.updateCache();
  44. return;
  45. }
  46. }
  47. AMRotationKey amrotationKey2 = ScriptableObject.CreateInstance<AMRotationKey>();
  48. amrotationKey2.frame = _frame;
  49. amrotationKey2.rotation = _rotation;
  50. amrotationKey2.easeType = 21;
  51. this.keys.Add(amrotationKey2);
  52. this.updateCache();
  53. }
  54. public bool changeObject(Transform _obj)
  55. {
  56. this.obj = _obj;
  57. this.updateCache();
  58. return true;
  59. }
  60. public override void updateCache()
  61. {
  62. if (this._obj != null)
  63. {
  64. this.objName = this._obj.name;
  65. }
  66. else
  67. {
  68. this.objName = string.Empty;
  69. }
  70. base.sortKeys();
  71. base.destroyCache();
  72. this.cache = new List<AMAction>();
  73. for (int i = 0; i < this.keys.Count; i++)
  74. {
  75. AMRotationAction amrotationAction = ScriptableObject.CreateInstance<AMRotationAction>();
  76. amrotationAction.startFrame = this.keys[i].frame;
  77. if (this.keys.Count > i + 1)
  78. {
  79. amrotationAction.endFrame = this.keys[i + 1].frame;
  80. }
  81. else
  82. {
  83. amrotationAction.endFrame = -1;
  84. }
  85. amrotationAction.obj = this.obj;
  86. amrotationAction.startRotation = (this.keys[i] as AMRotationKey).rotation;
  87. if (amrotationAction.endFrame != -1)
  88. {
  89. amrotationAction.endRotation = (this.keys[i + 1] as AMRotationKey).rotation;
  90. }
  91. amrotationAction.easeType = (this.keys[i] as AMRotationKey).easeType;
  92. amrotationAction.customEase = new List<float>(this.keys[i].customEase);
  93. this.cache.Add(amrotationAction);
  94. }
  95. base.updateCache();
  96. }
  97. public override void previewFrame(float frame, AMTrack extraTrack = null)
  98. {
  99. if (!this.obj)
  100. {
  101. return;
  102. }
  103. if (string.IsNullOrEmpty(this.objName))
  104. {
  105. this.objName = this.obj.name;
  106. }
  107. if (this.cache.Count <= 0)
  108. {
  109. return;
  110. }
  111. if (this.cache[0] == null)
  112. {
  113. this.updateCache();
  114. }
  115. if (frame <= (float)this.cache[0].startFrame || (this.cache[0] as AMRotationAction).endFrame == -1)
  116. {
  117. this.obj.rotation = (this.cache[0] as AMRotationAction).getStartQuaternion();
  118. return;
  119. }
  120. if (frame >= (float)(this.cache[this.cache.Count - 2] as AMRotationAction).endFrame)
  121. {
  122. this.obj.rotation = (this.cache[this.cache.Count - 2] as AMRotationAction).getEndQuaternion();
  123. return;
  124. }
  125. foreach (AMAction amaction in this.cache)
  126. {
  127. AMRotationAction amrotationAction = (AMRotationAction)amaction;
  128. if (frame >= (float)amrotationAction.startFrame && frame <= (float)amrotationAction.endFrame)
  129. {
  130. if (frame == (float)amrotationAction.startFrame)
  131. {
  132. this.obj.rotation = amrotationAction.getStartQuaternion();
  133. break;
  134. }
  135. if (frame == (float)amrotationAction.endFrame)
  136. {
  137. this.obj.rotation = amrotationAction.getEndQuaternion();
  138. break;
  139. }
  140. AnimationCurve curve = null;
  141. AMTween.EasingFunction easingFunction;
  142. if (amrotationAction.hasCustomEase())
  143. {
  144. if (AMRotationTrack.<>f__mg$cache0 == null)
  145. {
  146. AMRotationTrack.<>f__mg$cache0 = new AMTween.EasingFunction(AMTween.customEase);
  147. }
  148. easingFunction = AMRotationTrack.<>f__mg$cache0;
  149. curve = amrotationAction.easeCurve;
  150. }
  151. else
  152. {
  153. easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amrotationAction.easeType);
  154. }
  155. float num = frame - (float)amrotationAction.startFrame;
  156. if (num < 0f)
  157. {
  158. num = 0f;
  159. }
  160. float value = num / (float)amrotationAction.getNumberOfFrames();
  161. Quaternion startQuaternion = amrotationAction.getStartQuaternion();
  162. Quaternion endQuaternion = amrotationAction.getEndQuaternion();
  163. Quaternion rotation = default(Quaternion);
  164. rotation.x = easingFunction(startQuaternion.x, endQuaternion.x, value, curve);
  165. rotation.y = easingFunction(startQuaternion.y, endQuaternion.y, value, curve);
  166. rotation.z = easingFunction(startQuaternion.z, endQuaternion.z, value, curve);
  167. rotation.w = easingFunction(startQuaternion.w, endQuaternion.w, value, curve);
  168. this.obj.rotation = rotation;
  169. break;
  170. }
  171. }
  172. }
  173. public bool autoKey(Transform _obj, int frame)
  174. {
  175. if (!this.obj)
  176. {
  177. return false;
  178. }
  179. if (this.obj != _obj)
  180. {
  181. return false;
  182. }
  183. if (this.cache.Count <= 0)
  184. {
  185. if (_obj.rotation != this.cachedInitialRotation)
  186. {
  187. this.addKey(frame, _obj.rotation);
  188. return true;
  189. }
  190. return false;
  191. }
  192. else
  193. {
  194. Quaternion rotationAtFrame = this.getRotationAtFrame((float)frame);
  195. if (_obj.rotation != rotationAtFrame)
  196. {
  197. this.addKey(frame, _obj.rotation);
  198. return true;
  199. }
  200. return false;
  201. }
  202. }
  203. public Quaternion getRotationAtFrame(float frame)
  204. {
  205. if (frame <= (float)this.cache[0].startFrame || (this.cache[0] as AMRotationAction).endFrame == -1)
  206. {
  207. return (this.cache[0] as AMRotationAction).getStartQuaternion();
  208. }
  209. if (frame >= (float)(this.cache[this.cache.Count - 2] as AMRotationAction).endFrame)
  210. {
  211. return (this.cache[this.cache.Count - 2] as AMRotationAction).getEndQuaternion();
  212. }
  213. foreach (AMAction amaction in this.cache)
  214. {
  215. AMRotationAction amrotationAction = (AMRotationAction)amaction;
  216. if (frame >= (float)amrotationAction.startFrame && frame <= (float)amrotationAction.endFrame)
  217. {
  218. if (frame == (float)amrotationAction.startFrame)
  219. {
  220. return amrotationAction.getStartQuaternion();
  221. }
  222. if (frame == (float)amrotationAction.endFrame)
  223. {
  224. return amrotationAction.getEndQuaternion();
  225. }
  226. AnimationCurve curve = null;
  227. AMTween.EasingFunction easingFunction;
  228. if (amrotationAction.hasCustomEase())
  229. {
  230. if (AMRotationTrack.<>f__mg$cache1 == null)
  231. {
  232. AMRotationTrack.<>f__mg$cache1 = new AMTween.EasingFunction(AMTween.customEase);
  233. }
  234. easingFunction = AMRotationTrack.<>f__mg$cache1;
  235. curve = amrotationAction.easeCurve;
  236. }
  237. else
  238. {
  239. easingFunction = AMTween.GetEasingFunction((AMTween.EaseType)amrotationAction.easeType);
  240. }
  241. float num = frame - (float)amrotationAction.startFrame;
  242. if (num < 0f)
  243. {
  244. num = 0f;
  245. }
  246. float value = num / (float)amrotationAction.getNumberOfFrames();
  247. Quaternion startQuaternion = amrotationAction.getStartQuaternion();
  248. Quaternion endQuaternion = amrotationAction.getEndQuaternion();
  249. return new Quaternion
  250. {
  251. x = easingFunction(startQuaternion.x, endQuaternion.x, value, curve),
  252. y = easingFunction(startQuaternion.y, endQuaternion.y, value, curve),
  253. z = easingFunction(startQuaternion.z, endQuaternion.z, value, curve),
  254. w = easingFunction(startQuaternion.w, endQuaternion.w, value, curve)
  255. };
  256. }
  257. }
  258. Debug.LogError(string.Concat(new object[]
  259. {
  260. "Animator: Could not get ",
  261. this.obj.name,
  262. " rotation at frame '",
  263. frame,
  264. "'"
  265. }));
  266. return new Quaternion(0f, 0f, 0f, 0f);
  267. }
  268. public Vector4 getInitialRotation()
  269. {
  270. return (this.keys[0] as AMRotationKey).getRotationQuaternion();
  271. }
  272. public override AnimatorTimeline.JSONInit getJSONInit()
  273. {
  274. if (!this.obj || this.keys.Count <= 0)
  275. {
  276. return null;
  277. }
  278. AnimatorTimeline.JSONInit jsoninit = new AnimatorTimeline.JSONInit();
  279. jsoninit.type = "rotation";
  280. jsoninit.go = this.obj.gameObject.name;
  281. AnimatorTimeline.JSONQuaternion jsonquaternion = new AnimatorTimeline.JSONQuaternion();
  282. jsonquaternion.setValue(this.getInitialRotation());
  283. jsoninit.rotation = jsonquaternion;
  284. return jsoninit;
  285. }
  286. public override List<GameObject> getDependencies()
  287. {
  288. List<GameObject> list = new List<GameObject>();
  289. if (this.obj)
  290. {
  291. list.Add(this.obj.gameObject);
  292. }
  293. return list;
  294. }
  295. public override List<GameObject> updateDependencies(List<GameObject> newReferences, List<GameObject> oldReferences)
  296. {
  297. if (!this.obj)
  298. {
  299. return new List<GameObject>();
  300. }
  301. for (int i = 0; i < oldReferences.Count; i++)
  302. {
  303. if (oldReferences[i] == this.obj.gameObject)
  304. {
  305. this.obj = newReferences[i].transform;
  306. break;
  307. }
  308. }
  309. return new List<GameObject>();
  310. }
  311. [SerializeField]
  312. private Transform _obj;
  313. public string objName;
  314. public Quaternion cachedInitialRotation;
  315. [CompilerGenerated]
  316. private static AMTween.EasingFunction <>f__mg$cache0;
  317. [CompilerGenerated]
  318. private static AMTween.EasingFunction <>f__mg$cache1;
  319. }