ClipEditor.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Playables;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. /// <summary>
  8. /// Description of the on-screen area where a clip is drawn
  9. /// </summary>
  10. public struct ClipBackgroundRegion
  11. {
  12. /// <summary>
  13. /// The rectangle where the background of the clip is drawn.
  14. /// </summary>
  15. /// <remarks>
  16. /// The rectangle is clipped to the screen. The rectangle does not include clip borders.
  17. /// </remarks>
  18. public Rect position { get; private set; }
  19. /// <summary>
  20. /// The start time of the region, relative to the clip.
  21. /// </summary>
  22. public double startTime { get; private set; }
  23. /// <summary>
  24. /// The end time of the region, relative to the clip.
  25. /// </summary>
  26. public double endTime { get; private set; }
  27. /// <summary>
  28. /// Constructor
  29. /// </summary>
  30. /// <param name="_position"></param>
  31. /// <param name="_startTime"></param>
  32. /// <param name="_endTime"></param>
  33. public ClipBackgroundRegion(Rect _position, double _startTime, double _endTime)
  34. {
  35. position = _position;
  36. startTime = _startTime;
  37. endTime = _endTime;
  38. }
  39. /// <summary>
  40. /// Indicates whether this instance and a specified object are equal.
  41. /// </summary>
  42. /// <param name="obj">The object to compare with the current instance.</param>
  43. /// <returns>Returns <c>true</c> if <paramref name="obj"/> and this instance are the same type and represent the same value.</returns>
  44. public override bool Equals(object obj)
  45. {
  46. if (!(obj is ClipBackgroundRegion))
  47. return false;
  48. return Equals((ClipBackgroundRegion)obj);
  49. }
  50. /// <summary>
  51. /// Compares this object with another <c>ClipBackgroundRegion</c>.
  52. /// </summary>
  53. /// <param name="other">The object to compare with.</param>
  54. /// <returns>Returns true if <c>this</c> and <paramref name="other"/> are equal.</returns>
  55. public bool Equals(ClipBackgroundRegion other)
  56. {
  57. return position.Equals(other.position) &&
  58. startTime == other.startTime &&
  59. endTime == other.endTime;
  60. }
  61. /// <summary>
  62. /// Returns the hash code for this instance.
  63. /// </summary>
  64. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  65. public override int GetHashCode()
  66. {
  67. return HashUtility.CombineHash(
  68. position.GetHashCode(),
  69. startTime.GetHashCode(),
  70. endTime.GetHashCode()
  71. );
  72. }
  73. /// <summary>
  74. /// Compares two <c>ClipBackgroundRegion</c> objects.
  75. /// </summary>
  76. /// <param name="region1">The first object.</param>
  77. /// <param name="region2">The second object.</param>
  78. /// <returns>Returns true if they are equal.</returns>
  79. public static bool operator==(ClipBackgroundRegion region1, ClipBackgroundRegion region2)
  80. {
  81. return region1.Equals(region2);
  82. }
  83. /// <summary>
  84. /// Compares two <c>ClipBackgroundRegion</c> objects.
  85. /// </summary>
  86. /// <param name="region1">The first object.</param>
  87. /// <param name="region2">The second object.</param>
  88. /// <returns>Returns true if they are not equal.</returns>
  89. public static bool operator!=(ClipBackgroundRegion region1, ClipBackgroundRegion region2)
  90. {
  91. return !region1.Equals(region2);
  92. }
  93. }
  94. /// <summary>
  95. /// The user-defined options for drawing a clip.
  96. /// </summary>
  97. public struct ClipDrawOptions
  98. {
  99. private IEnumerable<Texture2D> m_Icons;
  100. /// <summary>
  101. /// Text that indicates if the clip should display an error.
  102. /// </summary>
  103. /// <remarks>
  104. /// If the error text is not empty or null, then the clip displays a warning. The error text is used as the tooltip.
  105. /// </remarks>
  106. public string errorText { get; set; }
  107. /// <summary>
  108. /// The tooltip to show for the clip.
  109. /// </summary>
  110. public string tooltip { get; set; }
  111. /// <summary>
  112. /// The color drawn under the clip. By default, the color is the same as the track color.
  113. /// </summary>
  114. public Color highlightColor { get; set; }
  115. /// <summary>
  116. /// Icons to display on the clip.
  117. /// </summary>
  118. public IEnumerable<Texture2D> icons
  119. {
  120. get { return m_Icons ?? System.Linq.Enumerable.Empty<Texture2D>(); }
  121. set { m_Icons = value;}
  122. }
  123. /// <summary>
  124. /// Indicates whether this instance and a specified object are equal.
  125. /// </summary>
  126. /// <param name="obj">The object to compare with the current instance.</param>
  127. /// <returns>Returns <c>true</c> if <paramref name="obj"/> and this instance are the same type and represent the same value.</returns>
  128. public override bool Equals(object obj)
  129. {
  130. if (!(obj is ClipDrawOptions))
  131. return false;
  132. return Equals((ClipDrawOptions)obj);
  133. }
  134. /// <summary>
  135. /// Compares this object with another <c>ClipDrawOptions</c>.
  136. /// </summary>
  137. /// <param name="other">The object to compare with.</param>
  138. /// <returns>Returns true if <c>this</c> and <paramref name="other"/> are equal.</returns>
  139. public bool Equals(ClipDrawOptions other)
  140. {
  141. return errorText == other.errorText &&
  142. tooltip == other.tooltip &&
  143. highlightColor == other.highlightColor &&
  144. System.Linq.Enumerable.SequenceEqual(icons, other.icons);
  145. }
  146. /// <summary>
  147. /// Returns the hash code for this instance.
  148. /// </summary>
  149. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  150. public override int GetHashCode()
  151. {
  152. return HashUtility.CombineHash(
  153. errorText != null ? errorText.GetHashCode() : 0,
  154. tooltip != null ? tooltip.GetHashCode() : 0,
  155. highlightColor.GetHashCode(),
  156. icons != null ? icons.GetHashCode() : 0
  157. );
  158. }
  159. /// <summary>
  160. /// Compares two <c>ClipDrawOptions</c> objects.
  161. /// </summary>
  162. /// <param name="options1">The first object.</param>
  163. /// <param name="options2">The second object.</param>
  164. /// <returns>Returns true if they are equal.</returns>
  165. public static bool operator==(ClipDrawOptions options1, ClipDrawOptions options2)
  166. {
  167. return options1.Equals(options2);
  168. }
  169. /// <summary>
  170. /// Compares two <c>ClipDrawOptions</c> objects.
  171. /// </summary>
  172. /// <param name="options1">The first object.</param>
  173. /// <param name="options2">The second object.</param>
  174. /// <returns>Returns true if they are not equal.</returns>
  175. public static bool operator!=(ClipDrawOptions options1, ClipDrawOptions options2)
  176. {
  177. return !options1.Equals(options2);
  178. }
  179. }
  180. /// <summary>
  181. /// Use this class to customize clip types in the TimelineEditor.
  182. /// </summary>
  183. public class ClipEditor
  184. {
  185. static readonly string k_NoPlayableAssetError = L10n.Tr("This clip does not contain a valid playable asset");
  186. static readonly string k_ScriptLoadError = L10n.Tr("The associated script can not be loaded");
  187. internal readonly bool supportsSubTimelines;
  188. /// <summary>
  189. /// Default constructor
  190. /// </summary>
  191. public ClipEditor()
  192. {
  193. supportsSubTimelines = TypeUtility.HasOverrideMethod(GetType(), nameof(GetSubTimelines));
  194. }
  195. /// <summary>
  196. /// Implement this method to override the default options for drawing a clip.
  197. /// </summary>
  198. /// <param name="clip">The clip being drawn.</param>
  199. /// <returns>The options for drawing a clip.</returns>
  200. public virtual ClipDrawOptions GetClipOptions(TimelineClip clip)
  201. {
  202. return new ClipDrawOptions()
  203. {
  204. errorText = GetErrorText(clip),
  205. tooltip = string.Empty,
  206. highlightColor = GetDefaultHighlightColor(clip),
  207. icons = System.Linq.Enumerable.Empty<Texture2D>()
  208. };
  209. }
  210. /// <summary>
  211. /// Override this method to draw a background for a clip .
  212. /// </summary>
  213. /// <param name="clip">The clip being drawn.</param>
  214. /// <param name="region">The on-screen area where the clip is drawn.</param>
  215. public virtual void DrawBackground(TimelineClip clip, ClipBackgroundRegion region)
  216. {
  217. }
  218. /// <summary>
  219. /// Called when a clip is created.
  220. /// </summary>
  221. /// <param name="clip">The newly created clip.</param>
  222. /// <param name="track">The track that the clip is assigned to.</param>
  223. /// <param name="clonedFrom">The source that the clip was copied from. This can be set to null if the clip is not a copy.</param>
  224. /// <remarks>
  225. /// The callback occurs before the clip is assigned to the track.
  226. /// </remarks>
  227. public virtual void OnCreate(TimelineClip clip, TrackAsset track, TimelineClip clonedFrom)
  228. {
  229. }
  230. /// <summary>
  231. /// Gets the error text for the specified clip.
  232. /// </summary>
  233. /// <param name="clip">The clip being drawn.</param>
  234. /// <returns>Returns the error text to be displayed as the tool tip for the clip. If there is no error to be displayed, this method returns string.Empty.</returns>
  235. public string GetErrorText(TimelineClip clip)
  236. {
  237. if (clip == null || clip.asset == null)
  238. return k_NoPlayableAssetError;
  239. var playableAsset = clip.asset as ScriptableObject;
  240. if (playableAsset == null || MonoScript.FromScriptableObject(playableAsset) == null)
  241. return k_ScriptLoadError;
  242. return string.Empty;
  243. }
  244. /// <summary>
  245. /// The color drawn under the clip. By default, the color is the same as the track color.
  246. /// </summary>
  247. /// <param name="clip">The clip being drawn.</param>
  248. /// <returns>Returns the highlight color of the clip being drawn.</returns>
  249. public Color GetDefaultHighlightColor(TimelineClip clip)
  250. {
  251. if (clip == null)
  252. return Color.white;
  253. return TrackResourceCache.GetTrackColor(clip.parentTrack);
  254. }
  255. /// <summary>
  256. /// Called when a clip is changed by the Editor.
  257. /// </summary>
  258. /// <param name="clip">The clip that changed.</param>
  259. public virtual void OnClipChanged(TimelineClip clip)
  260. {
  261. }
  262. /// <summary>
  263. /// Gets the sub-timelines for a specific clip. Implement this method if your clip supports playing nested timelines.
  264. /// </summary>
  265. /// <param name="clip">The clip with the ControlPlayableAsset.</param>
  266. /// <param name="director">The playable director driving the Timeline Clip. This may not be the same as TimelineEditor.inspectedDirector.</param>
  267. /// <param name="subTimelines">Specify the sub-timelines to control.</param>
  268. public virtual void GetSubTimelines(TimelineClip clip, PlayableDirector director, List<PlayableDirector> subTimelines)
  269. {
  270. }
  271. }
  272. }