Search Unity

Assign a Timeline to ControlTrack with C# without loosing the connection when the scene reloads

Discussion in 'Timeline' started by KristofferH, Nov 20, 2019.

  1. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    52
    I'm trying to add a TimelineAsset to a ControllTrack with C# and it is working. However when I restart Unity or reload the scene the connection is lost. If I however add the same GameObject (with the PlayableDirector) manually via the Inspector then Unity will remember it. I've tried using
    AssetDatabase.SaveAssets()
    EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), EditorSceneManager.GetActiveScene().path)
    EditorApplication.ExecuteMenuItem("File/Save Project")
    EditorUtility.SetDirty()
    to try to force Unity to save the changes in the assets but it does not help. Saving manually from the Editor UI does not help either.

    This is the code I use to set the GameObject with the PlayableDirector for the second timeline (called sequence) to be played from a ControlTrack in the first timeline (called profile). As I said this code works, the Source Game Object field in the Control Playable Asset looks like it is set properly in the Inspector and the animation (sequence) plays as expected but when I save the scene and reloads it, this connection is gone (Source Game Object field set to None), but if I set it manually (drag-n-drop the GameObject with the PlayableDirector for the second timeline to the field Source Game Object in the ControlTrack via the Inspector) and save the scene it works fine to reload the scene, nothing is lost, everything keeps working.
    Code (CSharp):
    1. var profile = parentProfile.GetComponent<PlayableDirector>().playableAsset as TimelineAsset;
    2. ControlTrack sequenceTrack = profile.CreateTrack<ControlTrack>(null, sequence.name);
    3. var sequenceClip = sequenceTrack.CreateClip<ControlPlayableAsset>();
    4. sequenceClip.displayName = sequence.name;
    5. var controlAsset = (ControlPlayableAsset)sequenceClip.asset;
    6. controlAsset.sourceGameObject.defaultValue = sequenceDirector.gameObject;
    I'm using Unity 2019.1.14 and have tried it in 2019.2.9 but I get the same result in both versions. @seant_unity do you have an explanation and/or workaround for this?
     
  2. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    52
    Sorry for duplicating this question, but I was not able to tagg you @seant_unity in the Answers section (it just tags someone else called just sean instead). Feel free to keep your answers in the answers section or move it over here if it needs a more in-depth discussion.
     
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    KristofferH likes this.
  4. KristofferH

    KristofferH

    Joined:
    Oct 27, 2012
    Posts:
    52
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    done
     
    KristofferH likes this.