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

Generate curves for float properties in custom behaviour

Discussion in 'Timeline' started by jin-archipin, Apr 7, 2020.

  1. jin-archipin

    jin-archipin

    Joined:
    Dec 17, 2019
    Posts:
    35
    Is there any way I can generate curves for float values in a custom behaviour in script?
    I know I can edit the curves in timeline editor like below, but I can't find how to edit in script of playable asset.
    upload_2020-4-7_12-48-47.png

    Thank you in advance.
     
  2. mikew_unity

    mikew_unity

    Unity Technologies

    Joined:
    Sep 27, 2016
    Posts:
    134
  3. jin-archipin

    jin-archipin

    Joined:
    Dec 17, 2019
    Posts:
    35
    @mikew_unity Um... I think that is showing how to edit the curve in editor, not in script. Am I missing something?

    I want something like

    Code (CSharp):
    1. var curve = new AnimationCurve();
    2. curve.Addkey(0.4, 100);
    3.  
    4. lightControlAsset.SetCurve("Intensity", curve); // Not exist
    or
    Code (CSharp):
    1. lightControlAsset.SetCurve(lightControlAsset.template.intensity, curve); // also not exist
     
    Last edited: Apr 7, 2020
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Here's an example that should help

    Code (CSharp):
    1. public class ExamplePlayableAsset : PlayableAsset
    2. {
    3.     [Serializable]
    4.     public class ExamplePlayableBehaviour : PlayableBehaviour
    5.     {
    6.         public float animatedField;
    7.     }
    8.    
    9.     public ExamplePlayableBehaviour template;
    10.     ...
    11. }
    12.  
    13. void AnimateField(TimelineClip clip)
    14. {
    15.     if (clip.curves == null)
    16.         clip.CreateCurves("AnimatedFields"); // name of the animation clip in Project Window
    17.  
    18.     clip.curves.SetCurve(string.Empty,
    19.         clip.asset.GetType(), // or typeof(ExamplePlayableAsset), i.e. the playable asset type
    20.         "animatedField", // the field of the playable behaviour to animate
    21.         AnimationCurve.Linear(0, 0, (float) clip.duration, 1) // the curve in local time of the timeline clip
    22.     );
    23. }
     
  5. jin-archipin

    jin-archipin

    Joined:
    Dec 17, 2019
    Posts:
    35