Search Unity

Animating properties on an Animation Track?

Discussion in 'Timeline' started by Aaron-Meyers, Jan 30, 2020.

  1. Aaron-Meyers

    Aaron-Meyers

    Joined:
    Dec 8, 2009
    Posts:
    305
    I frequently use C# properties in my scripts that will automatically set several fields based on a macro value, but since properties don't get serialized, it doesn't seem like they can be animated on an animation track. Is there any secrets or tips that would help with this?

    The best thing I can come up with at the moment is have a serialized field that is used to set the property in every tick of the update and then throw [ExecuteInEditMode] on the MonoBehaviour so I can scrub the timeline and see it all working in edit mode.
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Only serialized properties can be animated using the animation system, which is what the animation system.

    The only other option is to create a custom clip that uses an animated playable behaviour parameter that writes to the property, like in the following example.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Playables;
    5.  
    6.  
    7. [System.Serializable]
    8. public class ScreenTextBehaviour : PlayableBehaviour
    9. {
    10.     public string text = "Sample Text";
    11.     public Color vertexColor = Color.white; // This will be animated by timeline
    12.    
    13.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    14.     {
    15.         var target = playerData as SomeTextType;
    16.         if (target == null)
    17.             return;
    18.            
    19.         target.color = vertexColor;
    20.         target.text = text;      
    21.     }
    22. }
    23.  
    24. [System.Serializable]
    25. public class ScreenTextClip : PlayableAsset
    26. {
    27.     public ScreenTextBehaviour template = new ScreenTextBehaviour();
    28.  
    29.     // Factory method that generates a playable based on this asset
    30.     public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
    31.     {
    32.         return ScriptPlayable<ScreenTextBehaviour>.Create(graph, template);
    33.     }
    34. }
     
    tonytopper likes this.
  3. tonytopper

    tonytopper

    Joined:
    Jun 25, 2018
    Posts:
    226
    Are there any more complete examples of using PlayableBehaviour and PlayableAsset floating around?

    Preferably one that includes assigning animated values to MonoBehaviour Script component properties.