/TrackAssetExtension.cs Secret
Created
April 28, 2017 18:20
Star
You must be signed in to star a gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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