using System; #if UNITY_EDITOR using UnityEditor; #endif using UnityEngine; using UnityEngine.Timeline; using UnityEngine.Playables; namespace FT.Timeline { [Serializable] [TrackColor(0.565f, 0.753f, 0.565f)] [TrackClipType(typeof(SiblingOrderPlayableAsset))] [TrackBindingType(typeof(GameObject))] [ExcludeFromPreset] public class SiblingOrderTrack : TrackAsset { public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount) { return ScriptPlayable.Create(graph, inputCount); } protected override void OnCreateClip(TimelineClip clip) { clip.displayName = "Sibling Order"; base.OnCreateClip(clip); } #if UNITY_EDITOR public override void GatherProperties(PlayableDirector director, IPropertyCollector driver) { var boundObject = director.GetGenericBinding(this) as GameObject; if (boundObject != null) { var clips = GetClips(); foreach (var clip in clips) { var playableAsset = clip.asset as SiblingOrderPlayableAsset; if (playableAsset != null && playableAsset.children.Count == 0) { RefreshChildren(playableAsset, boundObject); } } } base.GatherProperties(director, driver); } public void RefreshChildren(SiblingOrderPlayableAsset playableAsset, GameObject boundObject) { if (playableAsset == null || boundObject == null) return; playableAsset.children.Clear(); int childCount = boundObject.transform.childCount; for (int i = 0; i < childCount; i++) { var child = boundObject.transform.GetChild(i).gameObject; var exposedRef = new ExposedReference { defaultValue = child }; exposedRef.exposedName = GUID.Generate().ToString(); playableAsset.children.Add(exposedRef); var director = boundObject.GetComponent(); if (director != null) { director.SetReferenceValue(exposedRef.exposedName, child); } } } #endif } }