123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- using System;
- using System.Linq;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Timeline;
- namespace UnityEditor.Timeline
- {
-
-
-
- public struct TrackDrawOptions
- {
-
-
-
-
-
-
- public string errorText { get; set; }
-
-
-
- public Color trackColor { get; set; }
-
-
-
- public float minimumHeight { get; set; }
-
-
-
-
-
-
- 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);
- }
- }
-
-
-
- public enum TrackBindingErrors
- {
-
-
-
- None = 0,
-
-
-
- BoundGameObjectDisabled = 1 << 0,
-
-
-
- NoValidComponent = 1 << 1,
-
-
-
- BehaviourIsDisabled = 1 << 2,
-
-
-
- InvalidBinding = 1 << 3,
-
-
-
- PrefabBound = 1 << 4,
-
-
-
- All = Int32.MaxValue
- }
-
-
-
- public class TrackEditor
- {
- static readonly string k_BoundGameObjectDisabled = L10n.Tr("The bound GameObject is disabled.");
- static readonly string k_NoValidComponent = L10n.Tr("Could not find appropriate component on this gameObject");
- static readonly string k_RequiredComponentIsDisabled = L10n.Tr("The component is disabled");
- static readonly string k_InvalidBinding = L10n.Tr("The bound object is not the correct type.");
- static readonly string k_PrefabBound = L10n.Tr("The bound object is a Prefab");
- readonly Dictionary<TrackAsset, System.Type> m_BindingCache = new Dictionary<TrackAsset, System.Type>();
-
-
-
- public static readonly float DefaultTrackHeight = 30.0f;
-
-
-
- public static readonly float MinimumTrackHeight = 10.0f;
-
-
-
- public static readonly float MaximumTrackHeight = 256.0f;
-
-
-
-
-
-
- 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
- };
- }
-
-
-
-
-
-
-
- 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)
- {
-
- if (HasFlag(detectErrors, TrackBindingErrors.PrefabBound) && PrefabUtility.IsPartOfPrefabAsset(boundObject))
- {
- return k_PrefabBound;
- }
-
- if (typeof(Component).IsAssignableFrom(bindingType))
- {
- var gameObject = boundObject as GameObject;
- var component = boundObject as Component;
- if (component != null)
- gameObject = component.gameObject;
-
- if (HasFlag(detectErrors, TrackBindingErrors.NoValidComponent) && gameObject != null && component == null)
- {
- component = gameObject.GetComponent(bindingType);
- if (component == null)
- {
- return k_NoValidComponent;
- }
- }
-
- if (HasFlag(detectErrors, TrackBindingErrors.BoundGameObjectDisabled) && gameObject != null && !gameObject.activeInHierarchy)
- {
- return k_BoundGameObjectDisabled;
- }
-
- var behaviour = component as Behaviour;
- if (HasFlag(detectErrors, TrackBindingErrors.BehaviourIsDisabled) && behaviour != null && !behaviour.enabled)
- {
- return k_RequiredComponentIsDisabled;
- }
-
- if (HasFlag(detectErrors, TrackBindingErrors.InvalidBinding) && component != null && !bindingType.IsAssignableFrom(component.GetType()))
- {
- return k_InvalidBinding;
- }
- }
-
- else if (HasFlag(detectErrors, TrackBindingErrors.InvalidBinding) && !bindingType.IsAssignableFrom(boundObject.GetType()))
- {
- return k_InvalidBinding;
- }
- }
- return string.Empty;
- }
-
-
-
-
-
- public Color GetTrackColor(TrackAsset track)
- {
- return TrackResourceCache.GetTrackColor(track);
- }
-
-
-
-
-
- public System.Type GetBindingType(TrackAsset track)
- {
- if (track == null)
- return null;
- if (m_BindingCache.TryGetValue(track, out var result))
- return result;
- result = track.outputs.Select(x => x.outputTargetType).FirstOrDefault();
- m_BindingCache[track] = result;
- return result;
- }
-
-
-
-
-
- public virtual void OnCreate(TrackAsset track, TrackAsset copiedFrom)
- {
- }
-
-
-
-
- public virtual void OnTrackChanged(TrackAsset track)
- {
- }
- static bool HasFlag(TrackBindingErrors errors, TrackBindingErrors flag)
- {
- return (errors & flag) != 0;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public virtual bool IsBindingAssignableFrom(UnityEngine.Object candidate, TrackAsset track)
- {
- var action = BindingUtility.GetBindingAction(GetBindingType(track), candidate);
- return action != BindingUtility.BindingAction.DoNotBind;
- }
-
-
-
-
-
-
-
-
-
-
-
- public virtual UnityEngine.Object GetBindingFrom(UnityEngine.Object candidate, TrackAsset track)
- {
- Type bindingType = GetBindingType(track);
- BindingUtility.BindingAction action = BindingUtility.GetBindingAction(bindingType, candidate);
- return BindingUtility.GetBinding(action, candidate, bindingType);
- }
- }
- static class TrackEditorExtension
- {
- public static bool SupportsBindingAssign(this TrackEditor editor)
- {
- return TypeUtility.HasOverrideMethod(editor.GetType(), nameof(TrackEditor.GetBindingFrom));
- }
- public static void OnCreate_Safe(this TrackEditor editor, TrackAsset track, TrackAsset copiedFrom)
- {
- try
- {
- editor.OnCreate(track, copiedFrom);
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- }
- }
- public static TrackDrawOptions GetTrackOptions_Safe(this TrackEditor editor, TrackAsset track, UnityEngine.Object binding)
- {
- try
- {
- return editor.GetTrackOptions(track, binding);
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- return CustomTimelineEditorCache.GetDefaultTrackEditor().GetTrackOptions(track, binding);
- }
- }
- public static UnityEngine.Object GetBindingFrom_Safe(this TrackEditor editor, UnityEngine.Object candidate, TrackAsset track)
- {
- try
- {
- return editor.GetBindingFrom(candidate, track);
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- return candidate;
- }
- }
- public static bool IsBindingAssignableFrom_Safe(this TrackEditor editor, UnityEngine.Object candidate, TrackAsset track)
- {
- try
- {
- return editor.IsBindingAssignableFrom(candidate, track);
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- return false;
- }
- }
- public static void OnTrackChanged_Safe(this TrackEditor editor, TrackAsset track)
- {
- try
- {
- editor.OnTrackChanged(track);
- }
- catch (Exception e)
- {
- Debug.LogException(e);
- }
- }
- }
- }
|