Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimelineClip.cs 31KB

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