- Home /
Draw default property types in custom PropertyDrawer
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?
Answer by Darress · Mar 05, 2015 at 11:15 AM
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);
}
Answer by friuns3 · Feb 20, 2013 at 10:50 PM
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.
you need to implement a function that overwrites GetPropertyHeight() so that it is multiplied by the number of elements
Why are you iterating through the property variable? Don't PropertyDrawers only draw one of the object's field/variable?
Your answer
![](https://koobas.hobune.stream/wayback/20220613085350im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
AttributeDrawer: Draw parent property? (the one [AttributeName] is attached to) 0 Answers
Getting CustomPropertyDrawer from SerializedProperty 1 Answer
alternative for header atrribute for enums 1 Answer
Draw custom Property Field in Custom Editor 1 Answer
Editor: How to do PropertyField for List elements? 4 Answers