Here is another version for those interested.
-
Color controls in attribute constructor
-
More attribute constructor options
-
Empty AnimationCurves are avoided
-
Raises a System.Exception if the attribute is used on a property which isn’t an AnimationCurve.
-
Place the combined script anywhere you like and it will still work because of compiler flags. Otherwise, place the Editor script in the Editor folder and the other script wherever. Unity suggests this, as it might eventually improve compile times, but I personally prefer code portability and I haven’t noticed any problems yet.
Combined script:
using UnityEngine;
// These compiler flags let you put the script anywhere in your project
#if UNITY_EDITOR
using UnityEditor;
#endif
/// <summary>
/// Forces an AnimationCurve to keep its keys within a certain window. The default is a range of (x0, y0) = (0, 0) to (x1, y1) = (1, 1).
/// </summary>
public class AnimationCurveSimpleAttribute : PropertyAttribute
{
public Rect bbox = new Rect(0f, 0f, 1f, 1f);
public Color color = Color.green;
public AnimationCurveSimpleAttribute() { }
public AnimationCurveSimpleAttribute(Rect bbox)
{
this.bbox = bbox;
}
public AnimationCurveSimpleAttribute(float xmin, float xmax, float ymin, float ymax)
{
bbox = new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
}
public AnimationCurveSimpleAttribute(Color color)
{
bbox = new Rect(0f, 0f, 1f, 1f);
this.color = color;
}
public AnimationCurveSimpleAttribute(float xmin, float xmax, float ymin, float ymax, Color color)
{
bbox = new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
this.color = color;
}
public AnimationCurveSimpleAttribute(Rect bbox, Color color)
{
this.bbox = bbox;
this.color = color;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(AnimationCurveSimpleAttribute))]
public class AnimationCurveSimpleDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
AnimationCurveSimpleAttribute curve = attribute as AnimationCurveSimpleAttribute;
// This attribute should only be used with AnimationCurves
if (property.propertyType != SerializedPropertyType.AnimationCurve)
throw new System.Exception("AnimationCurveSimple attribute can only be used with AnimationCurve properties, not " + property.propertyType);
// Check if the property has an assigned AnimationCurve. Create one if it doesn't.
// If it does, then make sure it has at least 2 keys to avoid empty curves in the inspector.
if (property.animationCurveValue == null ? true : property.animationCurveValue.keys.Length <= 1)
property.animationCurveValue = new AnimationCurve(new Keyframe(curve.bbox.xMin, curve.bbox.center.y), new Keyframe(curve.bbox.xMax, curve.bbox.center.y));
// Draw the curve in the inspector (this is not the popup editor window)
EditorGUI.CurveField(position, property, curve.color, curve.bbox, label);
}
}
#endif
For Editor folder:
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(AnimationCurveSimpleAttribute))]
public class AnimationCurveSimpleDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
AnimationCurveSimpleAttribute curve = attribute as AnimationCurveSimpleAttribute;
// This attribute should only be used with AnimationCurves
if (property.propertyType != SerializedPropertyType.AnimationCurve)
throw new System.Exception("AnimationCurveSimple attribute can only be used with AnimationCurve properties, not " + property.propertyType);
// Check if the property has an assigned AnimationCurve. Create one if it doesn't.
// If it does, then make sure it has at least 2 keys to avoid empty curves in the inspector.
if (property.animationCurveValue == null ? true : property.animationCurveValue.keys.Length <= 1)
property.animationCurveValue = new AnimationCurve(new Keyframe(curve.bbox.xMin, curve.bbox.center.y), new Keyframe(curve.bbox.xMax, curve.bbox.center.y));
// Draw the curve in the inspector (this is not the popup editor window)
EditorGUI.CurveField(position, property, curve.color, curve.bbox, label);
}
}
For anywhere:
using UnityEngine;
/// <summary>
/// Forces an AnimationCurve to keep its keys within a certain window. The default is a range of (x0, y0) = (0, 0) to (x1, y1) = (1, 1).
/// </summary>
public class AnimationCurveSimpleAttribute : PropertyAttribute
{
public Rect bbox = new Rect(0f, 0f, 1f, 1f);
public Color color = Color.green;
public AnimationCurveSimpleAttribute() { }
public AnimationCurveSimpleAttribute(Rect bbox)
{
this.bbox = bbox;
}
public AnimationCurveSimpleAttribute(float xmin, float xmax, float ymin, float ymax)
{
bbox = new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
}
public AnimationCurveSimpleAttribute(Color color)
{
bbox = new Rect(0f, 0f, 1f, 1f);
this.color = color;
}
public AnimationCurveSimpleAttribute(float xmin, float xmax, float ymin, float ymax, Color color)
{
bbox = new Rect(xmin, ymin, xmax - xmin, ymax - ymin);
this.color = color;
}
public AnimationCurveSimpleAttribute(Rect bbox, Color color)
{
this.bbox = bbox;
this.color = color;
}
}