Nessuna descrizione
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.

ClipEditor.cs 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. private bool m_HideClipName;
  101. /// <summary>
  102. /// Text that indicates if the clip should display an error.
  103. /// </summary>
  104. /// <remarks>
  105. /// If the error text is not empty or null, then the clip displays a warning. The error text is used as the tooltip.
  106. /// </remarks>
  107. public string errorText { get; set; }
  108. /// <summary>
  109. /// Controls the display of the clip name.
  110. /// </summary>
  111. /// <remarks>
  112. /// Set to true to display the clip name. Set to false to avoid drawing the clip name.
  113. /// </remarks>
  114. public bool displayClipName
  115. {
  116. get { return !m_HideClipName; }
  117. set { m_HideClipName = !value; }
  118. }
  119. /// <summary>
  120. /// Controls the display of the clip scale indicator.
  121. /// </summary>
  122. /// <remarks>
  123. /// Set to true to hide the clip scale indicator.
  124. /// This is useful if the scale indicator is interfering with your custom clip rendering, or if the scale indicator
  125. /// is not useful for your clip.
  126. /// </remarks>
  127. public bool hideScaleIndicator { get; set; }
  128. /// <summary>
  129. /// The tooltip to show for the clip.
  130. /// </summary>
  131. public string tooltip { get; set; }
  132. /// <summary>
  133. /// The color drawn under the clip. By default, the color is the same as the track color.
  134. /// </summary>
  135. public Color highlightColor { get; set; }
  136. /// <summary>
  137. /// Icons to display on the clip.
  138. /// </summary>
  139. public IEnumerable<Texture2D> icons
  140. {
  141. get { return m_Icons ?? System.Linq.Enumerable.Empty<Texture2D>(); }
  142. set { m_Icons = value; }
  143. }
  144. /// <summary>
  145. /// Indicates whether this instance and a specified object are equal.
  146. /// </summary>
  147. /// <param name="obj">The object to compare with the current instance.</param>
  148. /// <returns>Returns <c>true</c> if <paramref name="obj"/> and this instance are the same type and represent the same value.</returns>
  149. public override bool Equals(object obj)
  150. {
  151. if (!(obj is ClipDrawOptions))
  152. return false;
  153. return Equals((ClipDrawOptions)obj);
  154. }
  155. /// <summary>
  156. /// Compares this object with another <c>ClipDrawOptions</c>.
  157. /// </summary>
  158. /// <param name="other">The object to compare with.</param>
  159. /// <returns>Returns true if <c>this</c> and <paramref name="other"/> are equal.</returns>
  160. public bool Equals(ClipDrawOptions other)
  161. {
  162. return errorText == other.errorText &&
  163. tooltip == other.tooltip &&
  164. highlightColor == other.highlightColor &&
  165. System.Linq.Enumerable.SequenceEqual(icons, other.icons);
  166. }
  167. /// <summary>
  168. /// Returns the hash code for this instance.
  169. /// </summary>
  170. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  171. public override int GetHashCode()
  172. {
  173. return HashUtility.CombineHash(
  174. errorText != null ? errorText.GetHashCode() : 0,
  175. tooltip != null ? tooltip.GetHashCode() : 0,
  176. highlightColor.GetHashCode(),
  177. icons != null ? icons.GetHashCode() : 0
  178. );
  179. }
  180. /// <summary>
  181. /// Compares two <c>ClipDrawOptions</c> objects.
  182. /// </summary>
  183. /// <param name="options1">The first object.</param>
  184. /// <param name="options2">The second object.</param>
  185. /// <returns>Returns true if they are equal.</returns>
  186. public static bool operator ==(ClipDrawOptions options1, ClipDrawOptions options2)
  187. {
  188. return options1.Equals(options2);
  189. }
  190. /// <summary>
  191. /// Compares two <c>ClipDrawOptions</c> objects.
  192. /// </summary>
  193. /// <param name="options1">The first object.</param>
  194. /// <param name="options2">The second object.</param>
  195. /// <returns>Returns true if they are not equal.</returns>
  196. public static bool operator !=(ClipDrawOptions options1, ClipDrawOptions options2)
  197. {
  198. return !options1.Equals(options2);
  199. }
  200. }
  201. /// <summary>
  202. /// Use this class to customize clip types in the TimelineEditor.
  203. /// </summary>
  204. public class ClipEditor
  205. {
  206. static readonly string k_NoPlayableAssetError = L10n.Tr("This clip does not contain a valid playable asset");
  207. static readonly string k_ScriptLoadError = L10n.Tr("The associated script can not be loaded");
  208. internal readonly bool supportsSubTimelines;
  209. /// <summary>
  210. /// Default constructor
  211. /// </summary>
  212. public ClipEditor()
  213. {
  214. supportsSubTimelines = TypeUtility.HasOverrideMethod(GetType(), nameof(GetSubTimelines));
  215. }
  216. /// <summary>
  217. /// Implement this method to override the default options for drawing a clip.
  218. /// </summary>
  219. /// <param name="clip">The clip being drawn.</param>
  220. /// <returns>The options for drawing a clip.</returns>
  221. public virtual ClipDrawOptions GetClipOptions(TimelineClip clip)
  222. {
  223. return new ClipDrawOptions()
  224. {
  225. errorText = GetErrorText(clip),
  226. tooltip = string.Empty,
  227. highlightColor = GetDefaultHighlightColor(clip),
  228. icons = System.Linq.Enumerable.Empty<Texture2D>()
  229. };
  230. }
  231. /// <summary>
  232. /// Override this method to draw a background for a clip .
  233. /// </summary>
  234. /// <param name="clip">The clip being drawn.</param>
  235. /// <param name="region">The on-screen area where the clip is drawn.</param>
  236. public virtual void DrawBackground(TimelineClip clip, ClipBackgroundRegion region)
  237. {
  238. }
  239. /// <summary>
  240. /// Called when a clip is created.
  241. /// </summary>
  242. /// <param name="clip">The newly created clip.</param>
  243. /// <param name="track">The track that the clip is assigned to.</param>
  244. /// <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>
  245. /// <remarks>
  246. /// The callback occurs before the clip is assigned to the track.
  247. /// </remarks>
  248. public virtual void OnCreate(TimelineClip clip, TrackAsset track, TimelineClip clonedFrom)
  249. {
  250. }
  251. /// <summary>
  252. /// Gets the error text for the specified clip.
  253. /// </summary>
  254. /// <param name="clip">The clip being drawn.</param>
  255. /// <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>
  256. public string GetErrorText(TimelineClip clip)
  257. {
  258. if (clip == null || clip.asset == null)
  259. return k_NoPlayableAssetError;
  260. var playableAsset = clip.asset as ScriptableObject;
  261. if (playableAsset == null || MonoScript.FromScriptableObject(playableAsset) == null)
  262. return k_ScriptLoadError;
  263. return string.Empty;
  264. }
  265. /// <summary>
  266. /// The color drawn under the clip. By default, the color is the same as the track color.
  267. /// </summary>
  268. /// <param name="clip">The clip being drawn.</param>
  269. /// <returns>Returns the highlight color of the clip being drawn.</returns>
  270. public Color GetDefaultHighlightColor(TimelineClip clip)
  271. {
  272. if (clip == null)
  273. return Color.white;
  274. return TrackResourceCache.GetTrackColor(clip.GetParentTrack());
  275. }
  276. /// <summary>
  277. /// Called when a clip is changed by the Editor.
  278. /// </summary>
  279. /// <param name="clip">The clip that changed.</param>
  280. public virtual void OnClipChanged(TimelineClip clip)
  281. {
  282. }
  283. /// <summary>
  284. /// Gets the sub-timelines for a specific clip. Implement this method if your clip supports playing nested timelines.
  285. /// </summary>
  286. /// <param name="clip">The clip with the ControlPlayableAsset.</param>
  287. /// <param name="director">The playable director driving the Timeline Clip. This may not be the same as TimelineEditor.inspectedDirector.</param>
  288. /// <param name="subTimelines">Specify the sub-timelines to control.</param>
  289. public virtual void GetSubTimelines(TimelineClip clip, PlayableDirector director, List<PlayableDirector> subTimelines)
  290. {
  291. }
  292. }
  293. }