Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Is it possible to animate variables in an array in the animation window?

Discussion in 'Animation' started by MagicCancel, Feb 21, 2018.

  1. MagicCancel

    MagicCancel

    Joined:
    Jul 30, 2015
    Posts:
    25
    I have an array of AABB box classes I defined (just has 4 floats in it) and when I try changing its values in the animation timeline it's as if I'm changing it normally, so it can't change with the animation.
     
  2. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I have the same question.

    Obviously it's not possible to add array properties via the Animation window, so I tried to add the property via script like so:

    Code (CSharp):
    1. clip.SetCurve(
    2.     relativePath: string.Empty,
    3.     type: typeof(SplineAnimation),
    4.     propertyName: "attachPoints.Array.data[0].curveParameter",
    5.     curve);
    However, this shows up as a missing property in the animation window, even though the serialized data of the AnimationClip asset looks ok:

    AnimationProperties.PNG

    The SetCurve documentation states that it is possible to animate the object reference values of the materials array in the renderer components. Theoretically, this would mean that is should be possible to do the same thing with other arrays. But in fact, the materials array accessor seems to be some special case in the animation system and doesn't apply for other types of arrays.

    If anyone has more information on this, I would be very interested. Also I would like to know if it would be possible for Unity to add this feature or if there is something completely preventing it from ever working.
     
    CityWizardGames and jeffreyf like this.
  3. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    31
    Hello guys, did you figure out in the end? I am stuck now with the same problem
     
  4. lorewap3

    lorewap3

    Joined:
    Jun 24, 2020
    Posts:
    58
    Bump. I'm here in 2021 trying to find the same answer. I'm trying to animate variables in an array and it won't record anything.

    I can't say I'm too surprised that it doesn't work with a collection of structs, but an array of structs (or any other variable) should be base enough types to work.
     
    CityWizardGames likes this.
  5. unity_kwlDYZY5JApBhg

    unity_kwlDYZY5JApBhg

    Joined:
    Nov 14, 2020
    Posts:
    1
  6. Indie_Core

    Indie_Core

    Joined:
    Jun 14, 2021
    Posts:
    6
    Same problem im learning unity editor and i want my serialized property fields which are assigned to Lists of objects of types like float,int,color,texture2d and the problem is Animation window doesn't see them as properties which i could animate. When its simple type eg. public Color color; only then its exposed in the animation window. How to go about it then?
     
  7. lgarczyn

    lgarczyn

    Joined:
    Nov 23, 2014
    Posts:
    68
    It's impossible as far as I'm aware.

    What is possible is :

    * Hardcode N properties instead of an array.
    * Instead of serializing the array, use N children gameobjects, each with a component. You can use GetComponentsInChildren from the main script to get the array
     
  8. BrianJiang

    BrianJiang

    Joined:
    Jun 11, 2017
    Posts:
    7
    Bump. Is it planned for future? I don't want to hardcode an array
     
  9. Yuchen_Chang

    Yuchen_Chang

    Joined:
    Apr 24, 2020
    Posts:
    130
    There's a way, and may be the current only way to animate classes in array. If you use
    [SerializeReference]
    instead of [SerializeField], you can animate every element in the list/array.
    Here's an example:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. [Serializable]
    7. public class MyRefClass
    8. {
    9.     public int intField;
    10.     public bool boolField;
    11.     public Vector3 v3Field;
    12. }
    13. public class SerializedReferenceArrayAnimationTestScript : MonoBehaviour
    14. {
    15.     [SerializeReference] private List<MyRefClass> myRefClassList; // I think you can't animate a struct
    16.  
    17.     private void Reset()
    18.     {
    19.         myRefClassList = new();
    20.         myRefClassList.Add(new MyRefClass());
    21.         myRefClassList.Add(new MyRefClass());
    22.         myRefClassList.Add(new MyRefClass());
    23.     }
    24. }
    25.  
    If you open Animation window and start Recording, and adjust the field values in MyRefClass in myRefClassList, you could see that it records individual array data into the animation clip.
    However, it's very fragile, so use it carefully.
     
    Last edited: Jul 14, 2023
    zealotand and forestrf like this.