If I made a custom attribute that I wish would be applied to all properties, is there a way I can make it draw the respective EditorGUI field?
Right now, I would have to go through each property like so;
if (property.propertyType == SerializedPropertyType.Float)
EditorGUI.Slider (position, property, range.min, range.max, label);
else if (property.propertyType == SerializedPropertyType.Integer)
EditorGUI.IntSlider (position, property, range.min, range.max, label);
else
EditorGUI.LabelField (position, label.text, "Use Range with float or int.");
That would be pretty inconvenient to have to write all that. Any way around this?
2 Answers
2
This is an old thread, but here is the solution.
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.PropertyField(position, property, label, true);
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property);
}
If you want to expand it’s functionality with eg. a button:
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.PropertyField(position, property, label, true);
if (property.isExpanded)
{
if (GUI.Button(new Rect(position.xMin + 30f, position.yMax - 20f, position.width - 30f, 20f), "button"))
{
// do things
}
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
if (property.isExpanded)
return EditorGUI.GetPropertyHeight(property) + 20f;
return EditorGUI.GetPropertyHeight(property);
}
friuns3
2
foreach (SerializedProperty a in property)
{
position.y += 20;
EditorGUI.PropertyField(position, a, new GUIContent(a.name));
}
I tried your solution but ended up with fields overwriting each other.
– AmbroseFurbackyou need to implement a function that overwrites GetPropertyHeight() so that it is multiplied by the number of elements
– gardian06Why are you iterating through the property variable? Don't PropertyDrawers only draw one of the object's field/variable?
– AnomalusUndrdog