Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#Unity3D Output all the AnimationCurves found on the current selection to the console, nicely formatted so they can simply be copy/pasted into your script! Introspection, baby!
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 (tr.name);
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 += " });";
Debug.Log(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment