Curve fields in editor scripts


Hey, so, I’ve made a script that causes my “LibraryKitchen” script to display in a fun way. But I’m having trouble getting it to display animation curves. My problem is that apparently animation curves cannot be serialized properties, and I don’t know how to let my editor script find it. 've looked at the unity script reference, but it just has the example in javascript. Any idea how to do it in c#?

The line in question is towards the end:

m_animationCurve = m_Object.FindProperty("_curve");

And the error I’m currently getting is:

Assets/Editor/OptionalEditorScript.cs(18,17): error CS0029: Cannot implicitly convert type `UnityEditor.SerializedProperty' to `UnityEngine.AnimationCurve'

What’s my problem? do you know?

    using UnityEngine;
using System.Collections;
using UnityEditor;

[CustomEditor(typeof (LibraryKitchen))]
public class OptionalEditorScript : Editor {
	private SerializedObject m_Object;
	private SerializedProperty m_kitchenVar;
	private SerializedProperty m_libKitchenVar;
	private AnimationCurve m_animationCurve;
	public void OnEnable() {
		m_Object = new SerializedObject(target);
		m_kitchenVar = m_Object.FindProperty("_kitchenVar");
		m_libKitchenVar = m_Object.FindProperty("_libKitchenVar");
		m_animationCurve = m_Object.FindProperty("_curve");
	public override void OnInspectorGUI() {
		EditorGUILayout.Foldout(true, "Kitchen variables");

You can either use EditorGUILayout.CurveField to edit an AnimationCurve, or use EditorGUILayout.PropertyField to edit a SerializedProperty (for any property type, including animation curve).

The code in your answer is mixing these up, trying to assign a SerializedProperty to an AnimationCurve variable and then pass it to a CurveField call.

The simplest fix is to make m_animationCurve a SerializedProperty and then use EditorGUILayout.PropertyField to edit it.

(BTW, as a matter of style and clarity, I would suggest renaming OptionalEditorScript to LibraryKitchenEditor, to match the component it is editing.)