Skip to content

Instantly share code, notes, and snippets.

@to-osaki
Created August 21, 2022 09:56
Show Gist options
  • Save to-osaki/fde47de5d38a17e4c7d4fb51e0600f52 to your computer and use it in GitHub Desktop.
Save to-osaki/fde47de5d38a17e4c7d4fb51e0600f52 to your computer and use it in GitHub Desktop.

Find track by type

T FindTrack<T>(PlayableGraph graph) where T : TrackAsset
{
  var director = (graph.GetResolver() as PlayableDirector);
  foreach (var binding in director.playableAsset.outputs)
  {
    if (binding.sourceObject is T track)
    {
      return track;
    }
  }
  return null;
}

Set clip param into mixer

[TrackColor(1f, 0f, 0f)]
public class XxxxTrack : TrackAsset
{
  public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
  {
    var scriptPlayable = ScriptPlayable<XxxxMixer>.Create(graph, inputCount);
    var mixer = scriptPlayable.GetBehaviour();

    foreach (var clip in GetClips())
    {
      var asset = clip.asset as XxxxxPlayableAsset;
      mixer.yy = asset.zz;
    }
    return scriptPlayable;
  }
}

https://github.com/UnityTechnologies/ATerribleKingdom

Create Timeline at runtime

PlayableAsset CreateTimeline(AnimationClip[] clips)
{
  var tl = ScriptableObject.CreateInstance<TimelineAsset>();
  var track = tl.CreateTrack<AnimationTrack>("Animation");

  float total = 0f;
  for (int i = 0; i < clips.Length; i++)
  {
    var clip = clips[i];
    float start = total;
    float duration = clip.length;
    
    var clip = track.CreateClip<AnimationPlayableAsset>();
    (clip.asset as AnimationPlayableAsset).clip = clip;
    clip.start = start;
    clip.duration = duration;

    total += duration;
  }
  return tl;
}

Create timeline asset

  var tl = ScriptableObject.CreateInstance<TimelineAsset>();
  AssetDatabase.CreateAsset(tl, "test.playable"));
  AssetDatabase.SaveAssets();
  
  // need SaveAssets before CreateTrack, or AddObjectToAsset manually
  var track = tl.CreateTrack<AnimationTrack>();

  EditorUtility.SetDirty(tl);
  ProjectWindowUtil.ShowCreatedAsset(tl);
  TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);

Set TimelineClip.(pre|post)ExtrapolationMode from Script

https://forum.unity.com/threads/set-timelineclip-pre-post-extrapolationmode-from-script.475679/

Set play speed

playable.GetGraph().GetRootPlayable(0).SetSpeed(Xf);

Send Notification to Track binding object

public class Xxxx : PlayableBehaviour
{
  public override void OnBehaviourPause(Playable playable, FrameData info)
  {
    info.output.PushNotification(playable, new XxxxNotification();
  }
}

private class NotificationReceiver : MonoBehaviour, INotificationReceiver
{
	void INotificationReceiver.OnNotify(Playable origin, INotification notification, object context)
	{
		if (notification is XxxxNotification)
		{
      ...
		}
	}
}

https://blog.unity.com/ja/technology/how-to-create-custom-timeline-markers https://tsubakit1.hateblo.jp/entry/2018/12/10/233146

Modify clip displayName

public class XxxxTrack : TrackAsset
{
  protected override Playable CreatePlayable(PlayableGraph graph, GameObject gameObject, TimelineClip clip)
  {
    if (clip.asset is XxxxPlayableAsset asset)
    {
      clip.displayName = "Xxxx";
    }
    return base.CreatePlayable(graph, gameObject, clip);
  }
}
[CustomTimelineEditor(typeof(XxxxClip))]
public class XxxxClipEditor : ClipEditor
{
    public override void OnClipChanged(TimelineClip clip)
    {
        var asset = clip.asset as XxxxPlayableAsset;
        clip.displayName = asset.xxxx;
        base.OnClipChanged(clip);
    }
}

https://tips.hecomi.com/entry/2022/03/28/235336

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment