- Home /
Issue with property drawer control position in inspector
I am creating a custom property drawer. I want the label for my property , but instead of drawing the control for my property I want to draw a popup with string choices. Now this is all fine and is drawn sorta right. Now my problem is whenever I resize the inspector the width of my popup will change and my x is changing. Now they should be changing like the rest of the controls do, but they are changing wrong. They are becoming too wide and moving too far to the left, while the label stays in the proper spot. Anyone know how to fix it so it stays the right width and x position? I want it the same as the enums variables below it.
Here is my code:
[CustomPropertyDrawer(typeof(StringVerEnum))]
public class StringVerEnumDrawer : PropertyDrawer
{
string[] options = { "Item1", "Item2", "Item3", "Item4", "Item5™" };
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
int choice = property.enumValueIndex;
GUIContent LabelText= new GUIContent("My Label","My tooltip");
EditorGUI.PrefixLabel(position, LabelText);
var PopupPosition = new Rect(position.x + (position.x/0.1f), position.y , position.width -(position.width/3f) , position.height);
EditorGUI.BeginChangeCheck();
choice = EditorGUI.Popup(PopupPosition, choice, options);
if (EditorGUI.EndChangeCheck())
{
property.enumValueIndex = choice;
}
}
}
StringVerEnum is an attribute that I am using for the variable/property that is using this property drawer, incase that somehow matters. Now it is also entirely possible my position change values are just wrong, I'm not entirely sure. I do know when I use position it is the rect for the label and the control. It would be nice if there was someway to get the rect for just the label and just the control to try to make sure that everything is in the right place , but haven't been able to find anything in the docs that does that. Any help with getting my control/popup to always be in the right position is greatly appreciated.
Answer by arisonu123 · Aug 06, 2017 at 07:43 PM
So I found the answer. There is an overload of the popup function that allows me to pass the position with a label. This function appears to automatically calculate the space between the label and the control and puts the label in front of the control in the proper spot. New code:
[CustomPropertyDrawer(typeof(StringVerEnum))]
public class StringVerEnumDrawer : PropertyDrawer
{
GUIContent[] options = { new GUIContent("Item1"), new GUIContent("Item2"), new GUIContent("Item3"), new GUIContent("ItemTest"), new GUIContent("Item5™")};
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
int choice = property.enumValueIndex;
GUIContent LabelText = new GUIContent("My Label", "My tooltip");
EditorGUI.BeginChangeCheck();
choice = EditorGUI.Popup(position,LabelText,choice,options);
if (EditorGUI.EndChangeCheck())
{
property.enumValueIndex = choice;
}
}
}
Your answer
Follow this Question
Related Questions
Drawing Order of DecoratorDrawer doesn't work? 1 Answer
Why PropertyDrawers shared same values? 2 Answers
PropertyDrawer let's dissapear my INT 0 Answers
How to prevent overlap when using nested arrays in CustomPropertyDrawers? 1 Answer
Unity won't even let me draw my interface property drawer? WTF?!?! Y U No support interfaces???? 1 Answer