TimelineClip.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Serialization;
  5. namespace UnityEngine.Timeline
  6. {
  7. /// <summary>
  8. /// Implement this interface to support advanced features of timeline clips.
  9. /// </summary>
  10. public interface ITimelineClipAsset
  11. {
  12. /// <summary>
  13. /// Returns a description of the features supported by clips with PlayableAssets implementing this interface.
  14. /// </summary>
  15. ClipCaps clipCaps { get; }
  16. }
  17. /// <summary>
  18. /// Represents a clip on the timeline.
  19. /// </summary>
  20. [Serializable]
  21. public partial class TimelineClip : ICurvesOwner, ISerializationCallbackReceiver
  22. {
  23. /// <summary>
  24. /// The default capabilities for a clip
  25. /// </summary>
  26. public static readonly ClipCaps kDefaultClipCaps = ClipCaps.Blending;
  27. /// <summary>
  28. /// The default length of a clip in seconds.
  29. /// </summary>
  30. public static readonly float kDefaultClipDurationInSeconds = 5;
  31. /// <summary>
  32. /// The minimum timescale allowed on a clip
  33. /// </summary>
  34. public static readonly double kTimeScaleMin = 1.0 / 1000;
  35. /// <summary>
  36. /// The maximum timescale allowed on a clip
  37. /// </summary>
  38. public static readonly double kTimeScaleMax = 1000;
  39. internal static readonly string kDefaultCurvesName = "Clip Parameters";
  40. internal static readonly double kMinDuration = 1 / 60.0;
  41. // constant representing the longest possible sequence duration
  42. internal static readonly double kMaxTimeValue = 1000000; // more than a week's time, and within numerical precision boundaries
  43. /// <summary>
  44. /// How the clip handles time outside its start and end range.
  45. /// </summary>
  46. public enum ClipExtrapolation
  47. {
  48. /// <summary>
  49. /// No extrapolation is applied.
  50. /// </summary>
  51. None,
  52. /// <summary>
  53. /// Hold the time at the end value of the clip.
  54. /// </summary>
  55. Hold,
  56. /// <summary>
  57. /// Repeat time values outside the start/end range.
  58. /// </summary>
  59. Loop,
  60. /// <summary>
  61. /// Repeat time values outside the start/end range, reversing direction at each loop
  62. /// </summary>
  63. PingPong,
  64. /// <summary>
  65. /// Time values are passed in without modification, extending beyond the clips range
  66. /// </summary>
  67. Continue
  68. };
  69. /// <summary>
  70. /// How blend curves are treated in an overlap
  71. /// </summary>
  72. public enum BlendCurveMode
  73. {
  74. /// <summary>
  75. /// The curve is normalized against the opposing clip
  76. /// </summary>
  77. Auto,
  78. /// <summary>
  79. /// The blend curve is fixed.
  80. /// </summary>
  81. Manual
  82. };
  83. internal TimelineClip(TrackAsset parent)
  84. {
  85. // parent clip into track
  86. parentTrack = parent;
  87. }
  88. [SerializeField] double m_Start;
  89. [SerializeField] double m_ClipIn;
  90. [SerializeField] Object m_Asset;
  91. [SerializeField][FormerlySerializedAs("m_HackDuration")] double m_Duration;
  92. [SerializeField] double m_TimeScale = 1.0;
  93. [SerializeField] TrackAsset m_ParentTrack;
  94. // for mixing out scripts - default is no mix out (i.e. flat)
  95. [SerializeField] double m_EaseInDuration;
  96. [SerializeField] double m_EaseOutDuration;
  97. // the blend durations override ease in / out durations
  98. [SerializeField] double m_BlendInDuration = -1.0f;
  99. [SerializeField] double m_BlendOutDuration = -1.0f;
  100. // doubles as ease in/out and blend in/out curves
  101. [SerializeField] AnimationCurve m_MixInCurve;
  102. [SerializeField] AnimationCurve m_MixOutCurve;
  103. [SerializeField] BlendCurveMode m_BlendInCurveMode = BlendCurveMode.Auto;
  104. [SerializeField] BlendCurveMode m_BlendOutCurveMode = BlendCurveMode.Auto;
  105. [SerializeField] List<string> m_ExposedParameterNames;
  106. [SerializeField] AnimationClip m_AnimationCurves;
  107. [SerializeField] bool m_Recordable;
  108. // extrapolation
  109. [SerializeField] ClipExtrapolation m_PostExtrapolationMode;
  110. [SerializeField] ClipExtrapolation m_PreExtrapolationMode;
  111. [SerializeField] double m_PostExtrapolationTime;
  112. [SerializeField] double m_PreExtrapolationTime;
  113. [SerializeField] string m_DisplayName;
  114. /// <summary>
  115. /// Is the clip being extrapolated before its start time?
  116. /// </summary>
  117. public bool hasPreExtrapolation
  118. {
  119. get { return m_PreExtrapolationMode != ClipExtrapolation.None && m_PreExtrapolationTime > 0; }
  120. }
  121. /// <summary>
  122. /// Is the clip being extrapolated past its end time?
  123. /// </summary>
  124. public bool hasPostExtrapolation
  125. {
  126. get { return m_PostExtrapolationMode != ClipExtrapolation.None && m_PostExtrapolationTime > 0; }
  127. }
  128. /// <summary>
  129. /// A speed multiplier for the clip;
  130. /// </summary>
  131. public double timeScale
  132. {
  133. get { return clipCaps.HasAny(ClipCaps.SpeedMultiplier) ? Math.Max(kTimeScaleMin, Math.Min(m_TimeScale, kTimeScaleMax)) : 1.0; }
  134. set
  135. {
  136. UpdateDirty(m_TimeScale, value);
  137. m_TimeScale = clipCaps.HasAny(ClipCaps.SpeedMultiplier) ? Math.Max(kTimeScaleMin, Math.Min(value, kTimeScaleMax)) : 1.0;
  138. }
  139. }
  140. /// <summary>
  141. /// The start time, in seconds, of the clip
  142. /// </summary>
  143. public double start
  144. {
  145. get { return m_Start; }
  146. set
  147. {
  148. UpdateDirty(value, m_Start);
  149. var newValue = Math.Max(SanitizeTimeValue(value, m_Start), 0);
  150. if (m_ParentTrack != null && m_Start != newValue)
  151. {
  152. m_ParentTrack.OnClipMove();
  153. }
  154. m_Start = newValue;
  155. }
  156. }
  157. /// <summary>
  158. /// The length, in seconds, of the clip
  159. /// </summary>
  160. public double duration
  161. {
  162. get { return m_Duration; }
  163. set
  164. {
  165. UpdateDirty(m_Duration, value);
  166. m_Duration = Math.Max(SanitizeTimeValue(value, m_Duration), double.Epsilon);
  167. }
  168. }
  169. /// <summary>
  170. /// The end time, in seconds of the clip
  171. /// </summary>
  172. public double end
  173. {
  174. get { return m_Start + m_Duration; }
  175. }
  176. /// <summary>
  177. /// Local offset time of the clip.
  178. /// </summary>
  179. public double clipIn
  180. {
  181. get { return clipCaps.HasAny(ClipCaps.ClipIn) ? m_ClipIn : 0; }
  182. set
  183. {
  184. UpdateDirty(m_ClipIn, value);
  185. m_ClipIn = clipCaps.HasAny(ClipCaps.ClipIn) ? Math.Max(Math.Min(SanitizeTimeValue(value, m_ClipIn), kMaxTimeValue), 0.0) : 0;
  186. }
  187. }
  188. /// <summary>
  189. /// The name displayed on the clip
  190. /// </summary>
  191. public string displayName
  192. {
  193. get { return m_DisplayName; }
  194. set { m_DisplayName = value; }
  195. }
  196. /// <summary>
  197. /// The length, in seconds, of the PlayableAsset attached to the clip.
  198. /// </summary>
  199. public double clipAssetDuration
  200. {
  201. get
  202. {
  203. var playableAsset = m_Asset as IPlayableAsset;
  204. return playableAsset != null ? playableAsset.duration : double.MaxValue;
  205. }
  206. }
  207. /// <summary>
  208. /// An animation clip containing animated properties of the attached PlayableAsset
  209. /// </summary>
  210. /// <remarks>
  211. /// This is where animated clip properties are stored.
  212. /// </remarks>
  213. public AnimationClip curves
  214. {
  215. get { return m_AnimationCurves; }
  216. internal set { m_AnimationCurves = value; }
  217. }
  218. string ICurvesOwner.defaultCurvesName
  219. {
  220. get { return kDefaultCurvesName; }
  221. }
  222. /// <summary>
  223. /// Whether this clip contains animated properties for the attached PlayableAsset.
  224. /// </summary>
  225. /// <remarks>
  226. /// This property is false if the curves property is null or if it contains no information.
  227. /// </remarks>
  228. public bool hasCurves
  229. {
  230. get { return m_AnimationCurves != null && !m_AnimationCurves.empty; }
  231. }
  232. /// <summary>
  233. /// The PlayableAsset attached to the clip.
  234. /// </summary>
  235. public Object asset
  236. {
  237. get { return m_Asset; }
  238. set { m_Asset = value; }
  239. }
  240. Object ICurvesOwner.assetOwner
  241. {
  242. get { return parentTrack; }
  243. }
  244. TrackAsset ICurvesOwner.targetTrack
  245. {
  246. get { return parentTrack; }
  247. }
  248. [Obsolete("underlyingAsset property is obsolete. Use asset property instead", true)]
  249. public Object underlyingAsset
  250. {
  251. get { return null; }
  252. set {}
  253. }
  254. /// <summary>
  255. /// Returns the TrackAsset to which this clip is attached.
  256. /// </summary>
  257. public TrackAsset parentTrack
  258. {
  259. get { return m_ParentTrack; }
  260. set
  261. {
  262. if (m_ParentTrack == value)
  263. return;
  264. if (m_ParentTrack != null)
  265. m_ParentTrack.RemoveClip(this);
  266. m_ParentTrack = value;
  267. if (m_ParentTrack != null)
  268. m_ParentTrack.AddClip(this);
  269. }
  270. }
  271. /// <summary>
  272. /// The ease in duration of the timeline clip in seconds. This only applies if the start of the clip is not overlapping.
  273. /// </summary>
  274. public double easeInDuration
  275. {
  276. get { return clipCaps.HasAny(ClipCaps.Blending) ? Math.Min(Math.Max(m_EaseInDuration, 0), duration * 0.49) : 0; }
  277. set { m_EaseInDuration = clipCaps.HasAny(ClipCaps.Blending) ? Math.Max(0, Math.Min(SanitizeTimeValue(value, m_EaseInDuration), duration * 0.49)) : 0; }
  278. }
  279. /// <summary>
  280. /// The ease out duration of the timeline clip in seconds. This only applies if the end of the clip is not overlapping.
  281. /// </summary>
  282. public double easeOutDuration
  283. {
  284. get { return clipCaps.HasAny(ClipCaps.Blending) ? Math.Min(Math.Max(m_EaseOutDuration, 0), duration * 0.49) : 0; }
  285. set { m_EaseOutDuration = clipCaps.HasAny(ClipCaps.Blending) ? Math.Max(0, Math.Min(SanitizeTimeValue(value, m_EaseOutDuration), duration * 0.49)) : 0; }
  286. }
  287. [Obsolete("Use easeOutTime instead (UnityUpgradable) -> easeOutTime", true)]
  288. public double eastOutTime
  289. {
  290. get { return duration - easeOutDuration + m_Start; }
  291. }
  292. /// <summary>
  293. /// The time in seconds that the ease out begins
  294. /// </summary>
  295. public double easeOutTime
  296. {
  297. get { return duration - easeOutDuration + m_Start; }
  298. }
  299. /// <summary>
  300. /// The amount of overlap in seconds on the start of a clip.
  301. /// </summary>
  302. public double blendInDuration
  303. {
  304. get { return clipCaps.HasAny(ClipCaps.Blending) ? m_BlendInDuration : 0; }
  305. set { m_BlendInDuration = clipCaps.HasAny(ClipCaps.Blending) ? SanitizeTimeValue(value, m_BlendInDuration) : 0; }
  306. }
  307. /// <summary>
  308. /// The amount of overlap in seconds at the end of a clip.
  309. /// </summary>
  310. public double blendOutDuration
  311. {
  312. get { return clipCaps.HasAny(ClipCaps.Blending) ? m_BlendOutDuration : 0; }
  313. set { m_BlendOutDuration = clipCaps.HasAny(ClipCaps.Blending) ? SanitizeTimeValue(value, m_BlendOutDuration) : 0; }
  314. }
  315. /// <summary>
  316. /// The mode for calculating the blend curve of the overlap at the start of the clip
  317. /// </summary>
  318. public BlendCurveMode blendInCurveMode
  319. {
  320. get { return m_BlendInCurveMode; }
  321. set { m_BlendInCurveMode = value; }
  322. }
  323. /// <summary>
  324. /// The mode for calculating the blend curve of the overlap at the end of the clip
  325. /// </summary>
  326. public BlendCurveMode blendOutCurveMode
  327. {
  328. get { return m_BlendOutCurveMode; }
  329. set { m_BlendOutCurveMode = value; }
  330. }
  331. /// <summary>
  332. /// Returns whether the clip is blending in
  333. /// </summary>
  334. public bool hasBlendIn { get { return clipCaps.HasAny(ClipCaps.Blending) && m_BlendInDuration > 0; } }
  335. /// <summary>
  336. /// Returns whether the clip is blending out
  337. /// </summary>
  338. public bool hasBlendOut { get { return clipCaps.HasAny(ClipCaps.Blending) && m_BlendOutDuration > 0; } }
  339. /// <summary>
  340. /// The animation curve used for calculating weights during an ease in or a blend in.
  341. /// </summary>
  342. public AnimationCurve mixInCurve
  343. {
  344. get
  345. {
  346. // auto fix broken curves
  347. if (m_MixInCurve == null || m_MixInCurve.length < 2)
  348. m_MixInCurve = GetDefaultMixInCurve();
  349. return m_MixInCurve;
  350. }
  351. set { m_MixInCurve = value; }
  352. }
  353. /// <summary>
  354. /// The amount of the clip being used for ease or blend in as a percentage
  355. /// </summary>
  356. public float mixInPercentage
  357. {
  358. get { return (float)(mixInDuration / duration); }
  359. }
  360. /// <summary>
  361. /// The amount of the clip blending or easing in, in seconds
  362. /// </summary>
  363. public double mixInDuration
  364. {
  365. get { return hasBlendIn ? blendInDuration : easeInDuration; }
  366. }
  367. /// <summary>
  368. /// The animation curve used for calculating weights during an ease out or a blend out.
  369. /// </summary>
  370. public AnimationCurve mixOutCurve
  371. {
  372. get
  373. {
  374. if (m_MixOutCurve == null || m_MixOutCurve.length < 2)
  375. m_MixOutCurve = GetDefaultMixOutCurve();
  376. return m_MixOutCurve;
  377. }
  378. set { m_MixOutCurve = value; }
  379. }
  380. /// <summary>
  381. /// The time in seconds that an ease out or blend out starts
  382. /// </summary>
  383. public double mixOutTime
  384. {
  385. get { return duration - mixOutDuration + m_Start; }
  386. }
  387. /// <summary>
  388. /// The amount of the clip blending or easing out, in seconds
  389. /// </summary>
  390. public double mixOutDuration
  391. {
  392. get { return hasBlendOut ? blendOutDuration : easeOutDuration; }
  393. }
  394. /// <summary>
  395. /// The amount of the clip being used for ease or blend out as a percentage
  396. /// </summary>
  397. public float mixOutPercentage
  398. {
  399. get { return (float)(mixOutDuration / duration); }
  400. }
  401. /// <summary>
  402. /// Returns whether this clip is recordable in editor
  403. /// </summary>
  404. public bool recordable
  405. {
  406. get { return m_Recordable; }
  407. internal set { m_Recordable = value; }
  408. }
  409. [Obsolete("exposedParameter is deprecated and will be removed in a future release", true)]
  410. public List<string> exposedParameters
  411. {
  412. get { return m_ExposedParameterNames ?? (m_ExposedParameterNames = new List<string>()); }
  413. }
  414. /// <summary>
  415. /// Returns the capabilities supported by this clip.
  416. /// </summary>
  417. public ClipCaps clipCaps
  418. {
  419. get
  420. {
  421. var clipAsset = asset as ITimelineClipAsset;
  422. return (clipAsset != null) ? clipAsset.clipCaps : kDefaultClipCaps;
  423. }
  424. }
  425. internal int Hash()
  426. {
  427. return HashUtility.CombineHash(m_Start.GetHashCode(),
  428. m_Duration.GetHashCode(),
  429. m_TimeScale.GetHashCode(),
  430. m_ClipIn.GetHashCode(),
  431. ((int)m_PreExtrapolationMode).GetHashCode(),
  432. ((int)m_PostExtrapolationMode).GetHashCode());
  433. }
  434. /// <summary>
  435. /// Given a time, returns the weight from the mix out
  436. /// </summary>
  437. /// <param name="time">Time (relative to the timeline)</param>
  438. /// <returns></returns>
  439. public float EvaluateMixOut(double time)
  440. {
  441. if (!clipCaps.HasAny(ClipCaps.Blending))
  442. return 1.0f;
  443. if (mixOutDuration > Mathf.Epsilon)
  444. {
  445. var perc = (float)(time - mixOutTime) / (float)mixOutDuration;
  446. perc = Mathf.Clamp01(mixOutCurve.Evaluate(perc));
  447. return perc;
  448. }
  449. return 1.0f;
  450. }
  451. /// <summary>
  452. /// Given a time, returns the weight from the mix in
  453. /// </summary>
  454. /// <param name="time">Time (relative to the timeline)</param>
  455. /// <returns></returns>
  456. public float EvaluateMixIn(double time)
  457. {
  458. if (!clipCaps.HasAny(ClipCaps.Blending))
  459. return 1.0f;
  460. if (mixInDuration > Mathf.Epsilon)
  461. {
  462. var perc = (float)(time - m_Start) / (float)mixInDuration;
  463. perc = Mathf.Clamp01(mixInCurve.Evaluate(perc));
  464. return perc;
  465. }
  466. return 1.0f;
  467. }
  468. static AnimationCurve GetDefaultMixInCurve()
  469. {
  470. return AnimationCurve.EaseInOut(0, 0, 1, 1);
  471. }
  472. static AnimationCurve GetDefaultMixOutCurve()
  473. {
  474. return AnimationCurve.EaseInOut(0, 1, 1, 0);
  475. }
  476. /// <summary>
  477. /// Converts from global time to a clips local time.
  478. /// </summary>
  479. /// <param name="time">time relative to the timeline</param>
  480. /// <returns>
  481. /// The local time with extrapolation applied
  482. /// </returns>
  483. public double ToLocalTime(double time)
  484. {
  485. if (time < 0)
  486. return time;
  487. // handle Extrapolation
  488. if (IsPreExtrapolatedTime(time))
  489. time = GetExtrapolatedTime(time - m_Start, m_PreExtrapolationMode, m_Duration);
  490. else if (IsPostExtrapolatedTime(time))
  491. time = GetExtrapolatedTime(time - m_Start, m_PostExtrapolationMode, m_Duration);
  492. else
  493. time -= m_Start;
  494. // handle looping and time scale within the clip
  495. time *= timeScale;
  496. time += clipIn;
  497. return time;
  498. }
  499. /// <summary>
  500. /// Converts from global time to local time of the clip
  501. /// </summary>
  502. /// <param name="time">The time relative to the timeline</param>
  503. /// <returns>The local time, ignoring any extrapolation or bounds</returns>
  504. public double ToLocalTimeUnbound(double time)
  505. {
  506. return (time - m_Start) * timeScale + clipIn;
  507. }
  508. /// <summary>
  509. /// Converts from local time of the clip to global time
  510. /// </summary>
  511. /// <param name="time">Time relative to the clip</param>
  512. /// <returns>The time relative to the timeline</returns>
  513. internal double FromLocalTimeUnbound(double time)
  514. {
  515. return (time - clipIn) / timeScale + m_Start;
  516. }
  517. /// <summary>
  518. /// If this contains an animation asset, returns the animation clip attached. Otherwise returns null.
  519. /// </summary>
  520. public AnimationClip animationClip
  521. {
  522. get
  523. {
  524. if (m_Asset == null)
  525. return null;
  526. var playableAsset = m_Asset as AnimationPlayableAsset;
  527. return playableAsset != null ? playableAsset.clip : null;
  528. }
  529. }
  530. static double SanitizeTimeValue(double value, double defaultValue)
  531. {
  532. if (double.IsInfinity(value) || double.IsNaN(value))
  533. {
  534. Debug.LogError("Invalid time value assigned");
  535. return defaultValue;
  536. }
  537. return Math.Max(-kMaxTimeValue, Math.Min(kMaxTimeValue, value));
  538. }
  539. /// <summary>
  540. /// Returns whether the clip is being extrapolated past the end time.
  541. /// </summary>
  542. public ClipExtrapolation postExtrapolationMode
  543. {
  544. get { return clipCaps.HasAny(ClipCaps.Extrapolation) ? m_PostExtrapolationMode : ClipExtrapolation.None; }
  545. internal set { m_PostExtrapolationMode = clipCaps.HasAny(ClipCaps.Extrapolation) ? value : ClipExtrapolation.None; }
  546. }
  547. /// <summary>
  548. /// Returns whether the clip is being extrapolated before the start time.
  549. /// </summary>
  550. public ClipExtrapolation preExtrapolationMode
  551. {
  552. get { return clipCaps.HasAny(ClipCaps.Extrapolation) ? m_PreExtrapolationMode : ClipExtrapolation.None; }
  553. internal set { m_PreExtrapolationMode = clipCaps.HasAny(ClipCaps.Extrapolation) ? value : ClipExtrapolation.None; }
  554. }
  555. internal void SetPostExtrapolationTime(double time)
  556. {
  557. m_PostExtrapolationTime = time;
  558. }
  559. internal void SetPreExtrapolationTime(double time)
  560. {
  561. m_PreExtrapolationTime = time;
  562. }
  563. /// <summary>
  564. /// Given a time, returns whether it falls within the clips extrapolation
  565. /// </summary>
  566. /// <param name="sequenceTime">The time relative to the timeline</param>
  567. public bool IsExtrapolatedTime(double sequenceTime)
  568. {
  569. return IsPreExtrapolatedTime(sequenceTime) || IsPostExtrapolatedTime(sequenceTime);
  570. }
  571. /// <summary>
  572. /// Given a time, returns whether it falls within the clip pre-extrapolation
  573. /// </summary>
  574. /// <param name="sequenceTime">The time relative to the timeline</param>
  575. public bool IsPreExtrapolatedTime(double sequenceTime)
  576. {
  577. return preExtrapolationMode != ClipExtrapolation.None &&
  578. sequenceTime < m_Start && sequenceTime >= m_Start - m_PreExtrapolationTime;
  579. }
  580. /// <summary>
  581. /// Given a time, returns whether it falls within the clip post-extrapolation
  582. /// </summary>
  583. /// <param name="sequenceTime">The time relative to the timeline</param>
  584. public bool IsPostExtrapolatedTime(double sequenceTime)
  585. {
  586. return postExtrapolationMode != ClipExtrapolation.None &&
  587. (sequenceTime > end) && (sequenceTime - end < m_PostExtrapolationTime);
  588. }
  589. /// <summary>
  590. /// The start time of the clip, accounting for pre-extrapolation
  591. /// </summary>
  592. public double extrapolatedStart
  593. {
  594. get
  595. {
  596. if (m_PreExtrapolationMode != ClipExtrapolation.None)
  597. return m_Start - m_PreExtrapolationTime;
  598. return m_Start;
  599. }
  600. }
  601. /// <summary>
  602. /// The length of the clip in seconds, including extrapolation.
  603. /// </summary>
  604. public double extrapolatedDuration
  605. {
  606. get
  607. {
  608. double length = m_Duration;
  609. if (m_PostExtrapolationMode != ClipExtrapolation.None)
  610. length += Math.Min(m_PostExtrapolationTime, kMaxTimeValue);
  611. if (m_PreExtrapolationMode != ClipExtrapolation.None)
  612. length += m_PreExtrapolationTime;
  613. return length;
  614. }
  615. }
  616. static double GetExtrapolatedTime(double time, ClipExtrapolation mode, double duration)
  617. {
  618. if (duration == 0)
  619. return 0;
  620. switch (mode)
  621. {
  622. case ClipExtrapolation.None:
  623. break;
  624. case ClipExtrapolation.Loop:
  625. if (time < 0)
  626. time = duration - (-time % duration);
  627. else if (time > duration)
  628. time %= duration;
  629. break;
  630. case ClipExtrapolation.Hold:
  631. if (time < 0)
  632. return 0;
  633. if (time > duration)
  634. return duration;
  635. break;
  636. case ClipExtrapolation.PingPong:
  637. if (time < 0)
  638. {
  639. time = duration * 2 - (-time % (duration * 2));
  640. time = duration - Math.Abs(time - duration);
  641. }
  642. else
  643. {
  644. time = time % (duration * 2.0);
  645. time = duration - Math.Abs(time - duration);
  646. }
  647. break;
  648. case ClipExtrapolation.Continue:
  649. break;
  650. }
  651. return time;
  652. }
  653. /// <summary>
  654. /// Creates an AnimationClip to store animated properties for the attached PlayableAsset.
  655. /// </summary>
  656. /// <remarks>
  657. /// If curves already exists for this clip, this method produces no result regardless of the
  658. /// value specified for curvesClipName.
  659. /// </remarks>
  660. /// <remarks>
  661. /// When used from the editor, this method attempts to save the created curves clip to the TimelineAsset.
  662. /// The TimelineAsset must already exist in the AssetDatabase to save the curves clip. If the TimelineAsset
  663. /// does not exist, the curves clip is still created but it is not saved.
  664. /// </remarks>
  665. /// <param name="curvesClipName">
  666. /// The name of the AnimationClip to create.
  667. /// This method does not ensure unique names. If you want a unique clip name, you must provide one.
  668. /// See ObjectNames.GetUniqueName for information on a method that creates unique names.
  669. /// </param>
  670. public void CreateCurves(string curvesClipName)
  671. {
  672. if (m_AnimationCurves != null)
  673. return;
  674. m_AnimationCurves = TimelineCreateUtilities.CreateAnimationClipForTrack(string.IsNullOrEmpty(curvesClipName) ? kDefaultCurvesName : curvesClipName, parentTrack, true);
  675. }
  676. void ISerializationCallbackReceiver.OnBeforeSerialize()
  677. {
  678. m_Version = k_LatestVersion;
  679. }
  680. void ISerializationCallbackReceiver.OnAfterDeserialize()
  681. {
  682. if (m_Version < k_LatestVersion)
  683. {
  684. UpgradeToLatestVersion();
  685. }
  686. }
  687. /// <summary>
  688. /// Outputs a more readable representation of the timeline clip as a string
  689. /// </summary>
  690. /// <returns></returns>
  691. public override string ToString()
  692. {
  693. return UnityString.Format("{0} ({1:F2}, {2:F2}):{3:F2} | {4}", displayName, start, end, clipIn, parentTrack);
  694. }
  695. #if UNITY_EDITOR
  696. internal int DirtyIndex { get; private set; }
  697. internal void MarkDirty()
  698. {
  699. DirtyIndex++;
  700. }
  701. void UpdateDirty(double oldValue, double newValue)
  702. {
  703. if (oldValue != newValue)
  704. DirtyIndex++;
  705. }
  706. #else
  707. void UpdateDirty(double oldValue, double newValue) {}
  708. #endif
  709. };
  710. }