How to access AnimationCurve at runtime

Unity introduced AnimationCurve as a runtime class. But there is no doc or any sample on accessing it while the game is running. I'll be greatful if someone can to me how to do. Thanks.

4 Answers

4

You can't get AnimationCurves from an AnimationClip. Strangely/sadly you can only set or add curves.

You'll have to keep track of any curves you create procedurally, in a dictionary for instance.

Yes, now I saved the AnimationClipCurveData to disk and created a curve at runtime with it.

I wish i knew to well what i do is duplicate the controller usually fixes its self but how do you fix it with out doing that i do not know i thinks its a bug maybe.

What way do you intend to use it?

Here's an example usage for animating a light turn on through Evaluate. You edit the AnimationCurve in the inspector. You can also give it some simple default value through Linear and EaseInOut.

var intensity : AnimationCurve;

yield AnimateIntensity();

function AnimateIntensity() {       
    var duration = intensity[intensity.length - 1].time;
    var timer = 0.0f;
    while (timer < duration) {
        timer = Mathf.MoveTowards(timer, duration, Time.deltaTime);
        light.intensity = intensity.Evaluate(timer);
        yield;
    }
}

I can process the curves in a editor script to meet my requirement. I just intend to know more about the class. It's a runtime class but can't be accessed at runtime, very strange.

I mean "Access" as obtaining it from a clip at runtime, not just assigning it in inspector as a static reference.

If you want to edit at runtime an animation curve,check this out:
Runtime Curve Editor | GUI Tools | Unity Asset Store (new version available ! )

AnimationCurve curveX = AnimationUtility.GetEditorCurve(ac, “Bip01”, typeof(Transform), “m_LocalPosition.x”);
it’s ok in editor mode.