TimelineClip.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. /// <summary>
  249. /// underlyingAsset property is obsolete. Use asset property instead
  250. /// </summary>
  251. [Obsolete("underlyingAsset property is obsolete. Use asset property instead", true)]
  252. public Object underlyingAsset
  253. {
  254. get { return null; }
  255. set {}
  256. }
  257. /// <summary>
  258. /// Returns the TrackAsset to which this clip is attached.
  259. /// </summary>
  260. public TrackAsset parentTrack
  261. {
  262. get { return m_ParentTrack; }
  263. set
  264. {
  265. if (m_ParentTrack == value)
  266. return;
  267. if (m_ParentTrack != null)
  268. m_ParentTrack.RemoveClip(this);
  269. m_ParentTrack = value;
  270. if (m_ParentTrack != null)
  271. m_ParentTrack.AddClip(this);
  272. }
  273. }
  274. /// <summary>
  275. /// The ease in duration of the timeline clip in seconds. This only applies if the start of the clip is not overlapping.
  276. /// </summary>
  277. public double easeInDuration
  278. {
  279. get { return clipCaps.HasAny(ClipCaps.Blending) ? Math.Min(Math.Max(m_EaseInDuration, 0), duration) : 0; }
  280. set { m_EaseInDuration = clipCaps.HasAny(ClipCaps.Blending) ? Math.Max(0, Math.Min(SanitizeTimeValue(value, m_EaseInDuration), duration)) : 0; }
  281. }
  282. /// <summary>
  283. /// The ease out duration of the timeline clip in seconds. This only applies if the end of the clip is not overlapping.
  284. /// </summary>
  285. public double easeOutDuration
  286. {
  287. get { return clipCaps.HasAny(ClipCaps.Blending) ? Math.Min(Math.Max(m_EaseOutDuration, 0), duration) : 0; }
  288. set { m_EaseOutDuration = clipCaps.HasAny(ClipCaps.Blending) ? Math.Max(0, Math.Min(SanitizeTimeValue(value, m_EaseOutDuration), duration)) : 0; }
  289. }
  290. /// <summary>
  291. /// eastOutTime property is obsolete use easeOutTime property instead
  292. /// </summary>
  293. [Obsolete("Use easeOutTime instead (UnityUpgradable) -> easeOutTime", true)]
  294. public double eastOutTime
  295. {
  296. get { return duration - easeOutDuration + m_Start; }
  297. }
  298. /// <summary>
  299. /// The time in seconds that the ease out begins
  300. /// </summary>
  301. public double easeOutTime
  302. {
  303. get { return duration - easeOutDuration + m_Start; }
  304. }
  305. /// <summary>
  306. /// The amount of overlap in seconds on the start of a clip.
  307. /// </summary>
  308. public double blendInDuration
  309. {
  310. get { return clipCaps.HasAny(ClipCaps.Blending) ? m_BlendInDuration : 0; }
  311. set { m_BlendInDuration = clipCaps.HasAny(ClipCaps.Blending) ? SanitizeTimeValue(value, m_BlendInDuration) : 0; }
  312. }
  313. /// <summary>
  314. /// The amount of overlap in seconds at the end of a clip.
  315. /// </summary>
  316. public double blendOutDuration
  317. {
  318. get { return clipCaps.HasAny(ClipCaps.Blending) ? m_BlendOutDuration : 0; }
  319. set { m_BlendOutDuration = clipCaps.HasAny(ClipCaps.Blending) ? SanitizeTimeValue(value, m_BlendOutDuration) : 0; }
  320. }
  321. /// <summary>
  322. /// The mode for calculating the blend curve of the overlap at the start of the clip
  323. /// </summary>
  324. public BlendCurveMode blendInCurveMode
  325. {
  326. get { return m_BlendInCurveMode; }
  327. set { m_BlendInCurveMode = value; }
  328. }
  329. /// <summary>
  330. /// The mode for calculating the blend curve of the overlap at the end of the clip
  331. /// </summary>
  332. public BlendCurveMode blendOutCurveMode
  333. {
  334. get { return m_BlendOutCurveMode; }
  335. set { m_BlendOutCurveMode = value; }
  336. }
  337. /// <summary>
  338. /// Returns whether the clip is blending in
  339. /// </summary>
  340. public bool hasBlendIn { get { return clipCaps.HasAny(ClipCaps.Blending) && m_BlendInDuration > 0; } }
  341. /// <summary>
  342. /// Returns whether the clip is blending out
  343. /// </summary>
  344. public bool hasBlendOut { get { return clipCaps.HasAny(ClipCaps.Blending) && m_BlendOutDuration > 0; } }
  345. /// <summary>
  346. /// The animation curve used for calculating weights during an ease in or a blend in.
  347. /// </summary>
  348. public AnimationCurve mixInCurve
  349. {
  350. get
  351. {
  352. // auto fix broken curves
  353. if (m_MixInCurve == null || m_MixInCurve.length < 2)
  354. m_MixInCurve = GetDefaultMixInCurve();
  355. return m_MixInCurve;
  356. }
  357. set { m_MixInCurve = value; }
  358. }
  359. /// <summary>
  360. /// The amount of the clip being used for ease or blend in as a percentage
  361. /// </summary>
  362. public float mixInPercentage
  363. {
  364. get { return (float)(mixInDuration / duration); }
  365. }
  366. /// <summary>
  367. /// The amount of the clip blending or easing in, in seconds
  368. /// </summary>
  369. public double mixInDuration
  370. {
  371. get { return hasBlendIn ? blendInDuration : easeInDuration; }
  372. }
  373. /// <summary>
  374. /// The animation curve used for calculating weights during an ease out or a blend out.
  375. /// </summary>
  376. public AnimationCurve mixOutCurve
  377. {
  378. get
  379. {
  380. if (m_MixOutCurve == null || m_MixOutCurve.length < 2)
  381. m_MixOutCurve = GetDefaultMixOutCurve();
  382. return m_MixOutCurve;
  383. }
  384. set { m_MixOutCurve = value; }
  385. }
  386. /// <summary>
  387. /// The time in seconds that an ease out or blend out starts
  388. /// </summary>
  389. public double mixOutTime
  390. {
  391. get { return duration - mixOutDuration + m_Start; }
  392. }
  393. /// <summary>
  394. /// The amount of the clip blending or easing out, in seconds
  395. /// </summary>
  396. public double mixOutDuration
  397. {
  398. get { return hasBlendOut ? blendOutDuration : easeOutDuration; }
  399. }
  400. /// <summary>
  401. /// The amount of the clip being used for ease or blend out as a percentage
  402. /// </summary>
  403. public float mixOutPercentage
  404. {
  405. get { return (float)(mixOutDuration / duration); }
  406. }
  407. /// <summary>
  408. /// Returns whether this clip is recordable in editor
  409. /// </summary>
  410. public bool recordable
  411. {
  412. get { return m_Recordable; }
  413. internal set { m_Recordable = value; }
  414. }
  415. /// <summary>
  416. /// exposedParameter is deprecated and will be removed in a future release
  417. /// </summary>
  418. [Obsolete("exposedParameter is deprecated and will be removed in a future release", true)]
  419. public List<string> exposedParameters
  420. {
  421. get { return m_ExposedParameterNames ?? (m_ExposedParameterNames = new List<string>()); }
  422. }
  423. /// <summary>
  424. /// Returns the capabilities supported by this clip.
  425. /// </summary>
  426. public ClipCaps clipCaps
  427. {
  428. get
  429. {
  430. var clipAsset = asset as ITimelineClipAsset;
  431. return (clipAsset != null) ? clipAsset.clipCaps : kDefaultClipCaps;
  432. }
  433. }
  434. internal int Hash()
  435. {
  436. return HashUtility.CombineHash(m_Start.GetHashCode(),
  437. m_Duration.GetHashCode(),
  438. m_TimeScale.GetHashCode(),
  439. m_ClipIn.GetHashCode(),
  440. ((int)m_PreExtrapolationMode).GetHashCode(),
  441. ((int)m_PostExtrapolationMode).GetHashCode());
  442. }
  443. /// <summary>
  444. /// Given a time, returns the weight from the mix out
  445. /// </summary>
  446. /// <param name="time">Time (relative to the timeline)</param>
  447. /// <returns></returns>
  448. public float EvaluateMixOut(double time)
  449. {
  450. if (!clipCaps.HasAny(ClipCaps.Blending))
  451. return 1.0f;
  452. if (mixOutDuration > Mathf.Epsilon)
  453. {
  454. var perc = (float)(time - mixOutTime) / (float)mixOutDuration;
  455. perc = Mathf.Clamp01(mixOutCurve.Evaluate(perc));
  456. return perc;
  457. }
  458. return 1.0f;
  459. }
  460. /// <summary>
  461. /// Given a time, returns the weight from the mix in
  462. /// </summary>
  463. /// <param name="time">Time (relative to the timeline)</param>
  464. /// <returns></returns>
  465. public float EvaluateMixIn(double time)
  466. {
  467. if (!clipCaps.HasAny(ClipCaps.Blending))
  468. return 1.0f;
  469. if (mixInDuration > Mathf.Epsilon)
  470. {
  471. var perc = (float)(time - m_Start) / (float)mixInDuration;
  472. perc = Mathf.Clamp01(mixInCurve.Evaluate(perc));
  473. return perc;
  474. }
  475. return 1.0f;
  476. }
  477. static AnimationCurve GetDefaultMixInCurve()
  478. {
  479. return AnimationCurve.EaseInOut(0, 0, 1, 1);
  480. }
  481. static AnimationCurve GetDefaultMixOutCurve()
  482. {
  483. return AnimationCurve.EaseInOut(0, 1, 1, 0);
  484. }
  485. /// <summary>
  486. /// Converts from global time to a clips local time.
  487. /// </summary>
  488. /// <param name="time">time relative to the timeline</param>
  489. /// <returns>
  490. /// The local time with extrapolation applied
  491. /// </returns>
  492. public double ToLocalTime(double time)
  493. {
  494. if (time < 0)
  495. return time;
  496. // handle Extrapolation
  497. if (IsPreExtrapolatedTime(time))
  498. time = GetExtrapolatedTime(time - m_Start, m_PreExtrapolationMode, m_Duration);
  499. else if (IsPostExtrapolatedTime(time))
  500. time = GetExtrapolatedTime(time - m_Start, m_PostExtrapolationMode, m_Duration);
  501. else
  502. time -= m_Start;
  503. // handle looping and time scale within the clip
  504. time *= timeScale;
  505. time += clipIn;
  506. return time;
  507. }
  508. /// <summary>
  509. /// Converts from global time to local time of the clip
  510. /// </summary>
  511. /// <param name="time">The time relative to the timeline</param>
  512. /// <returns>The local time, ignoring any extrapolation or bounds</returns>
  513. public double ToLocalTimeUnbound(double time)
  514. {
  515. return (time - m_Start) * timeScale + clipIn;
  516. }
  517. /// <summary>
  518. /// Converts from local time of the clip to global time
  519. /// </summary>
  520. /// <param name="time">Time relative to the clip</param>
  521. /// <returns>The time relative to the timeline</returns>
  522. internal double FromLocalTimeUnbound(double time)
  523. {
  524. return (time - clipIn) / timeScale + m_Start;
  525. }
  526. /// <summary>
  527. /// If this contains an animation asset, returns the animation clip attached. Otherwise returns null.
  528. /// </summary>
  529. public AnimationClip animationClip
  530. {
  531. get
  532. {
  533. if (m_Asset == null)
  534. return null;
  535. var playableAsset = m_Asset as AnimationPlayableAsset;
  536. return playableAsset != null ? playableAsset.clip : null;
  537. }
  538. }
  539. static double SanitizeTimeValue(double value, double defaultValue)
  540. {
  541. if (double.IsInfinity(value) || double.IsNaN(value))
  542. {
  543. Debug.LogError("Invalid time value assigned");
  544. return defaultValue;
  545. }
  546. return Math.Max(-kMaxTimeValue, Math.Min(kMaxTimeValue, value));
  547. }
  548. /// <summary>
  549. /// Returns whether the clip is being extrapolated past the end time.
  550. /// </summary>
  551. public ClipExtrapolation postExtrapolationMode
  552. {
  553. get { return clipCaps.HasAny(ClipCaps.Extrapolation) ? m_PostExtrapolationMode : ClipExtrapolation.None; }
  554. internal set { m_PostExtrapolationMode = clipCaps.HasAny(ClipCaps.Extrapolation) ? value : ClipExtrapolation.None; }
  555. }
  556. /// <summary>
  557. /// Returns whether the clip is being extrapolated before the start time.
  558. /// </summary>
  559. public ClipExtrapolation preExtrapolationMode
  560. {
  561. get { return clipCaps.HasAny(ClipCaps.Extrapolation) ? m_PreExtrapolationMode : ClipExtrapolation.None; }
  562. internal set { m_PreExtrapolationMode = clipCaps.HasAny(ClipCaps.Extrapolation) ? value : ClipExtrapolation.None; }
  563. }
  564. internal void SetPostExtrapolationTime(double time)
  565. {
  566. m_PostExtrapolationTime = time;
  567. }
  568. internal void SetPreExtrapolationTime(double time)
  569. {
  570. m_PreExtrapolationTime = time;
  571. }
  572. /// <summary>
  573. /// Given a time, returns whether it falls within the clip's extrapolation
  574. /// </summary>
  575. /// <param name="sequenceTime">The time relative to the timeline</param>
  576. /// <returns>True if <paramref name="sequenceTime"/> is within the clip extrapolation</returns>
  577. public bool IsExtrapolatedTime(double sequenceTime)
  578. {
  579. return IsPreExtrapolatedTime(sequenceTime) || IsPostExtrapolatedTime(sequenceTime);
  580. }
  581. /// <summary>
  582. /// Given a time, returns whether it falls within the clip's pre-extrapolation
  583. /// </summary>
  584. /// <param name="sequenceTime">The time relative to the timeline</param>
  585. /// <returns>True if <paramref name="sequenceTime"/> is within the clip pre-extrapolation</returns>
  586. public bool IsPreExtrapolatedTime(double sequenceTime)
  587. {
  588. return preExtrapolationMode != ClipExtrapolation.None &&
  589. sequenceTime < m_Start && sequenceTime >= m_Start - m_PreExtrapolationTime;
  590. }
  591. /// <summary>
  592. /// Given a time, returns whether it falls within the clip's post-extrapolation
  593. /// </summary>
  594. /// <param name="sequenceTime">The time relative to the timeline</param>
  595. /// <returns>True if <paramref name="sequenceTime"/> is within the clip post-extrapolation</returns>
  596. public bool IsPostExtrapolatedTime(double sequenceTime)
  597. {
  598. return postExtrapolationMode != ClipExtrapolation.None &&
  599. (sequenceTime > end) && (sequenceTime - end < m_PostExtrapolationTime);
  600. }
  601. /// <summary>
  602. /// The start time of the clip, accounting for pre-extrapolation
  603. /// </summary>
  604. public double extrapolatedStart
  605. {
  606. get
  607. {
  608. if (m_PreExtrapolationMode != ClipExtrapolation.None)
  609. return m_Start - m_PreExtrapolationTime;
  610. return m_Start;
  611. }
  612. }
  613. /// <summary>
  614. /// The length of the clip in seconds, including extrapolation.
  615. /// </summary>
  616. public double extrapolatedDuration
  617. {
  618. get
  619. {
  620. double length = m_Duration;
  621. if (m_PostExtrapolationMode != ClipExtrapolation.None)
  622. length += Math.Min(m_PostExtrapolationTime, kMaxTimeValue);
  623. if (m_PreExtrapolationMode != ClipExtrapolation.None)
  624. length += m_PreExtrapolationTime;
  625. return length;
  626. }
  627. }
  628. static double GetExtrapolatedTime(double time, ClipExtrapolation mode, double duration)
  629. {
  630. if (duration == 0)
  631. return 0;
  632. switch (mode)
  633. {
  634. case ClipExtrapolation.None:
  635. break;
  636. case ClipExtrapolation.Loop:
  637. if (time < 0)
  638. time = duration - (-time % duration);
  639. else if (time > duration)
  640. time %= duration;
  641. break;
  642. case ClipExtrapolation.Hold:
  643. if (time < 0)
  644. return 0;
  645. if (time > duration)
  646. return duration;
  647. break;
  648. case ClipExtrapolation.PingPong:
  649. if (time < 0)
  650. {
  651. time = duration * 2 - (-time % (duration * 2));
  652. time = duration - Math.Abs(time - duration);
  653. }
  654. else
  655. {
  656. time = time % (duration * 2.0);
  657. time = duration - Math.Abs(time - duration);
  658. }
  659. break;
  660. case ClipExtrapolation.Continue:
  661. break;
  662. }
  663. return time;
  664. }
  665. /// <summary>
  666. /// Creates an AnimationClip to store animated properties for the attached PlayableAsset.
  667. /// </summary>
  668. /// <remarks>
  669. /// If curves already exists for this clip, this method produces no result regardless of the
  670. /// value specified for curvesClipName.
  671. /// </remarks>
  672. /// <remarks>
  673. /// When used from the editor, this method attempts to save the created curves clip to the TimelineAsset.
  674. /// The TimelineAsset must already exist in the AssetDatabase to save the curves clip. If the TimelineAsset
  675. /// does not exist, the curves clip is still created but it is not saved.
  676. /// </remarks>
  677. /// <param name="curvesClipName">
  678. /// The name of the AnimationClip to create.
  679. /// This method does not ensure unique names. If you want a unique clip name, you must provide one.
  680. /// See ObjectNames.GetUniqueName for information on a method that creates unique names.
  681. /// </param>
  682. public void CreateCurves(string curvesClipName)
  683. {
  684. if (m_AnimationCurves != null)
  685. return;
  686. m_AnimationCurves = TimelineCreateUtilities.CreateAnimationClipForTrack(string.IsNullOrEmpty(curvesClipName) ? kDefaultCurvesName : curvesClipName, parentTrack, true);
  687. }
  688. /// <summary>
  689. /// Called before Unity serializes this object.
  690. /// </summary>
  691. void ISerializationCallbackReceiver.OnBeforeSerialize()
  692. {
  693. m_Version = k_LatestVersion;
  694. }
  695. /// <summary>
  696. /// Called after Unity deserializes this object.
  697. /// </summary>
  698. void ISerializationCallbackReceiver.OnAfterDeserialize()
  699. {
  700. if (m_Version < k_LatestVersion)
  701. {
  702. UpgradeToLatestVersion();
  703. }
  704. }
  705. /// <summary>
  706. /// Outputs a more readable representation of the timeline clip as a string
  707. /// </summary>
  708. /// <returns></returns>
  709. public override string ToString()
  710. {
  711. return UnityString.Format("{0} ({1:F2}, {2:F2}):{3:F2} | {4}", displayName, start, end, clipIn, parentTrack);
  712. }
  713. /// <summary>
  714. /// Use this method to adjust ease in and ease out values to avoid overlapping.
  715. /// </summary>
  716. /// <remarks>
  717. /// Ease values will be adjusted to respect the ratio between ease in and ease out.
  718. /// </remarks>
  719. public void ConformEaseValues()
  720. {
  721. if (m_EaseInDuration + m_EaseOutDuration > duration)
  722. {
  723. var ratio = CalculateEasingRatio(m_EaseInDuration, m_EaseOutDuration);
  724. m_EaseInDuration = duration * ratio;
  725. m_EaseOutDuration = duration * (1.0 - ratio);
  726. }
  727. }
  728. static double CalculateEasingRatio(double easeIn, double easeOut)
  729. {
  730. if (Math.Abs(easeIn - easeOut) < TimeUtility.kTimeEpsilon)
  731. return 0.5;
  732. if (easeIn == 0.0)
  733. return 0.0;
  734. if (easeOut == 0.0)
  735. return 1.0;
  736. return easeIn / (easeIn + easeOut);
  737. }
  738. #if UNITY_EDITOR
  739. internal int DirtyIndex { get; private set; }
  740. internal void MarkDirty()
  741. {
  742. DirtyIndex++;
  743. }
  744. void UpdateDirty(double oldValue, double newValue)
  745. {
  746. if (oldValue != newValue)
  747. DirtyIndex++;
  748. }
  749. #else
  750. void UpdateDirty(double oldValue, double newValue) {}
  751. #endif
  752. };
  753. }