123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Playables;
- using UnityEngine.Timeline;
- namespace UnityEditor.Timeline
- {
- /// <summary>
- /// The user-defined options for drawing a track."
- /// </summary>
- public struct TrackDrawOptions
- {
- /// <summary>
- /// Text that indicates if the track should display an error.
- /// </summary>
- /// <remarks>
- /// If the error text is not empty or null, then the track displays a warning. The error text is used as the tooltip.
- /// </remarks>
- public string errorText { get; set; }
- /// <summary>
- /// The highlight color of the track.
- /// </summary>
- public Color trackColor { get; set; }
- /// <summary>
- /// The minimum height of the track.
- /// </summary>
- public float minimumHeight { get; set; }
- /// <summary>
- /// The icon displayed on the track header.
- /// </summary>
- /// <remarks>
- /// If this value is null, then the default icon for the track is used.
- /// </remarks>
- public Texture2D icon { get; set; }
- public override bool Equals(object obj)
- {
- if (!(obj is TrackDrawOptions))
- return false;
- return Equals((TrackDrawOptions)obj);
- }
- public bool Equals(TrackDrawOptions other)
- {
- return errorText == other.errorText &&
- trackColor == other.trackColor &&
- minimumHeight == other.minimumHeight &&
- icon == other.icon;
- }
- public override int GetHashCode()
- {
- return HashUtility.CombineHash(
- errorText != null ? errorText.GetHashCode() : 0,
- trackColor.GetHashCode(),
- minimumHeight.GetHashCode(),
- icon != null ? icon.GetHashCode() : 0
- );
- }
- public static bool operator==(TrackDrawOptions options1, TrackDrawOptions options2)
- {
- return options1.Equals(options2);
- }
- public static bool operator!=(TrackDrawOptions options1, TrackDrawOptions options2)
- {
- return !options1.Equals(options2);
- }
- }
- /// <summary>
- /// The errors displayed for the track binding.
- /// </summary>
- public enum TrackBindingErrors
- {
- /// <summary>
- /// Select no errors.
- /// </summary>
- None = 0,
- /// <summary>
- /// The bound GameObject is disabled.
- /// </summary>
- BoundGameObjectDisabled = 1 << 0,
- /// <summary>
- /// The bound GameObject does not have a valid component.
- /// </summary>
- NoValidComponent = 1 << 1,
- /// <summary>
- /// The bound Object is a disabled Behaviour.
- /// </summary>
- BehaviourIsDisabled = 1 << 2,
- /// <summary>
- /// The bound Object is not of the correct type.
- /// </summary>
- InvalidBinding = 1 << 3,
- /// <summary>
- /// The bound Object is part of a prefab, and not an instance.
- /// </summary>
- PrefabBound = 1 << 4,
- /// <summary>
- /// Select all errors.
- /// </summary>
- All = Int32.MaxValue
- }
- /// <summary>
- /// Use this class to customize track types in the TimelineEditor.
- /// </summary>
- public class TrackEditor
- {
- static readonly string k_BoundGameObjectDisabled = LocalizationDatabase.GetLocalizedString("The bound GameObject is disabled.");
- static readonly string k_NoValidComponent = LocalizationDatabase.GetLocalizedString("Could not find appropriate component on this gameObject");
- static readonly string k_RequiredComponentIsDisabled = LocalizationDatabase.GetLocalizedString("The component is disabled");
- static readonly string k_InvalidBinding = LocalizationDatabase.GetLocalizedString("The bound object is not the correct type.");
- static readonly string k_PrefabBound = LocalizationDatabase.GetLocalizedString("The bound object is a Prefab");
- readonly Dictionary<TrackAsset, System.Type> m_BindingCache = new Dictionary<TrackAsset, System.Type>();
- /// <summary>
- /// The default height of a track.
- /// </summary>
- public static readonly float DefaultTrackHeight = 30.0f;
- /// <summary>
- /// The minimum unscaled height of a track.
- /// </summary>
- public static readonly float MinimumTrackHeight = 10.0f;
- /// <summary>
- /// The maximum height of a track.
- /// </summary>
- public static readonly float MaximumTrackHeight = 256.0f;
- /// <summary>
- /// Implement this method to override the default options for drawing a track.
- /// </summary>
- /// <param name="track">The track from which track options are retrieved.</param>
- /// <param name="binding">The binding for the track.</param>
- /// <returns>The options for drawing the track.</returns>
- public virtual TrackDrawOptions GetTrackOptions(TrackAsset track, UnityEngine.Object binding)
- {
- return new TrackDrawOptions()
- {
- errorText = GetErrorText(track, binding, TrackBindingErrors.All),
- minimumHeight = DefaultTrackHeight,
- trackColor = GetTrackColor(track),
- icon = null
- };
- }
- /// <summary>
- /// Gets the error text for the specified track.
- /// </summary>
- /// <param name="track">The track to retrieve options for.</param>
- /// <param name="boundObject">The binding for the track.</param>
- /// <param name="detectErrors">The errors to check for.</param>
- /// <returns>An error to be displayed on the track, or string.Empty if there is no error.</returns>
- public string GetErrorText(TrackAsset track, UnityEngine.Object boundObject, TrackBindingErrors detectErrors)
- {
- if (track == null || boundObject == null)
- return string.Empty;
- var bindingType = GetBindingType(track);
- if (bindingType != null)
- {
- // bound to a prefab asset
- if (HasFlag(detectErrors, TrackBindingErrors.PrefabBound) && PrefabUtility.IsPartOfPrefabAsset(boundObject))
- {
- return k_PrefabBound;
- }
- // If we are a component, allow for bound game objects (legacy)
- if (typeof(Component).IsAssignableFrom(bindingType))
- {
- var gameObject = boundObject as GameObject;
- var component = boundObject as Component;
- if (component != null)
- gameObject = component.gameObject;
- // game object is bound with no component
- if (HasFlag(detectErrors, TrackBindingErrors.NoValidComponent) && gameObject != null && component == null)
- {
- component = gameObject.GetComponent(bindingType);
- if (component == null)
- {
- return k_NoValidComponent;
- }
- }
- // attached gameObject is disables (ignores Activation Track)
- if (HasFlag(detectErrors, TrackBindingErrors.BoundGameObjectDisabled) && gameObject != null && !gameObject.activeInHierarchy)
- {
- return k_BoundGameObjectDisabled;
- }
- // component is disabled
- var behaviour = component as Behaviour;
- if (HasFlag(detectErrors, TrackBindingErrors.BehaviourIsDisabled) && behaviour != null && !behaviour.enabled)
- {
- return k_RequiredComponentIsDisabled;
- }
- // mismatched binding
- if (HasFlag(detectErrors, TrackBindingErrors.InvalidBinding) && component != null && !bindingType.IsAssignableFrom(component.GetType()))
- {
- return k_InvalidBinding;
- }
- }
- // Mismatched binding (non-component)
- else if (HasFlag(detectErrors, TrackBindingErrors.InvalidBinding) && !bindingType.IsAssignableFrom(boundObject.GetType()))
- {
- return k_InvalidBinding;
- }
- }
- return string.Empty;
- }
- /// <summary>
- /// Gets the color information of a track.
- /// </summary>
- /// <param name="track"></param>
- /// <returns>Returns the color for the specified track.</returns>
- public Color GetTrackColor(TrackAsset track)
- {
- return TrackResourceCache.GetTrackColor(track);
- }
- /// <summary>
- /// Gets the binding type for a track.
- /// </summary>
- /// <param name="track">The track to retrieve the binding type from.</param>
- /// <returns>Returns the binding type for the specified track. Returns null if the track does not have binding.</returns>
- public System.Type GetBindingType(TrackAsset track)
- {
- if (track == null)
- return null;
- System.Type result = null;
- if (m_BindingCache.TryGetValue(track, out result))
- return result;
- result = track.outputs.Select(x => x.outputTargetType).FirstOrDefault();
- m_BindingCache[track] = result;
- return result;
- }
- /// <summary>
- /// Callback for when a track is created.
- /// </summary>
- /// <param name="track">The track that is created.</param>
- /// <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>
- public virtual void OnCreate(TrackAsset track, TrackAsset copiedFrom)
- {
- }
- /// <summary>
- /// Callback for when a track is changed.
- /// </summary>
- /// <param name="track">The track that is changed.</param>
- public virtual void OnTrackChanged(TrackAsset track)
- {
- }
- private static bool HasFlag(TrackBindingErrors errors, TrackBindingErrors flag)
- {
- return (errors & flag) != 0;
- }
- }
- }
|