Search Unity

Set ControlPlayableAsset clip's Source Game Object

Discussion in 'Timeline' started by hoperin, Apr 24, 2018.

  1. hoperin

    hoperin

    Joined:
    Feb 9, 2014
    Posts:
    52
    Everytime I think I've finally got a handle on Timeline another thign throws me off. I'm trying to set the target of the ControlPlayableAsset clips that I'm spawning on a Control Track. I'm getting this error:

    Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.ExposedReference<UnityEngine.GameObject>'

    This is the chunk of code in question:
    Code (CSharp):
    1. void AddControlClip(PlayableDirector TLparent, GameObject target, string name){
    2.  
    3.         TimelineAsset tl = TLparent.playableAsset as TimelineAsset;
    4.  
    5.         //find the control track;
    6.         TrackAsset track = tl.GetOutputTrack (0);
    7.  
    8.         //create the new clip on the TL
    9.         TimelineClip tlClip = track.CreateClip<ControlPlayableAsset>();
    10.         ControlPlayableAsset clip = tlClip.asset as ControlPlayableAsset;
    11.  
    12. [B]        //assign its target where target = Gameobject
    13.         clip.sourceGameObject = target;[/B]
    14.  
    15.         clip.postPlayback = ActivationControlPlayable.PostPlaybackState.Inactive;
    16.  
    17.     }
    where

    clip.sourceGameObject = target;

    is the culprit...

    Thanks in advance! I'm sure it's just a simple syntax thing like all myprevious Timeline issues but I just don't get this one.
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    954
    use TLparent.SetReferenceValue(clip.sourceGameObject.exposedName, target);

    Because Timeline is an asset, scene references cannot be set directly on clips, so instead an exposed reference is used, which is just a lookup id. The actual gameObject is stored in a lookup table inside the playable director.
     
  3. hoperin

    hoperin

    Joined:
    Feb 9, 2014
    Posts:
    52
    Thanks for the reply! This half works -- it assigns the right target, but it also overwrites each control clip on the track so that they all target it, not just the single clip I'm trying to target.

    I guess this is because it's applying the target to the playableDirector as a whole? But there must be a way to assign exposed references individually?
     
  4. hoperin

    hoperin

    Joined:
    Feb 9, 2014
    Posts:
    52
    Just wanted to throw in an update for anyone who is having the same issue that I figured out what was missing thanks to another thread dealing with exposed references!
    ( https://forum.unity.com/threads/timeline-cinemachine-create-shots-by-script.482480/#post-3141845 )

    I was missing initialization of exposedName (whatever that does, I'm not sure at all honestly, but it works!)

    Code (CSharp):
    1. clip.sourceGameObject.exposedName = UnityEditor.GUID.Generate ().ToString ();
    2. TLparent.SetReferenceValue (clip.sourceGameObject.exposedName, target);
     
    Pacosmico likes this.
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    954
    Yes, that's a good point that I missed. The exposed reference does that when you view it in the editor, but when creating them from script the exposedName has a default value.

    You can set the exposedName to something meaningful (instead of a GUID)
    clip.sourceGameObject.exposedName = "LookAtTarget";
    TLparent.SetReferenceValue(new PropertyName("LookAtTarget"), target);

    In that case, the string should show appear in the inspector.
     
  6. Cleverlie

    Cleverlie

    Joined:
    Dec 23, 2013
    Posts:
    196
    it would be really useful if this tiny bits and snippets of code are added in a pedagogical way to the unity official documentation. I find myself having to copy this little snippets in a folder and organize them myself to keep track of how to work with timeline, the information is very disperse and only googling with a good eye you encounter this answers to questions and threads made a time ago, just a suggestion for the people handling the documentation.
     
    BobMCleod and hoperin like this.
  7. BobMCleod

    BobMCleod

    Joined:
    Sep 15, 2017
    Posts:
    1
    How I would LOVE that! I am spending hours upon hours assembling small tidbits into a workable implementation too.
     
    Cleverlie likes this.