Search Unity

TimelineClip is not getting serialized properly

Discussion in 'Timeline' started by Stacklucker, Jan 19, 2021.

  1. Stacklucker

    Stacklucker

    Joined:
    Jan 23, 2015
    Posts:
    82
    Hi there,

    I am running into a serialization issue when dynamically creating TimelineClips for a TrackAsset in editor code.
    I am creating the TimelineClip via

    Code (CSharp):
    1. TimelineClip defaultClip = tracks[i].CreateDefaultClip();
    and feeding into its asset field a cloned PlayableAsset,

    Code (CSharp):
    1.    var clone = ScriptableObject.Instantiate(asset);
    2. defaultClip.asset = clone;
    3.  
    This seems to work well while the editor is running, but closing and reopening Unity again will see the asset field of the TimelineClip set to null. "DefaultClip" still retains all its other fields, like the start, duration of parentTrack information, but the cloned asset "clone" is nowhere to be found.

    I tried dirtying the asset

    Code (CSharp):
    1. EditorUtility.SetDirty(defaultClip.asset);
    as well as the TrackAsset and the TimelineAsset, but the connection to the PlayableAsset keeps getting lost.

    Since TimelineClip doesn't inherit from UnityEngine.Object I can't set it dirty. Is there some other way to serialized/save the TimelineClip and keeps it's connection to the PlayableAsset?

    I suspect the cloning of the PlayableAsset to be the root of the issue. Maybe there is a better way to duplicate a given PlayableAsset and feed it into a new TimelineClip?

    Any help is much appreciated.
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Cloned ScriptableObjects need to be saved to an asset in order to be persisted as references in assets. Otherwise they are saved in the scene. Since assets cannot refer to scene objects, the reference will be nulled once you restart.

    So either you need to create a new asset on disk for it, or you need to use AssetDatabase.AddObjectToAsset to add it as a sub asset of the Timeline to which the track belongs
     
  3. Stacklucker

    Stacklucker

    Joined:
    Jan 23, 2015
    Posts:
    82
    @DavidGeoffroy Thank you very much, adding the cloned playable asset to the timeline did the trick!