Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use A Value of the Track Binding on CreatePlayable

Discussion in 'Timeline' started by enslaved2die, Sep 15, 2020.

  1. enslaved2die

    enslaved2die

    Joined:
    Aug 13, 2017
    Posts:
    24
    I want to set a default to a value in my clip template on CreatePlayable() which I get from the corresponding Track Binding Component

    I cant figure out how to get access to the TrackBinding in the CreatePlayable() or even with the newly introduced Clip Editor.
    I always get to a road block where I either cant access the graph when I have the track or I cant get access the track when I have the graph.

    The idea behind this is to always have a default value but can be changed to a different one for specific uses cases.

    Okay I think this is a little bit to superficial, so here my real goal:
    I want to use the Sprite Resolver in the 2D Animation Package with the Timeline (Animation is kinda broke)
    I want to drag the Sprite Resolver Component into the Timeline and when I create a Clip I want that clip to get the Sprite Category of the Track Binding (Sprite Resolver), but I also want to change the Category String to a different one.

    e.g I have my Mouth with different phonomes shapes in a Category called "Phonemes", but than I want to also use a different Category for e.g emotions.

    Of course I can put the category and label in manually all the time, but this is extremly tedious and I want a more elegant solution.

    Setting a dynamically receivable default which can be changed after creation.

    Maybe Im stuck In an approach that is way to complicated.

    I also thought about keeping the category string empty and using the original one, but than when I change the category name its stuck an the new category name, so I need to store the default category name and change it back after the current clip has ended or so.

    I hope the forum can help me.



    Code where I think I want to put the default value:
    Code (CSharp):
    1.  
    2. public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    3.     {
    4. // Get the Director
    5.         var director = graph.GetResolver() as PlayableDirector;
    6. // use the Director to get the DefaultBinding of the Track... but I cant get the track here
    7.  
    8.         settings.category = "DEFAULT VALUE";
    9.  
    10.         var playable = ScriptPlayable<SpriteResolverClipBehaviour>.Create(graph, settings);
    11.  
    12.         return playable;
    13.     }
     
    Last edited: Sep 15, 2020
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Yes, unfortunately the track is not available at that point unless you explicitly assign it.

    An alternative, if you are using a custom track, is to use a TrackAsset.CreateTrackMixer override to set the defaults instead. Something like the following:

    Code (CSharp):
    1. public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount)
    2. {
    3.     var director = graph.GetResolver() as PlayableDirector;
    4.     var binding = director.GetGenericBinding(this) as MyBindingType;
    5.     foreach (var clip in GetClips())
    6.     {
    7.         var playableAsset = clip.asset as MyPlayableAsset;
    8.         if (playableAsset != null)
    9.             playableAsset.settings.DefaultValue = binding.DefaultValue;
    10.     }    
    11.     return base.CreateTrackMixer(graph, go, inputCount);
    12. }
    13.  
    Of course, you could just assign the track to a property in MyPlayableAsset here as well.

    Or, you can get the track binding from the PlayableBehaviour PrepareFrame playerData parameter from inside the graph - that usually requires a bit of bookkeeping to know if it's the first call.
     
  3. enslaved2die

    enslaved2die

    Joined:
    Aug 13, 2017
    Posts:
    24
    Yeah this works, but than I have the problem that I cant change it after that because the TrackMixer is forcing the override.
    Is there a "Only On Create" Function or something like this?
    Maybe via the Clip Editor?
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    I'm not sure I understand. CreateTrackMixer is only called immediately prior to CreatePlayable. It will be set only when the timeline is about to be played.

    The Clip Editor does have OnCreate call that gives you access to owning TimelineClip (and thus parentTrack), if your intent is to only set it when the clip is created. You can access the director using TimelineEditor.inspectedDirector
     
  5. enslaved2die

    enslaved2die

    Joined:
    Aug 13, 2017
    Posts:
    24
    This is why Timeline is overwriting my changed values immediately.


    Thats the magic trick!

    Yes exactly what I wanted.
    Parse a value OnCreate into the created Clip.
    This allows me to change the value after creation
    Thanks!

    Here is my solution, its located in the ClipEditor:

    Code (CSharp):
    1.     public override void OnCreate(TimelineClip clip, TrackAsset track, TimelineClip clonedFrom)
    2.     {
    3.         var shotClip = (SpriteResolverClip)clip.asset;
    4.  
    5.         var spriteResolver =  TimelineEditor.inspectedDirector.GetGenericBinding(track) as SpriteResolver;
    6.    
    7.         shotClip.settings.category = spriteResolver.GetCategory();
    8.     }
     
    Last edited: Sep 15, 2020