- Home /
EditorGUI PrefixLabel for multiple property fields.
Hello,
For my current project I've defined a very simple IntVector2 class.
Until now I've been using an also pretty simple custom inspector to draw its two properties, each on a separate line.
But now with even more custom inspector coolness, the script's inspector is getting crowded.
I've been following Jasper Flick's awesome tutorial to try and get them on a single line.
I did get the properties to display on a single line, but then, it's not really following the inspector's original behavior:
Also, it's only when the first property has changed that the prefix label gets to be bold.. In my case it means that if the Y property of my IntVector2 is modified, the prefix label is not displayed in bold.
and the same behavior can be observed when importing Jasper Flick's demo package, if the color property is changed,
then the prefix label is not bold.
So my questions would be:
maybe I'm not asking the right question?
Screenshot for issue #1 (weird selection):
Screenshot for issue #2 (bolding behavior):
I'm using Unity Free 4.3.4f1
and here is the code of my IntVector2 PropertyDrawer's OnGUI method ( heavily based on Jasper Flick's tutorial)
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
label = EditorGUI.BeginProperty(position, label, property);
var contentPosition = EditorGUI.PrefixLabel(position, label);
EditorGUIUtility.labelWidth = 14f;
contentPosition.width /= 2f;
EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative(@"x"));
contentPosition.x += contentPosition.width;
EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative(@"y"));
EditorGUI.EndProperty();
}
I'll end this with a big thank you for your time reading all this, and to Jasper Flick for his tutorial :)
Answer by scanzy · Apr 21, 2017 at 02:19 AM
Hey, I found some solutions:
1) How can I tell a prefix label to not select the next control ?
from the docs (https://docs.unity3d.com/ScriptReference/PropertyDrawer.html)
Rect content = EditorGUI.PrefixLabel(rect, GUIUtility.GetControlID(FocusType.Passive), label);
This would not let you select the label (it won't become blue if you click it)
2) Is there a way to get a prefix label so it would be bold if any of the property fields have changed ?
from the docs: (https://docs.unity3d.com/ScriptReference/EditorGUI.BeginProperty.html)
BeginProperty and EndProperty automatically handle default labels, bold font for prefab overrides, revert to prefab right click menu, and setting showMixedValue to true if the values of the property are different when multi-object editing.
So we only need to use:
label = EditorGUI.BeginProperty(rect, label, property);
//prefix label here
//fields here
EditorGUI.EndProperty();
You already implemented this in your code, I think this is working now because they fixed bugs.
Sometimes the docs can hide some small details that can be got only reading a lot of times, sometimes we just need to wait for bugs to be fixed... I hope they could share some source code with the community so we could have the possibility to better understand all this and work better.
Your answer
Follow this Question
Related Questions
Losing Class References 0 Answers
Showing the editors of list elements 0 Answers
Saving changes to mixed CustomInspector 1 Answer
How to choose the transform tool from script ? 4 Answers
Custom editor: How to initialise a new array element? 1 Answer