Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to dynamically set animation key frame value?

Discussion in 'Animation' started by VElysianP, Feb 10, 2019.

  1. VElysianP

    VElysianP

    Joined:
    Jul 24, 2018
    Posts:
    2
    I created animation clips like the image shown below, but I need to revise the Position.y value dynamically by script. Therefore I am wondering if there is a method for me to revise the Position.y value. Thank you very much.
    upload_2019-2-10_17-38-12.png
     
    tonytopper likes this.
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    In the editor, you can use the UnityEditor.AnimationUtility class to get access to the curves of an AnimationClip and modify their keyframes however you like.

    At runtime, you can't. AnimationClip.SetCurve might work, but there is no GetCurve so you can't just modify a single keyframe of an existing curve unless you get it in the editor and serialize it separately from the clip.

    I would just use a script to control the object instead. That way you get full control.
     
  3. VElysianP

    VElysianP

    Joined:
    Jul 24, 2018
    Posts:
    2
    Thanks a lot! I know there are ways to edit the curve, but this is not the function I want. I will switch to controlling the object instead. :)
     
  4. Diesign

    Diesign

    Joined:
    May 15, 2016
    Posts:
    16
    @Kybernetik woah so we can't modify a single keyframe of an animation curve at runtime unless that keyframe has already been set as a variable in the editor?
     
  5. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    Probably not even that. As I said, AnimationClip.SetCurve might work.
     
  6. amoswazana

    amoswazana

    Joined:
    Nov 23, 2015
    Posts:
    5
    If anyone wants I wrote a simple helper class for basic keyframe changing functionality.

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class AnimationCurveChanger : MonoBehaviour {
    5.     public static void UpdateAnimationCurveWithSingleKeyFrame(GameObject animatorGameObject, string animationClipName, string propertyPath, string propertyName, float time, float value) {
    6.         UpdateAnimationCurveWithMultipleKeyFrames(animatorGameObject, animationClipName, propertyPath, propertyName, new float[] { time }, new float[] { value });
    7.     }
    8.  
    9.     public static void UpdateAnimationCurveWithMultipleKeyFrames(GameObject animatorGameObject, string animationClipName, string propertyPath, string propertyName, float[] times, float[] values) {
    10.         // Get all animation clips from animatorGameObject, these are the dropdown menu options in the Animator.
    11.         // For example: when creating a button animator these will be named by default - Normal, Pressed, Highlighted, Disabled
    12.         AnimationClip[] animationClips = AnimationUtility.GetAnimationClips(animatorGameObject);
    13.         for (int i = 0; i < animationClips.Length; i++) {
    14.             // Find the relevant animationClip by name
    15.             if (animationClips[i].name == animationClipName) {
    16.                 // Get all curve bindings, these are the rows in the Animator.
    17.                 // For example: Button : Scale.x
    18.                 EditorCurveBinding[] curveBindings = AnimationUtility.GetCurveBindings(animationClips[i]);
    19.                 for (int j = 0; j < curveBindings.Length; j++) {
    20.                     // Find the relevant curve binding row
    21.                     if (curveBindings[j].path == propertyPath && curveBindings[j].propertyName == propertyName) {
    22.                         // Modify the curve
    23.                         AnimationCurve editorCurve = AnimationUtility.GetEditorCurve(animationClips[i], curveBindings[j]);
    24.                         AnimationCurve newCurve = new AnimationCurve();
    25.                         for (int l = 0; l < times.Length; l++) {
    26.                             newCurve.AddKey(times[l], values[l]); ;
    27.                         }
    28.                         // Set the curve, thus "saving" it to the scene
    29.                         AnimationUtility.SetEditorCurve(animationClips[i], curveBindings[j], newCurve);
    30.                     }
    31.                 }
    32.             }
    33.         }
    34.     }
    35. }
    36.  
    You can call this function like so:

    Code (CSharp):
    1. AnimationCurveChanger.UpdateAnimationCurveWithSingleKeyFrame(RelevantGameObject, "Normal", "cornerIcon", "m_SizeDelta", 0, 20);
     
  7. AAAAAAAAAE

    AAAAAAAAAE

    Joined:
    Jun 8, 2013
    Posts:
    100
    But this uses UnityEditor classes..is this available for runtime .. i mean as app..when run outside of editor ?