Editor GUI / Button gets pushed "away" when the window is smaller

Hello

I am creating a new window in the editor. It’s going quite good, but I got stuck within one thing:

So, in a line, I have a label and two buttons. When I make the window smaller, then the search button disappears - it get’s pushed to the right, so that it is not fully visible anymore.
Rather it should always stay some space from the edge. So it is also it the predefined unity windows. But I have no Idea how they did ti. Here how it looks:

161261-c-c-c-c.png

I have also set up a minimal width of the window, so that the label never get’s in contact with the button.

Here is my code:

private void DrawFilterGraphics(string header, Func<GameObject, bool> filter, List<GameObject> list)
        {
            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(new GUIContent(header), boldLabel);
            GUILayout.Button(new GUIContent("= " + String.Format("{0:0,0}", list.Count)), GUILayout.Width(75));
            EditorGUILayout.Space(10);

            if (GUILayout.Button(new GUIContent("Search"), GUILayout.Width(60)))
            {
                GameObject[] gameObjects = GameObject.FindObjectsOfType<GameObject>();
                list.Clear();

                foreach (GameObject gameObject in gameObjects)
                {
                    if (filter(gameObject))
                    {
                        list.Add(gameObject);
                    }
                }
                Selection.objects = list.ToArray();
                
            }

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
            DrawList(list);
            EditorGUILayout.Space(5);
            HelperUtility.DrawLine();
        }

I tried it with EditorGUILayout.Space(); and GUILayout.FlexibleSpace(), but nothing worked.
Does somebody has an idea how to do this?

Thank you very much

Huxi

Well, first of all do not use a LabelField. They are a very special kind of label which are meant for usage in the inspector. They add some additional offset. If you want to display a label, use GUILayout.Label. Furthermore in order to seperate the label from the buttons you want to insert a FlexibleSpace in between. The FlexibleSpace will simply consume all not used space in a layout group. This should ensure that the buttons stick to the right side. Of course if the available space is too small to contain all of your content it could still be pushed out of bounds.

Apart from those changes I would also recommend to set minSize to a reasonable value inside OnEnable. So your layout might require a minimum size to display so you could force the window to keep that size. This does not always work when you docked several windows together because the size constraint of the active tab is used.

Hello @Bunny83

thank you very much for your answer. I change the label, added the flexibleSpace and it helped a bit, but the button was still out of bound. But just a little bit. So I made the buttons little bit smaller, so that now it works fine. :slight_smile:

161262-c-c-c-c-ok.png

Thank you very much for your support!

Huxi