Skip to content

Instantly share code, notes, and snippets.

@douduck08
Created August 21, 2020 08:57
Show Gist options
  • Save douduck08/9ea9db09736bd5589eaa7a8c22d4e6b9 to your computer and use it in GitHub Desktop.
Save douduck08/9ea9db09736bd5589eaa7a8c22d4e6b9 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public static class DirectorExtension {
/// <summary>
/// When 'trackName' is null or empty, this method will set all the tracks without name check.
/// </summary>
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);
}
}
}
}
/// <summary>
/// When 'trackName' is null or empty, this method will set all the tracks without name check.
/// </summary>
public static void SetBinding<T> (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);
}
}
}
}
/// <summary>
/// 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.
/// </summary>
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<TransformTweenTrack> ();
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);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment