- Home /
Custom inspector: uniformly display all instances of a custom class
I had the hardest time summarizing my question, so apologies for the vague title.
Basically I've created a custom class and I'd like to display all instances of it in the inspector. I can do this manually for each instance of the class, but doing so would be painfully redundant...
More specifically, my custom class contains some of the following code:
public class Base {
[System.Serializable]
public class Parameter {
public string uiName;
public string uiDescription;
public float val;
public Parameter (string _uiName, string _uiDescription, float _val) {
uiName = _uiName;
uiDescription = _uiDescription;
val = _val;
}
}
public Parameter parameter = new Parameter("Name", "This is a description", 10f);
}
And in my custom inspector:
Base b = (Base)target; GUIContent parameter = new GUIContent(b.parameter.name, b.parameter.description); b.parameter.value = EditorGUILayout.FloatField(parameter, b.parameter.value);
Is there a particular reason you need a custom inspector? Unity automatically shows any public Serializable class (the parameter variable) as a foldout in the inspector. That is, assu$$anonymous$$g Base is a $$anonymous$$onoBehaviour class...
Answer by frogtamer · Nov 01, 2013 at 11:37 PM
Ok after some more research it looks like PropertyDrawers accomplish what I want to do. I did have to move the Parameter class out of the Base class though.
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Parameter))]
public class ParameterDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
EditorGUI.BeginProperty (position, label, property);
// Label
label.text = property.FindPropertyRelative("uiName").stringValue;
label.tooltip = property.FindPropertyRelative("uiDescription").stringValue;
position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
// Draw fields
Rect valRect = new Rect (position.x, position.y, 50, position.height);
EditorGUI.PropertyField (valRect, property.FindPropertyRelative("val"), GUIContent.none);
EditorGUI.EndProperty ();
}
}
Your answer
Follow this Question
Related Questions
How can I create a list of preset class/objects to be used in the editor 1 Answer
How do you get the default inspector to draw for Prefabs ? 2 Answers
Can a GameObject's full inspector be drawn in a custom Editor Window ? 1 Answer
Editor GUI: Object preview icon 2 Answers
Questions Regarding Images in Custom Inspector/Editor 1 Answer