Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SequenceState.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. using UnityEngine.Timeline;
  7. namespace UnityEditor.Timeline
  8. {
  9. class SequenceState : ISequenceState
  10. {
  11. readonly WindowState m_WindowState;
  12. readonly SequenceState m_ParentSequence;
  13. double m_Time;
  14. Range? m_CachedEvaluableRange;
  15. public TimelineAsset asset { get; }
  16. public PlayableDirector director { get; }
  17. public TimelineClip hostClip { get; }
  18. public double start { get; }
  19. public double timeScale { get; }
  20. public bool isAssetOnly { get; set; }
  21. public double duration
  22. {
  23. get
  24. {
  25. if (asset == null)
  26. return 0.0;
  27. var assetDuration = asset.durationMode == TimelineAsset.DurationMode.FixedLength ? asset.fixedDuration : asset.duration;
  28. return hostClip == null ? assetDuration : Math.Min(hostClip.duration, assetDuration);
  29. }
  30. }
  31. [NonSerialized] List<UnityEngine.Object> m_CachedChildAssets;
  32. public List<UnityEngine.Object> cachedChildAssets
  33. {
  34. get
  35. {
  36. if (m_CachedChildAssets == null && asset != null)
  37. {
  38. m_CachedChildAssets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(asset)).ToList();
  39. }
  40. return m_CachedChildAssets;
  41. }
  42. }
  43. public void InvalidateChildAssetCache()
  44. {
  45. m_CachedChildAssets = null;
  46. }
  47. bool? m_IsReadOnly;
  48. public bool isReadOnly
  49. {
  50. get
  51. {
  52. if (!m_IsReadOnly.HasValue)
  53. m_IsReadOnly = FileUtility.IsReadOnly(asset);
  54. return m_IsReadOnly.Value;
  55. }
  56. }
  57. public void ResetIsReadOnly()
  58. {
  59. m_IsReadOnly = null;
  60. }
  61. public TimelineAssetViewModel viewModel
  62. {
  63. get
  64. {
  65. return TimelineWindowViewPrefs.GetOrCreateViewModel(asset);
  66. }
  67. }
  68. public double time
  69. {
  70. get
  71. {
  72. if (m_ParentSequence != null)
  73. return hostClip.ToLocalTimeUnbound(m_ParentSequence.time);
  74. return GetLocalTime();
  75. }
  76. set
  77. {
  78. var correctedValue = Math.Min(value, TimeUtility.k_MaxTimelineDurationInSeconds);
  79. viewModel.windowTime = correctedValue;
  80. if (m_ParentSequence != null)
  81. m_ParentSequence.time = hostClip.FromLocalTimeUnbound(correctedValue);
  82. else
  83. SetLocalTime(correctedValue);
  84. }
  85. }
  86. public int frame
  87. {
  88. get { return TimeUtility.ToFrames(time, frameRate); }
  89. set { time = TimeUtility.FromFrames(Mathf.Max(0, value), frameRate); }
  90. }
  91. public double frameRate
  92. {
  93. get
  94. {
  95. if (asset != null)
  96. return asset.editorSettings.frameRate;
  97. return TimelineAsset.EditorSettings.kDefaultFrameRate;
  98. }
  99. set
  100. {
  101. var settings = asset.editorSettings;
  102. if (Math.Abs(settings.frameRate - value) > TimeUtility.kFrameRateEpsilon)
  103. {
  104. settings.frameRate = Math.Max(value, TimeUtility.kFrameRateEpsilon);
  105. EditorUtility.SetDirty(asset);
  106. }
  107. }
  108. }
  109. public SequenceState(WindowState windowState, TimelineAsset asset, PlayableDirector director, TimelineClip hostClip, SequenceState parentSequence)
  110. {
  111. m_WindowState = windowState;
  112. m_ParentSequence = parentSequence;
  113. this.asset = asset;
  114. this.director = director;
  115. this.hostClip = hostClip;
  116. isAssetOnly = asset != null && director == null;
  117. start = hostClip == null ? 0.0 : hostClip.start;
  118. timeScale = hostClip == null ? 1.0 : hostClip.timeScale * parentSequence.timeScale;
  119. if (asset != null)
  120. {
  121. asset.AssetModifiedOnDisk += AssetOnAssetModifiedOnDisk;
  122. }
  123. }
  124. void AssetOnAssetModifiedOnDisk()
  125. {
  126. m_WindowState?.Refresh();
  127. }
  128. public Range GetEvaluableRange()
  129. {
  130. if (hostClip == null)
  131. return new Range
  132. {
  133. start = 0.0,
  134. end = duration
  135. };
  136. if (!m_CachedEvaluableRange.HasValue)
  137. {
  138. var globalRange = GetGlobalEvaluableRange();
  139. m_CachedEvaluableRange = new Range
  140. {
  141. start = ToLocalTime(globalRange.start),
  142. end = ToLocalTime(globalRange.end)
  143. };
  144. }
  145. return m_CachedEvaluableRange.Value;
  146. }
  147. public double ToGlobalTime(double t)
  148. {
  149. if (hostClip == null)
  150. return t;
  151. return m_ParentSequence.ToGlobalTime(hostClip.FromLocalTimeUnbound(t));
  152. }
  153. public double ToLocalTime(double t)
  154. {
  155. if (hostClip == null)
  156. return t;
  157. return hostClip.ToLocalTimeUnbound(m_ParentSequence.ToLocalTime(t));
  158. }
  159. double GetLocalTime()
  160. {
  161. if (!m_WindowState.previewMode && !Application.isPlaying)
  162. return viewModel.windowTime;
  163. // the time needs to always be synchronized with the director
  164. if (director != null)
  165. m_Time = director.time;
  166. return m_Time;
  167. }
  168. void SetLocalTime(double newTime)
  169. {
  170. // do this prior to the calback, because the callback pulls from the get
  171. if (director != null)
  172. director.time = newTime;
  173. if (Math.Abs(m_Time - newTime) > TimeUtility.kTimeEpsilon)
  174. {
  175. m_Time = newTime;
  176. m_WindowState.InvokeTimeChangeCallback();
  177. }
  178. }
  179. Range GetGlobalEvaluableRange()
  180. {
  181. if (hostClip == null)
  182. return new Range
  183. {
  184. start = 0.0,
  185. end = duration
  186. };
  187. var currentRange = new Range
  188. {
  189. start = hostClip.ToLocalTimeUnbound(ToGlobalTime(hostClip.start)),
  190. end = hostClip.ToLocalTimeUnbound(ToGlobalTime(hostClip.end))
  191. };
  192. return Range.Intersection(currentRange, m_ParentSequence.GetGlobalEvaluableRange());
  193. }
  194. public void Dispose()
  195. {
  196. if (asset != null)
  197. {
  198. asset.AssetModifiedOnDisk -= AssetOnAssetModifiedOnDisk;
  199. }
  200. TimelineWindowViewPrefs.SaveViewModel(asset);
  201. }
  202. }
  203. }