AnimationCurve在Unity中和进行复制?
保存为模板的方法
如果项目中需要使用曲线的方式比较多时,有可能有些曲线只是临时性的,我们也要这要操作,便捷性不高,对于曲线模板的可以这样使用。
使用PropertyDrawer
using UnityEngine;
using UnityEditor;
namespace YU.Art
{
[CustomPropertyDrawer(typeof(AnimationCurve))]
public class AnimationCurvePropertyDrawer : PropertyDrawer
{
static AnimationCurve buffer;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 16;
}
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
Event e = Event.current;
rect.height = 16;
if (e.type == EventType.MouseDown && e.button == 1 && rect.Contains(e.mousePosition))
{
GenericMenu context = new GenericMenu();
context.AddItem(new GUIContent("Copy"), false, () => { buffer = property.animationCurveValue; });
context.AddItem(new GUIContent("Paste"), false, () =>
{
if (buffer == null) return;
property.animationCurveValue = new AnimationCurve(buffer.keys);
property.animationCurveValue.preWrapMode = buffer.preWrapMode;
property.animationCurveValue.postWrapMode = buffer.postWrapMode;
property.serializedObject.ApplyModifiedProperties();
});
context.ShowAsContext();
e.Use();
}
EditorGUI.PropertyField(rect, property);
}
}
}