Skip to content

Instantly share code, notes, and snippets.

@SK83RJOSH
Created April 28, 2017 18:20
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
public static class TrackAssetExtensions
{
static Type TrackAssetType = typeof(TrackAsset);
static Type TimelineClipType = typeof(TimelineClip);
public static TimelineClip CreateClipInternal(this TrackAsset track)
{
var method = TrackAssetType.GetMethod("CreateNewClipContainerInternal", BindingFlags.NonPublic | BindingFlags.Instance);
return method.Invoke(track, new object[] { }) as TimelineClip;
}
public static TimelineClip CreateClipInternal(this TrackAsset track, Type type)
{
if (type == null || !typeof(IPlayableAsset).IsAssignableFrom(type) || !typeof(ScriptableObject).IsAssignableFrom(type))
{
throw new InvalidOperationException("Cannot create an clip for type " + type.Name);
}
var scriptableObject = ScriptableObject.CreateInstance(type);
if (scriptableObject == null)
{
throw new InvalidOperationException("Could not create an instance of the ScriptableObject type " + type.Name);
}
scriptableObject.name = type.Name;
// Creates a valid clip
var timelineClip = track.CreateClipInternal();
// Sets the clip title, asset, and length
var method = TrackAssetType.GetMethod("OnCreateClipFromAsset", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(track, new object[] { scriptableObject, timelineClip });
// Add the clip to m_Clips
track.AddClip(timelineClip);
return timelineClip;
}
public static TimelineClip CreateClipInternal<T>(this TrackAsset track) where T : ScriptableObject, IPlayableAsset, new()
{
return track.CreateClipInternal(typeof(T));
}
public static TimelineClip CreateClip(this TrackAsset track, Type type)
{
var method = TrackAssetType.GetMethod("CreateClipOfType", BindingFlags.NonPublic | BindingFlags.Instance);
return method.Invoke(track, new object[] { type }) as TimelineClip;
}
public static TimelineClip CreateClip<T>(this TrackAsset track) where T : ScriptableObject, IPlayableAsset, new()
{
return track.CreateClip(typeof(T));
}
public static void AddClip(this TrackAsset track, TimelineClip clip)
{
var method = TrackAssetType.GetMethod("AddClip", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(track, new object[] { clip });
}
public static void RemoveClip(this TrackAsset track, TimelineClip clip)
{
var method = TrackAssetType.GetMethod("RemoveClip", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(track, new object[] { clip });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment