Good morning fair programmers!
I’m building a PropertyDrawer for an object I would like to expose as a List<>
in the inspector, I’m mostly there but I can’t find a way for the list to automatically add what’s generally Element 0, Element 1, … in fact that area comes out empty:
and adding the label just shifts everything down:
I did try a few things such as adding a name
member as other posts seem to suggest, but no victory over there.
Here’s the whole thing:
public class AmbiancePad : MonoBehaviour
{
...
public List<SoundFilterControl> filters = new List<SoundFilterControl>();
...
}
[CustomPropertyDrawer(typeof(SoundFilterControl))]
class SoundFilterControlDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
float margin = 12;
int lines = 3;
SerializedProperty controlMode = property.FindPropertyRelative("controlMode");
if (controlMode.enumValueIndex == (int)SoundFilterControlMode.Distance)
{
lines += 4;
}
else
{
lines += 6;
}
SerializedProperty customCurveMode = property.FindPropertyRelative("curveMode");
if (customCurveMode.enumValueIndex == (int) SoundFilterCurveMode.Custom)
{
lines += 1;
}
return EditorGUIUtility.singleLineHeight * lines + margin;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
SerializedProperty effect = AppendProperty(ref position, property, "filter");
SerializedProperty controlMode = AppendProperty(ref position, property, "controlMode");
if (controlMode.enumValueIndex == (int) SoundFilterControlMode.Distance)
{
AppendProperty(ref position, property, "distanceFrom");
AppendProperty(ref position, property, "distanceMin");
AppendProperty(ref position, property, "distanceMax");
}
else
{
AppendProperty(ref position, property, "controlParameters");
AppendProperty(ref position, property, "controlParameter");
AppendProperty(ref position, property, "effectParameter");
AppendProperty(ref position, property, "effectControlRangeMin");
AppendProperty(ref position, property, "effectControlRangeMax");
}
SerializedProperty curveMode = AppendProperty(ref position, property, "curveMode");
if (curveMode.enumValueIndex == (int)SoundFilterCurveMode.Custom)
{
AppendProperty(ref position, property, "customCurve");
}
EditorGUI.EndProperty();
}
private SerializedProperty AppendProperty(ref Rect position, SerializedProperty property, string propertyRelativeName)
{
Rect propertyRect = EditorUtils.NextLineRect(ref position);
SerializedProperty relativeProperty = property.FindPropertyRelative(propertyRelativeName);
EditorGUI.PropertyField(propertyRect, relativeProperty);
return relativeProperty;
}
}
Thanks!