Search Unity

[SOLVED] How to restore state of the game object modified by script playable

Discussion in 'Timeline' started by palex-nx, May 12, 2020.

  1. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Hello everyone!
    I use script playables and signals to modify some game objects and their related scripts properties, like activeSelf, color, position, etc. How do I restore the original values when time line is stopped or restarted or closed in edit mode? Thanks in advance!

    P.S. Im about to use command pattern with undo method and commands stack for this, but I hope there's more elegant builtin solution?
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    On your custom playable asset, implement IPropertyPreview. The 'driver' will let you put serializable fields into 'Preview', which means their values will be automatically reverted when Timeline is done previewing, and any changes to those values won't be saved with the scene.

    The downside is you need to know the serialized name of the fields, which isn't obvious for Unity built-in component fields. The easiest way to find these is to look in the scene file (in text mode).

    For example, if your clip modifies the position field of the Transform component, you can implement GatherProperties as follows.


    Code (CSharp):
    1.      
    2. public void GatherProperties(PlayableDirector director, IPropertyCollector driver)
    3. {
    4.     driver.AddFromName<Transform>("m_LocalPosition.x");
    5.     driver.AddFromName<Transform>("m_LocalPosition.y");
    6.     driver.AddFromName<Transform>("m_LocalPosition.z");
    7. }
    8.  
    When using it on the clip, this version will assume you are using the gameObject bound to the track. Otherwise, the IPropertyCollector has overloads that take an explicit gameObject.

    You can also do this at the track level (GatherProperties is implemented in TrackAsset, you would need to override it). In that case, you need to supply the gameObject to driver.AddFromName();
     
    palex-nx likes this.