- Home /
Unity Property Drawer - Making more space
I have a problem creating my custom GUI layout. I don't know how to make more space for my labels so they don't get on each other like here
I saw this post but it didn't help me. http://answers.unity3d.com/questions/542901/property-drawer-and-children.html
My code for Property Drawer is here:
using UnityEngine; using UnityEditor; using System.Collections;
[CustomPropertyDrawer(typeof(Level))] public class LevelType : PropertyDrawer {
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
EditorGUIUtility.LookLikeControls();
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
Rect w1 = new Rect(position.x, position.y, 50, position.height);
Rect w2 = new Rect(position.x+60, position.y, 25, position.height);
EditorGUI.LabelField(w1, "Health", GUIStyle.none);
EditorGUI.PropertyField(w2, property.FindPropertyRelative ("Balls"), GUIContent.none);
Rect w3 = new Rect(position.x, position.y+30, 50, position.height);
Rect w4 = new Rect(position.x+60, position.y+30, 25, position.height);
EditorGUI.LabelField(w3, "Mana", GUIStyle.none);
EditorGUI.PropertyField(w4, property.FindPropertyRelative ("Balls"), GUIContent.none);
EditorGUI.EndProperty();
}
}
Answer by GameVortex · Jan 27, 2015 at 09:58 PM
You need to overwrite the GetPropertyHeight function and return the height you want it to use.
What do you mean? I linked you the scripting documentation on GetPropertyHeight. What specifically do you have problems figuring out?
I just didn't know how to write it in code, this was a problem. But after some searches I've managed to do it right. Thanks!
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 50.0f;
}
Answer by toomasio · Apr 20, 2018 at 06:59 AM
Some code to help with this. Kinda a hack way of doing it but it works.
//need to set field amount manually if you add more fields
private int fieldAmount = 4;
private float fieldSize = 16;
private float padding = 2;
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
//set the height of the drawer by the field size and padding
return (fieldSize * fieldAmount)+(padding * fieldAmount);
}
// Draw the property inside the given rect
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
//divide all field heights by the field amount..then minus the padding
position.height /= fieldAmount; position.height -= padding;
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
//first field of the drawer
EditorGUI.PropertyField(position,data);
position.y += fieldSize + padding; //offset position.y by field size
savedData.id = EditorGUI.IntField(position, savedData.id);
position.y += fieldSize + padding;
savedData.id = EditorGUI.IntField(position, savedData.id);
position.y += fieldSize + padding;
savedData.id = EditorGUI.IntField(position, savedData.id);
EditorGUI.EndProperty();
}
Your answer
Follow this Question
Related Questions
Material doesn't have a color property '_Color' 4 Answers
property drawer for enum + two other attributes 2 Answers
How to create Prefab with property 2 Answers
Using SerializedObject in a build 1 Answer