- Home /
How to reference a TrackAsset or PlayableAsset
Hi,
I have some scenes that are being loaded through a Master Timeline (I created a custom Track, PlayableAsset and PlayableBehaviour). These scenes have a sub-timeline that i am also loading in the aforementioned Master Timeline, using a ControlTrack.
I wonder how can I get a reference to the ControlTrack (or the clip on this track) that contains my sub-timeline. I am currently taking it by it´s name (code below) but ideally i would like to make a public ControlTrack property on my PlayableBehaviour that loads the scenes so it has a reference to its own timeline... although it seems impossible to drag & drop a clip or track from the Timeline to a reference slot!
TimelineAsset playable = mainPlayableDirector.playableAsset as TimelineAsset;
// Look for the Control Track of the loaded scene.
foreach (PlayableBinding binding in playable.outputs)
{
if(binding.sourceObject)
{
if (binding.sourceObject.GetType() == typeof(ControlTrack))
{
ControlTrack tmpControlTrack = (ControlTrack)binding.sourceObject;
// If our Control Track exists, assign the TIMELINE object to all its clips if (tmpControlTrack.name == "TIMELINE " + scene.name)
{
controlTrack = tmpControlTrack;
}
}
}
}
Anyone knows a better way of doing this? I thought about using controlTrack.GetInstanceID() and store them on a Dictionary using the scene name as the key or something like that, but i don´t really like the solution...
Thanks! D
Answer by seant_unity · Sep 29, 2020 at 07:48 PM
You can use TimelineAsset.GetOutputTracks() to walk the list of tracks in the timeline asset. And if you want drag and drop, you can change the hideFlags on the Tracks themselves to show up as subassets on your timeline file - like recorded animation clips do. (e.g. track.hideFlags &= ~HideFlags.HideInHierarchy).
Answer by Dan_G · Sep 30, 2020 at 05:17 PM
Hi @seant_unity , I just changed my code and I am now using TimelineAsset.GetOutputTracks() to go through all the tracks in my timeline asset. It works better than my approach above! However, my problem persists. I still have to filter the ControlTracks in the Timeline by its name, which is a very weak solution as anyone can accidentally change the track´s name. I tried to store the InstanceID or HashCode somewhere to have a solid reference to the ControlTrack, but they change every time i close and open Unity...
Regarding the drag & drop solution, i think I didn´t explained myself well. Ideally, I would like to find a solution where i can create an ExposedReference on my CustomClip (extends from PlayableAsset) and then reference or assign (doingdrag & drop?) a ControlTrack or ControlPlayableAsset. This way i will have my CustomClip well coupled with the ControlTrack or ControlPlayableAsset that contains it´s subtimeline (remember the CustomClip just loads a scene that contains a sub-timeline). The issue is that i cannot reference any ControlTrack or Clip as i cannot drag & drop them to any ExposedReference slot...
Any suggestion? I hope i explained myself better this time :) PS: I think i did not fully understand the hideFlags solution... can you please elaborate if it is pertinent?
foreach (TrackAsset track in playable.GetOutputTracks())
{
if (track.GetType() == typeof(ControlTrack))
{
// If our Control Track exists, we assign the TIMELINE object to all its clips
if (track.name == "TIMELINE " + scene.name)
{
controlTrack = (ControlTrack)track;
}
}
}
You don't need an exposed reference for ControlTracks, or ControlPlayableAssets as they are assets (i.e. ScriptableObjects). ExposedReferences are for accessing a scene object from an asset file. Just a regular variable (e..g public ControlTrack controlTrack) will do fine.
The problem is they are hidden in the project window. If you run a script to unhide them, foreach (ControlTrack track in playable.GetOutputTracks().OfType()) { track.hideFlags &= ~HideFlags.HideInHierarchy(); EditorUtility.SetDirty(track); } AssetDatabase.SaveAssets();
then in the project window, the playable file will have a foldout and the control tracks will be listed. From there you can drag and drop them to the inspector.