- Home /
Curve fields in editor scripts
EDITED: PROBLEM UPDATED.
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() {
m_Object.Update();
EditorGUILayout.Foldout(true, "Kitchen variables");
EditorGUILayout.PropertyField(m_libKitchenVar);
EditorGUILayout.PropertyField(m_kitchenVar);
EditorGUILayout.PropertyField(m_libKitchenVar);
EditorGUILayout.CurveField(m_animationCurve);
m_Object.ApplyModifiedProperties();
}
}
[1]: http://docs.unity3d.com/Documentation/ScriptReference/EditorGUI.CurveField.html
You don't need to pass anything. GUILayoutOption is just if you want to control width/height etc. of the entry field. See http://docs.unity3d.com/Documentation/ScriptReference/GUILayoutOption.html for more info.
hey, yoyo! I've updated the problem. Thanks so much for your help. Unfortunately, I think the problem is actually also my understanding of serialized properties, so I'm still getting the error above. Any clue what's gone wrong, still?
You can just use SerializedProperty and PropertyField for m_animationCurve as well, no need for CurveField once you have a serialized property.
Perfect. Got it, thanks! Please post your comment as an answer so I can credit you for it. You rule the school, yoyo.
Glad it worked, and answer posted. (Note that you can also up-vote comments ;-)
Answer by yoyo · Dec 18, 2012 at 05:10 AM
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.)
sure sure! I agree about the rena$$anonymous$$g. that was just a quick and dirty solution. like having babies to keep a relationship going. thanks again.
(As a matter of style and caution, I would suggest counselling first, then maybe a dog.)
(As a matter of style and good will, I am giving you a gigantic pixelated Chinese sword. To thank you for your awesome help).
Your answer
Follow this Question
Related Questions
Need help with editor script to copy animation data into individual curves 1 Answer
Why can't I edit animation curves imported from Maya? 1 Answer
AnimationClip.SetCurve not setting a curve... 0 Answers
EDITOR: How to Change Animation Curve Colors 0 Answers
How can I define two-dimensional curves? (like: circle) 1 Answer