Using AnimationCurve in List. or Builtin Arrays

Hi everyone, I have this little trouble, using the AnimationCurve in builtin arrays don’t keep the modified values, if I try to retrieve the values of any curve the result are like if I put this(AnimationCurve.Linear(0,0,1,1)) every time, my question is: How can I keep those values even if I change from one scene to another?, I ask this because I’m trying to use a multiple animation curve to manipulate some values over time using the “CustomEditor” to make an Editor Tool.

The idea is to use something like this:

var theCurve : AnimationCurve[]= new AnimationCurve[5];

for (i = 0; i < theCurve.length; i ++) {
    theCurve _= EditorGUILayout.CurveField("Some Name", theCurve*);*_

}
Any help will be very appreciated.
Thanks a lot to eveyone who can help me.

Hi every one, I found a solution very interesting about this, the solution is:

// This will be "Main Script".
var animationContainer = new List.<AnimationParameters>();
var howManyAnimations = int;

function Update () {
    if (howManyAnimations > animationContainer)
        animationContainer.Add(AnimationParameters);
}

// This will be "AnimationParameters" script.

class AnimationParameters {
    var curveAnim : AnimationCurve;
}

This way you can use multiple animation curves and every curve keeps the values without loosing them if you change from one scene to another.

You can check this to see how it’s applied. :wink:

Where is "theCurve" declared? On a script attached to a GameObject? In this case make sure you don't override the array somewhere. You should use the Reset function to initialise the array. Reset is only called once when the script instance is attached or when you rightclick the component and select "Reset".

If you declared the array in a custom editor / inspector class that wouldn't be a good idea. This class instances are recreated everytime it is reopened. These classes aren't meant for saving static data. Save them to an asset that is serialized.

Some more information what you're actually trying to accomplish would help.

Do you create a custom inspector for a script of yours or a general EditorWindow like the animation window? Where is the data stored?

edit

Ok since Unity can't save AnimationCurves you have to use an AnimationClip that holds the curves.

Normally when assigning AnimationCurves to an AnimationClip you have to specify a relative transform path, a component and a property that is animated by this animation curve. I just want to store the curves and don't care about the clip itself. To be able to store multiple clips i just used the array index as path-string.. xD

function OnInspectorGUI()
{
    if (Target.test == null)
    {
        Debug.Log("Created Clip");
        Target.test = new AnimationClip();
    }
    var curves = AnimationUtility.GetAllCurves(Target.test);
    var CC = new AnimationCurve[5];
    for (i = 0; i < curves.length; i ++)
    {
        CC _= curves*.curve;*_
 _*}*_
 _*for (i = 0; i < CC.length; i ++)*_
 _*{*_
 <em>_if (CC *== null)*_</em>
 <em>_*{*_</em>
 <em>_*Debug.Log("Created curve");*_</em>
 <em><em>_CC *= AnimationCurve.Linear(0,0,1,1);*_</em></em>
 <em><em>_*}*_</em></em>
 <em><em><em><em>CC _= EditorGUILayout.CurveField("Some Name", CC*);*_</em></em></em></em>
 <em><em><em><em><em>_Target.test.SetCurve("Name"+i,Transform,"",CC*);*_</em></em></em></em></em>
 <em><em><em><em><em>_*}*_</em></em></em></em></em>
<em><em><em><em><em>_*}*_</em></em></em></em></em>
<em><em><em><em><em>_*```*_</em></em></em></em></em>
<em><em><em><em><em>_*<p>You should read the manual to understand <a href="http://unity3d.com/support/documentation/ScriptReference/AnimationUtility.GetAllCurves.html">AnimationUtility.GetAllCurves</a> and <a href="http://unity3d.com/support/documentation/ScriptReference/AnimationClip.SetCurve.html">SetCurves</a></p>*_</em></em></em></em></em>
<em><em><em><em><em>_*<p>It's not really nice but the only alternative is to save the keyframe data "manually" with types that are serializable.</p>*_</em></em></em></em></em>