There’s an Edit at the bottom of post:
I’m working on a custom Editor for a script. I have begun a horizontal layout using:
GUILayout.BeginHorizontal();
Now I want to space my elements out nicely, so I tried using GUILayout.Width()
on the elements. The following below works fine (when I enter the values manually and not use variables):
case SerializedPropertyType.Float:
isFloat = true;
goto case SerializedPropertyType.Integer;
case SerializedPropertyType.Integer:
GUILayout.BeginHorizontal ();
GUILayout.Label (name, GUILayout.Width(200));
if (isFloat) {
brush_sp.floatValue = GUILayout.HorizontalSlider (brush_sp.floatValue, slide.min, slide.max, GUILayout.Width(130));
brush_sp.floatValue = EditorGUILayout.FloatField (brush_sp.floatValue, GUILayout.Width(70));
} else {
brush_sp.intValue = Mathf.RoundToInt (GUILayout.HorizontalSlider (brush_sp.intValue, slide.min, slide.max));
brush_sp.intValue = EditorGUILayout.IntField (brush_sp.intValue);
}
GUILayout.EndHorizontal();
break;
The code above results in the following:
Obviously though this doesn’t change with the inspector width, so I made the following changes:
public float width
{
get { return EditorGUILayout.GetControlRect (GUILayout.Height(0)).width;}
}
And
case SerializedPropertyType.Float:
isFloat = true;
goto case SerializedPropertyType.Integer;
case SerializedPropertyType.Integer:
float w = width;
GUILayout.BeginHorizontal ();
float label_width = Mathf.Floor(w * 0.5f);
float slider_width = Mathf.Floor(label_width * 0.65f);
float field_width = Mathf.Floor(label_width - slider_width);
GUILayout.Label (name, GUILayout.Width(label_width));
if (isFloat) {
brush_sp.floatValue = GUILayout.HorizontalSlider (brush_sp.floatValue, slide.min, slide.max, GUILayout.Width(slider_width));
brush_sp.floatValue = EditorGUILayout.FloatField (brush_sp.floatValue, GUILayout.Width(field_width));
} else {
// Not Implemented
}
GUILayout.EndHorizontal();
break;
Only change was getting the width and using it for the sizes instead of hard-coding them, and it completely messes them up, I have even used labels to display the value of w and the 3 width variables and even when all the values are exactly identical it doesn’t work and I get this instead:
I have spent several hours trying to fix this problem and nothing changes the result, even just replaying 1 of the elements with a variable width instead of manually entering the number messes it up. Does anyone have an idea what’s wrong?
Edit: I decided to stop using the supplied Layout classes, and wrote my own manager for simplifying the use of Rects with the standard GUI and EditorGUI classes. I am having no problems with this anymore. I still am however very confused how calling the Width function with a variable made it not work, while typing the value in manually did, I even did an equality check to ensure the numbers were actually equal. I may keep this open encase anyone knows a solution to the problem still, if not to help me at least to help anyone else who may run into this problem. Feel free to close this question if you are a moderator and feel it shouldn’t stay open.