Skip to content

Instantly share code, notes, and snippets.

@douduck08
Created August 21, 2020 08:57
Show Gist options
  • Select an option

  • Save douduck08/9ea9db09736bd5589eaa7a8c22d4e6b9 to your computer and use it in GitHub Desktop.

Select an option

Save douduck08/9ea9db09736bd5589eaa7a8c22d4e6b9 to your computer and use it in GitHub Desktop.

Revisions

  1. douduck08 created this gist Aug 21, 2020.
    57 changes: 57 additions & 0 deletions DirectorExtension.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    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);
    }
    }
    }
    }
    }
    }