Not entirely sure where to post this… UI seemed right until I read the stickies.
This is a non-issue really but I’m new to custom editors and none of my attempts to figure this out have worked.
I can ignore this small issue but would prefer not to. So my question is: How do I make a label’s width match its content?
I making a custom editor for a scriptable object. The editor has a Horizontal group that contains 2 controls. An IntField and a TextField, with labels, representing an ID and Name respectively. It looks like this:
![]()
I simply want the fields to hug the labels. In other words, I want the label’s width to match its content.
Setting GUILayout.ExpandWidth(false) only seems to affect the field’s width and not the width of the label.
Using PrefixLabel breaks drag-to-change value functionality. I don’t “Need” that functionality but it’d be nice to keep. However, a prefix takes in a GUIStyle anyway, and not GUILayoutOptions. So I can’t even use ExpandWidth(false) here. Naturally I tried to make a new guiStyle to get around this but that didn’t work as intended either. Lastly, I tried a skin with my style and setting GUI.skin to my skin and so on but isn’t this is overkill? Regardless that didn’t work either. So I’m either missing something or doing it wrong.
myScriptableObj so;
//GUIStyle myStyle;
public void OnEnable() {
so = (myScriptableObj)target;
}
//public void OnGUI() {
//myStyle = new GUIStyle(GUI.skin.label);
//myStyle.margin = new RectOffset(someRect);
//myStyle.padding = new RectOffset(someRect);
}
public override void OnInspectorGUI() {
EditorGUILayout.BeginHorizontal();
//EditorGUILayout.PrefixLabel("#", myStyle);
so.objID = EditorGUILayout.IntField("#", so.objID, GUILayout.ExpandWidth(false));
//EditorGUILayout.PrefixLabel("Name", myStyle);
so.objName = EditorGUILayout.TextField("Name", so.objName, GUILayout.ExpandWidth(false));
EditorGUILayout.EndHorizontal();
}
Any insight would be appreciated. Is GUISkin the right appraoch? What about a Content Fitter? How do I even create one in a custom editor if applicable here?