- Home /
Can I use custom inspector widgets, such as a slider for a floating point property, without writing a custom editor?
Suppose I have a class with 10 public properties, which all happen to be floats. If I want one of them controlled by a slider, is there a code attribute I can add to do so, or do I have to write a custom editor that knows about all 10 properties and implements text boxes for nine of them and a slider for the other one?
I suspect I need a custom editor, which is unfortunate. I browsed all the answers related to "inspector", and this was the closest I found:
http://answers.unity3d.com/questions/18198/whats-attribute-to-limit-range-of-serialized-variable
Many thanks.
EDIT: As of Unity 4, the PropertyDrawer class enables precisely what I was looking for when I asked this question.
Answer by skovacs1 · Oct 14, 2010 at 03:12 PM
I believe that you are correct in your assumption that a CustomEditor is the solution. An attribute would be a great way to do this, but that's not the way the code was written. I'm surprised how few questions there are on CustomEditor, but they aren't that hard.
If you check the docs on Editor, there is a good js example of a CustomEditor - target
is the object being inspected and it is of the type specified as the type your CustomEditor is for. To do it in another language isn't that much different. The different built-in GUI interfaces that you can use can be found in EditorGUILayout.
Thanks, that's what I thought. I've created custom editors, I agree they aren't that hard -- though on a complex object (lists, custom structs, etc.) they can get tricky. It's a shame there's not a way to override the widget used for a single property, rather than having to override the editor for the entire component.
Answer by sotirosn · Nov 21, 2013 at 02:16 AM
I think you can just use the range attribute.
class MyClass : MonoBehaviour {
[Range(0, 1)]
public float sliderValue;
public float otherValue;
}
This does just what I was looking for, and is very simple! Thanks!
Answer by yoyo · Nov 15, 2010 at 08:28 PM
This isn't precisely what I want, but useful enough to call out. The DrawDefaultInspector() method does just what it says, which is useful if you want to add a control or two to the normal editor. You can add a slider for one of the 10 attributes and keep the standard textbox control as well.
http://unity3d.com/support/documentation/ScriptReference/Editor.DrawDefaultInspector.html
If you want the default widgets available just as an "advanced" feature of your editor, this works nicely (_foldoutDefaultInspector is a bool member variable of the editor):
_foldoutDefaultInspector = EditorGUILayout.Foldout(_foldoutDefaultInspector, "Advanced");
if (_foldoutDefaultInspector)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Space(12);
DrawDefaultInspector();
EditorGUILayout.EndHorizontal();
}
maybe use EditorGUIUtility.singleLineHeight ins$$anonymous$$d of 12?