Custom Editor GUILayout.Width isn't working when using variables, but works when not

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:
107483-img-unity-issue.png

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:
107484-img-unity-issue2.png

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.

A little late I know, but I’ve just run into this same problem, and maybe someone else finds this helpful. I can only guess why it’s not working with variables, sorry, I supose they don´t want the component width to be driven by a parameter that can change it’s value over time, so instead, they want us to use the correct options. Even if it’s not that way, what I’ve done for not having to write my own implementation for managing the Rects (btw, congrats for that!!!), is to set the MinWidth for the elements and then use GUILayout.ExpandWidth(true), someting like :

GUILayout.BeginHorizontal(GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true));

You just have to use the same min width in all your elements if you want them to be equal.
If not, use a number like 1 or 10 or 100 something like that to stablish the standard and asing the others the % difference. For exaple if you want other to be half that, just asign 0.5, 5 or 50 to its min width.

On the other hand, it’s very important to have all your elements inside the correct “Vertical” and “Horizontal” spaces. I’ve seen inside your “Horizontals” you don´t use any “Vertical” for the columns, if you want them to match sizes, this isi a must.

For example, for creating a line with buttons all the same size that always match the width of the inspector you should use something like this (this will create two lines, the first one with 4 buttons and the second one with 3, but all of them in the same row will have the same width):

    public override void OnInspectorGUI() {

        EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
        EditorGUILayout.BeginHorizontal();
        
        EditorGUILayout.BeginVertical();
        if(GUILayout.Button("Button 1", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Button 2", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Button 3", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Button 4", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
        EditorGUILayout.BeginHorizontal();

        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Button 5", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();

        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Button 6", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("Button 7", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {

        }
        EditorGUILayout.EndVertical();

        EditorGUILayout.EndHorizontal();
        EditorGUILayout.EndVertical();

    }

Hope this was helpful.

Regards.