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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

[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


    Jul 23, 2018
    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


    Unity Technologies

    Aug 25, 2015
    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):
    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. }
    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.