Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Creating a graph and playable in editor context resulting in an error

Discussion in 'Timeline' started by JakubTrailmix, Aug 16, 2020.

  1. JakubTrailmix


    Jun 1, 2018

    I'm trying to do a timeline clip visualisation of the effect it's going to cause in the inspector.
    Basically we have a base TransformTrack which allows you to perform a set of tweens movements on a number of game objects at a time.
    There is no possibility of actually previewing the timeline for scrubbing as none of the items is actually created, they're fed in to the clip at runtime - that is why I basically want to simulate how something will behave and have a small preview window in the inspector of that clip.

    I got quite far with this, basically got he working preview window and the whole simulation nicely playing back.
    The only issue is that on any code change when we trigger a script compilation I get a
    Code (CSharp):
    1. Assembly Cache Should Be Empty
    This only happens when I use that inspector and I presume there is an issue as for the preview and proper prediction for the simulation I need to create the playable and the playableBehaviour, cache it and run some methods on top of that.
    I cannot reliably run the behaviour on it's own or the copy the calculation out of the clip behaviour as the clip itself can be extended and we have several versions that apply different effects to the movement.

    The code I use to create the temporary graph and playable:
    Code (CSharp):
    2.                 if(!m_previewPlayableGraph.IsValid())
    3.                     m_previewPlayableGraph = PlayableGraph.Create($"Preview graph for Transform Move Clip Inspector");
    5.                 m_previewPlayable = playableAsset.CreatePlayable(m_previewPlayableGraph, null);
    6.                 var playableType = m_previewPlayable.GetPlayableType();
    7.                 if (s_behaviourBaseType.IsAssignableFrom(playableType))
    8.                     m_previewTransformBehaviour = ((ScriptPlayable<TransformMoveBehaviour>) m_previewPlayable).GetBehaviour();
    Then OnDisable and OnDestroy I call

    Code (CSharp):
    1.             if(m_previewPlayable.IsValid())
    2.                 m_previewPlayable.Destroy();
    3.             if(m_previewPlayableGraph.IsValid())
    4.                 m_previewPlayableGraph.Destroy();
    I presume this is the cause of that error, as when I remove that code it all compiles fine... though doesn't allow me to do the preview.

    What would be the correct way of clearing that data so that there is no errors?
  2. seant_unity


    Unity Technologies

    Aug 25, 2015
    So I believe this is happening because Destroy() queues the playable/graph to be destroyed, but the process to do the actual destruction isn't being run before the reload occurs.

    PlayableDirector has an internal method (ProcessPendingPlayableGraphs) that kicks the playable processing, but it's internal. Internally PlayableDirector calls this when it destroys the graph (similar to DestroyImmediate).

    A few things to try
    1) Use AssemblyReloadEvents.beforeAssemblyReload callback to call the destroy. That _might_ work.
    2) Use a custom playable asset and playable director instead of a monobehaviour to manage the graph, similar to how a timeline uses it.
    3) Call the ProcessPendingPlayableGraphs using reflection. This might require the use of a temporary playabledirector.

    Admittedly, none of these are good solutions, so I'd also recommend filing a bug on the issue.