using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; public static class DirectorExtension { /// /// When 'trackName' is null or empty, this method will set all the tracks without name check. /// public static void SetBindingGameObject (this PlayableDirector director, GameObject gameObject, string trackName = "") { foreach (var ouput in director.playableAsset.outputs) { if (string.IsNullOrEmpty (trackName) || ouput.streamName == trackName) { if (ouput.outputTargetType != null) { var bindingTarget = gameObject.GetComponent (ouput.outputTargetType); director.SetGenericBinding (ouput.sourceObject, bindingTarget); } else { director.SetGenericBinding (ouput.sourceObject, gameObject); } } } } /// /// When 'trackName' is null or empty, this method will set all the tracks without name check. /// public static void SetBinding (this PlayableDirector director, T binding, string trackName = "") where T : UnityEngine.Object { foreach (var ouput in director.playableAsset.outputs) { if (ouput.outputTargetType == typeof (T)) { if (string.IsNullOrEmpty (trackName) || ouput.streamName == trackName) { director.SetGenericBinding (ouput.sourceObject, binding); } } } } /// /// When 'trackName' is null or empty, this method will set all the tracks without name check. /// When 'clipName' is null or empty, this method will set all the clips without name check. /// public static void SetTransformTweenLocation (this PlayableDirector director, Transform start, Transform end, string trackName = "", string clipName = "") { var timeline = director.playableAsset as TimelineAsset; var tracks = timeline.GetOutputTracks ().OfType (); foreach (var track in tracks) { if (string.IsNullOrEmpty (trackName) || track.name == trackName) { foreach (var clip in track.GetClips ()) { if (string.IsNullOrEmpty (clipName) || clip.displayName == clipName) { var tweenClip = clip.asset as TransformTweenClip; director.SetReferenceValue (tweenClip.startLocation.exposedName, start); director.SetReferenceValue (tweenClip.endLocation.exposedName, end); } } } } } }