Unity: Log AnimationCurves

I’ve grown to love AnimationCurves! I use them for all sorts of things and go to great lengths to tweak them to perfection.

But once they’re perfect, how do I get that perfect curve out of the curve editor and into the script, so that whenever I use this script in another project it will hold the same perfect curve?

Well, I’ve written a little editor-script that looks at all the scripts on your currently selected gameObjects and outputs all AnimationCurves it can find to the console, nicely formatted so you can simply copy/paste them into your script.

And bam! Perfection by default!

using UnityEngine;
using UnityEditor;
using System;
using System.Collections;

public class AnimationCurveOutput : EditorWindow {

    [MenuItem("Assets/Log AnimationCurves")]
    public static void LogAnimCurves() {
		//Get selection
		Transform[] trs = Selection.GetTransforms (SelectionMode.Deep | SelectionMode.DeepAssets);
		//Look for AnimationCurves
		foreach (Transform tr in trs) {
            Component[] components = tr.GetComponents<Component>();
            for (int i = 0; i < components.Length; i++) {
                Component c = components[i];
                if (c != null) {
                    Type t = c.GetType();

                    System.Reflection.FieldInfo[] fieldInfo = t.GetFields();
                    foreach (System.Reflection.FieldInfo info in fieldInfo) {
						if(info.FieldType == typeof(AnimationCurve)) {
							Debug.Log (;
							LogAnimationCurve((AnimationCurve) info.GetValue(c));

	static void LogAnimationCurve(AnimationCurve a) { LogAnimationCurve(a, false); }
	static void LogAnimationCurve(AnimationCurve a, bool square) {
		string str = "new AnimationCurve(new Keyframe[] { ";
		int count = 1;
		foreach(Keyframe k in a.keys) {
			str += "new Keyframe(" + (k.time * (square ? k.time : 1f)) + "f, " + k.value + "f, " + k.inTangent + "f, " + k.outTangent + "f)";
			if(count < a.keys.Length) str += ", ";
			else str += " ";
		str += " });";

Or check it out in Gist form on GitHub!

Bookmark the permalink. Follow any comments here with the RSS feed for this post. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>