暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TimelineWindowTimeControl.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using UnityEditorInternal;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. using Object = UnityEngine.Object;
  6. namespace UnityEditor.Timeline
  7. {
  8. class TimelineWindowTimeControl : IAnimationWindowControl
  9. {
  10. [Serializable]
  11. public struct ClipData
  12. {
  13. public double start;
  14. public double duration;
  15. public TrackAsset track;
  16. }
  17. #if UNITY_2021_2_OR_NEWER
  18. const AnimationWindowState.SnapMode k_SnapMode = AnimationWindowState.SnapMode.SnapToFrame;
  19. #else
  20. const AnimationWindowState.SnapMode k_SnapMode = AnimationWindowState.SnapMode.SnapToClipFrame;
  21. #endif
  22. [SerializeField] ClipData m_ClipData;
  23. [SerializeField] TimelineClip m_Clip;
  24. [SerializeField] AnimationWindowState m_AnimWindowState;
  25. TrackAsset track
  26. {
  27. get
  28. {
  29. if (m_Clip != null)
  30. {
  31. return m_Clip.GetParentTrack();
  32. }
  33. return m_ClipData.track;
  34. }
  35. }
  36. static TimelineWindow window
  37. {
  38. get
  39. {
  40. return TimelineWindow.instance;
  41. }
  42. }
  43. static WindowState state
  44. {
  45. get
  46. {
  47. if (window != null)
  48. return window.state;
  49. return null;
  50. }
  51. }
  52. void OnStateChange()
  53. {
  54. if (state != null && state.dirtyStamp > 0 && m_AnimWindowState != null)
  55. m_AnimWindowState.Repaint();
  56. }
  57. public void Init(AnimationWindowState animState, TimelineClip clip)
  58. {
  59. m_Clip = clip;
  60. m_AnimWindowState = animState;
  61. }
  62. public void Init(AnimationWindowState animState, ClipData clip)
  63. {
  64. m_ClipData = clip;
  65. m_AnimWindowState = animState;
  66. }
  67. public override void OnEnable()
  68. {
  69. if (state != null)
  70. state.OnTimeChange += OnStateChange;
  71. base.OnEnable();
  72. }
  73. public void OnDisable()
  74. {
  75. if (state != null)
  76. state.OnTimeChange -= OnStateChange;
  77. }
  78. public override AnimationKeyTime time
  79. {
  80. get
  81. {
  82. if (state == null)
  83. return AnimationKeyTime.Time(0.0f, 0.0f);
  84. return AnimationKeyTime.Time(ToAnimationClipTime(state.editSequence.time), (float)state.referenceSequence.frameRate);
  85. }
  86. }
  87. void ChangeTime(float newTime)
  88. {
  89. if (state != null && state.editSequence.director != null)
  90. {
  91. // avoid rounding errors
  92. var finalTime = ToGlobalTime(newTime);
  93. if (TimeUtility.OnFrameBoundary(finalTime, state.referenceSequence.frameRate, TimeUtility.kFrameRateEpsilon))
  94. finalTime = TimeUtility.RoundToFrame(finalTime, state.referenceSequence.frameRate);
  95. state.editSequence.time = finalTime;
  96. window.Repaint();
  97. }
  98. }
  99. static void ChangeFrame(int frame)
  100. {
  101. if (state != null)
  102. {
  103. state.editSequence.frame = frame;
  104. window.Repaint();
  105. }
  106. }
  107. public override void GoToTime(float newTime)
  108. {
  109. ChangeTime(newTime);
  110. }
  111. public override void GoToFrame(int frame)
  112. {
  113. ChangeFrame(frame);
  114. }
  115. public override void StartScrubTime() { }
  116. public override void EndScrubTime() { }
  117. public override void ScrubTime(float newTime)
  118. {
  119. ChangeTime(newTime);
  120. }
  121. public override void GoToPreviousFrame()
  122. {
  123. if (state != null)
  124. ChangeFrame(state.editSequence.frame - 1);
  125. }
  126. public override void GoToNextFrame()
  127. {
  128. if (state != null)
  129. ChangeFrame(state.editSequence.frame + 1);
  130. }
  131. AnimationWindowCurve[] GetCurves()
  132. {
  133. var curves =
  134. (m_AnimWindowState.showCurveEditor &&
  135. m_AnimWindowState.activeCurves.Count > 0) ? m_AnimWindowState.activeCurves : m_AnimWindowState.allCurves;
  136. return curves.ToArray();
  137. }
  138. public override void GoToPreviousKeyframe()
  139. {
  140. var newTime = AnimationWindowUtility.GetPreviousKeyframeTime(GetCurves(), time.time, m_AnimWindowState.clipFrameRate);
  141. GoToTime(m_AnimWindowState.SnapToFrame(newTime, k_SnapMode));
  142. }
  143. public override void GoToNextKeyframe()
  144. {
  145. var newTime = AnimationWindowUtility.GetNextKeyframeTime(GetCurves(), time.time, m_AnimWindowState.clipFrameRate);
  146. GoToTime(m_AnimWindowState.SnapToFrame(newTime, k_SnapMode));
  147. }
  148. public override void GoToFirstKeyframe()
  149. {
  150. GoToTime(0);
  151. }
  152. public override void GoToLastKeyframe()
  153. {
  154. double animClipTime = 0;
  155. if (m_Clip != null)
  156. {
  157. var curves = m_Clip.curves;
  158. var animAsset = m_Clip.asset as AnimationPlayableAsset;
  159. if (animAsset != null)
  160. {
  161. animClipTime = animAsset.clip != null ? animAsset.clip.length : 0;
  162. }
  163. else if (curves != null)
  164. {
  165. animClipTime = curves.length;
  166. }
  167. else
  168. {
  169. animClipTime = m_Clip.clipAssetDuration;
  170. }
  171. }
  172. else
  173. {
  174. animClipTime = m_ClipData.duration;
  175. }
  176. GoToTime((float)animClipTime);
  177. }
  178. public override bool canPlay
  179. {
  180. get
  181. {
  182. return state != null && state.previewMode;
  183. }
  184. }
  185. public override bool playing
  186. {
  187. get
  188. {
  189. return state != null && state.playing;
  190. }
  191. }
  192. static void SetPlaybackState(bool playbackState)
  193. {
  194. if (state == null || playbackState == state.playing)
  195. return;
  196. state.SetPlaying(playbackState);
  197. }
  198. public override bool StartPlayback()
  199. {
  200. SetPlaybackState(true);
  201. return state != null && state.playing;
  202. }
  203. public override void StopPlayback()
  204. {
  205. SetPlaybackState(false);
  206. }
  207. public override bool PlaybackUpdate() { return state != null && state.playing; }
  208. public override bool canRecord
  209. {
  210. get { return state != null && state.canRecord; }
  211. }
  212. public override bool recording
  213. {
  214. get { return state != null && state.recording; }
  215. }
  216. public override bool canPreview
  217. {
  218. get { return false; }
  219. }
  220. public override bool previewing
  221. {
  222. get { return false; }
  223. }
  224. public override bool StartRecording(Object targetObject)
  225. {
  226. if (!canRecord)
  227. return false;
  228. if (track != null && state != null && !state.ignorePreview)
  229. {
  230. state.ArmForRecord(track);
  231. return state.recording;
  232. }
  233. return false;
  234. }
  235. public override void StopRecording()
  236. {
  237. if (track != null && state != null && !state.ignorePreview)
  238. state.UnarmForRecord(track);
  239. }
  240. public override void OnSelectionChanged() { }
  241. public override void ResampleAnimation() { }
  242. public override bool StartPreview()
  243. {
  244. if (state != null)
  245. state.previewMode = true;
  246. return state != null && state.previewMode;
  247. }
  248. public override void StopPreview()
  249. {
  250. if (state != null)
  251. state.previewMode = false;
  252. }
  253. public override void ProcessCandidates() { }
  254. public override void ClearCandidates() { }
  255. double ToGlobalTime(float localTime)
  256. {
  257. if (m_Clip != null)
  258. return Math.Max(0, m_Clip.FromLocalTimeUnbound(localTime));
  259. return Math.Max(0, m_ClipData.start + localTime);
  260. }
  261. float ToAnimationClipTime(double globalTime)
  262. {
  263. if (m_Clip != null)
  264. return (float)m_Clip.ToLocalTimeUnbound(globalTime);
  265. return (float)(globalTime - m_ClipData.start);
  266. }
  267. }
  268. }