Brak opisu
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.

TrackEditor.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. namespace UnityEditor.Timeline
  7. {
  8. /// <summary>
  9. /// The user-defined options for drawing a track."
  10. /// </summary>
  11. public struct TrackDrawOptions
  12. {
  13. /// <summary>
  14. /// Text that indicates if the track should display an error.
  15. /// </summary>
  16. /// <remarks>
  17. /// If the error text is not empty or null, then the track displays a warning. The error text is used as the tooltip.
  18. /// </remarks>
  19. public string errorText { get; set; }
  20. /// <summary>
  21. /// The highlight color of the track.
  22. /// </summary>
  23. public Color trackColor { get; set; }
  24. /// <summary>
  25. /// The minimum height of the track.
  26. /// </summary>
  27. public float minimumHeight { get; set; }
  28. /// <summary>
  29. /// The icon displayed on the track header.
  30. /// </summary>
  31. /// <remarks>
  32. /// If this value is null, then the default icon for the track is used.
  33. /// </remarks>
  34. public Texture2D icon { get; set; }
  35. /// <summary>
  36. /// Indicates whether this instance and a specified object are equal.
  37. /// </summary>
  38. /// <param name="obj">The object to compare with the current instance.</param>
  39. /// <returns>Returns <c>true</c> if <paramref name="obj"/> and this instance are the same type and represent the same value.</returns>
  40. public override bool Equals(object obj)
  41. {
  42. if (!(obj is TrackDrawOptions))
  43. return false;
  44. return Equals((TrackDrawOptions)obj);
  45. }
  46. /// <summary>
  47. /// Compares this object with another <c>TrackDrawOptions</c>.
  48. /// </summary>
  49. /// <param name="other">The object to compare with.</param>
  50. /// <returns>Returns true if <c>this</c> and <paramref name="other"/> are equal.</returns>
  51. public bool Equals(TrackDrawOptions other)
  52. {
  53. return errorText == other.errorText &&
  54. trackColor == other.trackColor &&
  55. minimumHeight == other.minimumHeight &&
  56. icon == other.icon;
  57. }
  58. /// <summary>
  59. /// Returns the hash code for this instance.
  60. /// </summary>
  61. /// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
  62. public override int GetHashCode()
  63. {
  64. return HashUtility.CombineHash(
  65. errorText != null ? errorText.GetHashCode() : 0,
  66. trackColor.GetHashCode(),
  67. minimumHeight.GetHashCode(),
  68. icon != null ? icon.GetHashCode() : 0
  69. );
  70. }
  71. /// <summary>
  72. /// Compares two <c>TrackDrawOptions</c> objects.
  73. /// </summary>
  74. /// <param name="options1">The first object.</param>
  75. /// <param name="options2">The second object.</param>
  76. /// <returns>Returns true if they are equal.</returns>
  77. public static bool operator ==(TrackDrawOptions options1, TrackDrawOptions options2)
  78. {
  79. return options1.Equals(options2);
  80. }
  81. /// <summary>
  82. /// Compares two <c>TrackDrawOptions</c> objects.
  83. /// </summary>
  84. /// <param name="options1">The first object.</param>
  85. /// <param name="options2">The second object.</param>
  86. /// <returns>Returns true if they are not equal.</returns>
  87. public static bool operator !=(TrackDrawOptions options1, TrackDrawOptions options2)
  88. {
  89. return !options1.Equals(options2);
  90. }
  91. }
  92. /// <summary>
  93. /// The errors displayed for the track binding.
  94. /// </summary>
  95. public enum TrackBindingErrors
  96. {
  97. /// <summary>
  98. /// Select no errors.
  99. /// </summary>
  100. None = 0,
  101. /// <summary>
  102. /// The bound GameObject is disabled.
  103. /// </summary>
  104. BoundGameObjectDisabled = 1 << 0,
  105. /// <summary>
  106. /// The bound GameObject does not have a valid component.
  107. /// </summary>
  108. NoValidComponent = 1 << 1,
  109. /// <summary>
  110. /// The bound Object is a disabled Behaviour.
  111. /// </summary>
  112. BehaviourIsDisabled = 1 << 2,
  113. /// <summary>
  114. /// The bound Object is not of the correct type.
  115. /// </summary>
  116. InvalidBinding = 1 << 3,
  117. /// <summary>
  118. /// The bound Object is part of a prefab, and not an instance.
  119. /// </summary>
  120. PrefabBound = 1 << 4,
  121. /// <summary>
  122. /// Select all errors.
  123. /// </summary>
  124. All = Int32.MaxValue
  125. }
  126. /// <summary>
  127. /// Use this class to customize track types in the TimelineEditor.
  128. /// </summary>
  129. public class TrackEditor
  130. {
  131. static readonly string k_BoundGameObjectDisabled = L10n.Tr("The bound GameObject is disabled.");
  132. static readonly string k_NoValidComponent = L10n.Tr("Could not find appropriate component on this gameObject");
  133. static readonly string k_RequiredComponentIsDisabled = L10n.Tr("The component is disabled");
  134. static readonly string k_InvalidBinding = L10n.Tr("The bound object is not the correct type.");
  135. static readonly string k_PrefabBound = L10n.Tr("The bound object is a Prefab");
  136. readonly Dictionary<TrackAsset, System.Type> m_BindingCache = new Dictionary<TrackAsset, System.Type>();
  137. /// <summary>
  138. /// The default height of a track.
  139. /// </summary>
  140. public static readonly float DefaultTrackHeight = 30.0f;
  141. /// <summary>
  142. /// The minimum unscaled height of a track.
  143. /// </summary>
  144. public static readonly float MinimumTrackHeight = 10.0f;
  145. /// <summary>
  146. /// The maximum height of a track.
  147. /// </summary>
  148. public static readonly float MaximumTrackHeight = 256.0f;
  149. /// <summary>
  150. /// Implement this method to override the default options for drawing a track.
  151. /// </summary>
  152. /// <param name="track">The track from which track options are retrieved.</param>
  153. /// <param name="binding">The binding for the track.</param>
  154. /// <returns>The options for drawing the track.</returns>
  155. public virtual TrackDrawOptions GetTrackOptions(TrackAsset track, UnityEngine.Object binding)
  156. {
  157. return new TrackDrawOptions()
  158. {
  159. errorText = GetErrorText(track, binding, TrackBindingErrors.All),
  160. minimumHeight = DefaultTrackHeight,
  161. trackColor = GetTrackColor(track),
  162. icon = null
  163. };
  164. }
  165. /// <summary>
  166. /// Gets the error text for the specified track.
  167. /// </summary>
  168. /// <param name="track">The track to retrieve options for.</param>
  169. /// <param name="boundObject">The binding for the track.</param>
  170. /// <param name="detectErrors">The errors to check for.</param>
  171. /// <returns>An error to be displayed on the track, or string.Empty if there is no error.</returns>
  172. public string GetErrorText(TrackAsset track, UnityEngine.Object boundObject, TrackBindingErrors detectErrors)
  173. {
  174. if (track == null || boundObject == null)
  175. return string.Empty;
  176. var bindingType = GetBindingType(track);
  177. if (bindingType != null)
  178. {
  179. // bound to a prefab asset
  180. if (HasFlag(detectErrors, TrackBindingErrors.PrefabBound) && PrefabUtility.IsPartOfPrefabAsset(boundObject))
  181. {
  182. return k_PrefabBound;
  183. }
  184. // If we are a component, allow for bound game objects (legacy)
  185. if (typeof(Component).IsAssignableFrom(bindingType))
  186. {
  187. var gameObject = boundObject as GameObject;
  188. var component = boundObject as Component;
  189. if (component != null)
  190. gameObject = component.gameObject;
  191. // game object is bound with no component
  192. if (HasFlag(detectErrors, TrackBindingErrors.NoValidComponent) && gameObject != null && component == null)
  193. {
  194. component = gameObject.GetComponent(bindingType);
  195. if (component == null)
  196. {
  197. return k_NoValidComponent;
  198. }
  199. }
  200. // attached gameObject is disables (ignores Activation Track)
  201. if (HasFlag(detectErrors, TrackBindingErrors.BoundGameObjectDisabled) && gameObject != null && !gameObject.activeInHierarchy)
  202. {
  203. return k_BoundGameObjectDisabled;
  204. }
  205. // component is disabled
  206. var behaviour = component as Behaviour;
  207. if (HasFlag(detectErrors, TrackBindingErrors.BehaviourIsDisabled) && behaviour != null && !behaviour.enabled)
  208. {
  209. return k_RequiredComponentIsDisabled;
  210. }
  211. // mismatched binding
  212. if (HasFlag(detectErrors, TrackBindingErrors.InvalidBinding) && component != null && !bindingType.IsAssignableFrom(component.GetType()))
  213. {
  214. return k_InvalidBinding;
  215. }
  216. }
  217. // Mismatched binding (non-component)
  218. else if (HasFlag(detectErrors, TrackBindingErrors.InvalidBinding) && !bindingType.IsAssignableFrom(boundObject.GetType()))
  219. {
  220. return k_InvalidBinding;
  221. }
  222. }
  223. return string.Empty;
  224. }
  225. /// <summary>
  226. /// Gets the color information of a track.
  227. /// </summary>
  228. /// <param name="track"></param>
  229. /// <returns>Returns the color for the specified track.</returns>
  230. public Color GetTrackColor(TrackAsset track)
  231. {
  232. return TrackResourceCache.GetTrackColor(track);
  233. }
  234. /// <summary>
  235. /// Gets the binding type for a track.
  236. /// </summary>
  237. /// <param name="track">The track to retrieve the binding type from.</param>
  238. /// <returns>Returns the binding type for the specified track. Returns null if the track does not have binding.</returns>
  239. public System.Type GetBindingType(TrackAsset track)
  240. {
  241. if (track == null)
  242. return null;
  243. if (m_BindingCache.TryGetValue(track, out var result))
  244. return result;
  245. result = track.outputs.Select(x => x.outputTargetType).FirstOrDefault();
  246. m_BindingCache[track] = result;
  247. return result;
  248. }
  249. /// <summary>
  250. /// Callback for when a track is created.
  251. /// </summary>
  252. /// <param name="track">The track that is created.</param>
  253. /// <param name="copiedFrom">The source that the track is copied from. This can be set to null if the track is not a copy.</param>
  254. public virtual void OnCreate(TrackAsset track, TrackAsset copiedFrom)
  255. {
  256. }
  257. /// <summary>
  258. /// Callback for when a track is changed.
  259. /// </summary>
  260. /// <param name="track">The track that is changed.</param>
  261. public virtual void OnTrackChanged(TrackAsset track)
  262. {
  263. }
  264. static bool HasFlag(TrackBindingErrors errors, TrackBindingErrors flag)
  265. {
  266. return (errors & flag) != 0;
  267. }
  268. /// <summary>
  269. /// Override this method to validate if a binding for <paramref name="track"/>
  270. /// can be determined from <paramref name="candidate"/>.
  271. ///
  272. /// The default implementation of this method will return true if
  273. /// - <paramref name="candidate"/> is not null or,
  274. /// - <paramref name="candidate"/> is not part of a Prefab Asset or,
  275. /// - <paramref name="candidate"/> is a Component that can be bound to <paramref name="track"/>
  276. /// </summary>
  277. /// <param name="candidate"></param>
  278. /// <param name="track">TBD</param>
  279. /// <returns>True if a binding can be determined from <paramref name="candidate"/>.</returns>
  280. /// <seealso cref="UnityEngine.Timeline.TrackBindingTypeAttribute"/>
  281. /// <seealso cref="UnityEngine.Timeline.TrackAsset"/>
  282. public virtual bool IsBindingAssignableFrom(UnityEngine.Object candidate, TrackAsset track)
  283. {
  284. var action = BindingUtility.GetBindingAction(GetBindingType(track), candidate);
  285. return action != BindingUtility.BindingAction.DoNotBind;
  286. }
  287. /// <summary>
  288. /// Override this method to determine which object to bind to <paramref name="track"/>.
  289. /// A binding object should be determined from <paramref name="candidate"/>.
  290. ///
  291. /// By default, the `TrackBindingType` attribute from <paramref name="track"/> will be used to determine the binding.
  292. /// </summary>
  293. /// <param name="candidate">The source object from which a track binding should be determined.</param>
  294. /// <param name="track">The track to bind an object to.</param>
  295. /// <returns>The object to bind to <paramref name="track"/>.</returns>
  296. /// <seealso cref="UnityEngine.Timeline.TrackBindingTypeAttribute"/>
  297. /// <seealso cref="UnityEngine.Timeline.TrackAsset"/>
  298. public virtual UnityEngine.Object GetBindingFrom(UnityEngine.Object candidate, TrackAsset track)
  299. {
  300. Type bindingType = GetBindingType(track);
  301. BindingUtility.BindingAction action = BindingUtility.GetBindingAction(bindingType, candidate);
  302. return BindingUtility.GetBinding(action, candidate, bindingType);
  303. }
  304. }
  305. static class TrackEditorExtension
  306. {
  307. public static bool SupportsBindingAssign(this TrackEditor editor)
  308. {
  309. return TypeUtility.HasOverrideMethod(editor.GetType(), nameof(TrackEditor.GetBindingFrom));
  310. }
  311. public static void OnCreate_Safe(this TrackEditor editor, TrackAsset track, TrackAsset copiedFrom)
  312. {
  313. try
  314. {
  315. editor.OnCreate(track, copiedFrom);
  316. }
  317. catch (Exception e)
  318. {
  319. Debug.LogException(e);
  320. }
  321. }
  322. public static TrackDrawOptions GetTrackOptions_Safe(this TrackEditor editor, TrackAsset track, UnityEngine.Object binding)
  323. {
  324. try
  325. {
  326. return editor.GetTrackOptions(track, binding);
  327. }
  328. catch (Exception e)
  329. {
  330. Debug.LogException(e);
  331. return CustomTimelineEditorCache.GetDefaultTrackEditor().GetTrackOptions(track, binding);
  332. }
  333. }
  334. public static UnityEngine.Object GetBindingFrom_Safe(this TrackEditor editor, UnityEngine.Object candidate, TrackAsset track)
  335. {
  336. try
  337. {
  338. return editor.GetBindingFrom(candidate, track);
  339. }
  340. catch (Exception e)
  341. {
  342. Debug.LogException(e);
  343. return candidate;
  344. }
  345. }
  346. public static bool IsBindingAssignableFrom_Safe(this TrackEditor editor, UnityEngine.Object candidate, TrackAsset track)
  347. {
  348. try
  349. {
  350. return editor.IsBindingAssignableFrom(candidate, track);
  351. }
  352. catch (Exception e)
  353. {
  354. Debug.LogException(e);
  355. return false;
  356. }
  357. }
  358. public static void OnTrackChanged_Safe(this TrackEditor editor, TrackAsset track)
  359. {
  360. try
  361. {
  362. editor.OnTrackChanged(track);
  363. }
  364. catch (Exception e)
  365. {
  366. Debug.LogException(e);
  367. }
  368. }
  369. }
  370. }