Last active
June 21, 2022 14:36
-
-
Save shane-harper/e13c4fe23084009f1e62d365424240cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
using UnityEditor; | |
using UnityEngine; | |
public class AnimationCurveCreator : EditorWindow | |
{ | |
private const string Title = "Curve Creator"; | |
[SerializeField] | |
private AnimationCurve _curve = new AnimationCurve(new Keyframe(0, 0, 0, 0), new Keyframe(1, 1, 0, 1)); | |
[SerializeField] | |
private string _output; | |
private static GUIStyle TextBoxStyle => new GUIStyle(EditorStyles.textField) { wordWrap = true }; | |
[MenuItem("Window/" + Title)] | |
public static void Open() | |
{ | |
var window = GetWindow<AnimationCurveCreator>(true, Title); | |
window.Show(); | |
} | |
private void OnGUI() | |
{ | |
using (var check = new EditorGUI.ChangeCheckScope()) | |
{ | |
_curve = EditorGUILayout.CurveField("Animation Curve", _curve); | |
if (check.changed) | |
{ | |
_output = GenerateCode(_curve); | |
Repaint(); | |
} | |
} | |
// Disable generated code | |
EditorGUILayout.SelectableLabel(_output, TextBoxStyle, GUILayout.ExpandHeight(true)); | |
if (GUILayout.Button("Copy")) | |
{ | |
GUIUtility.systemCopyBuffer = _output; | |
} | |
} | |
private static string GenerateCode(AnimationCurve curve) | |
{ | |
var builder = new StringBuilder("new AnimationCurve("); | |
var count = curve.keys.Length; | |
const string keyFormat = "new Keyframe({0}f, {1}f, {2}f, {3}f)"; | |
for (var i = 0; i < count; ++i) | |
{ | |
var key = curve.keys[i]; | |
builder.AppendFormat(keyFormat, key.time, key.value, key.inTangent, key.outTangent); | |
if (i < count - 1) | |
{ | |
builder.Append(", "); | |
} | |
} | |
const string suffix = ");"; | |
builder.Append(suffix); | |
return builder.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment