Home > OS >  In editor window script when adding a string before object field object it's adding a big space
In editor window script when adding a string before object field object it's adding a big space

Time:07-07

I'm trying to remove or reduce the space between the numbers and the objects. Tried something but nothing helped to reduce or remove the space.

spaces

The code

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class RenameSelected : EditorWindow
{
    private static readonly Vector2Int size = new Vector2Int(500, 500);
    bool showPosition = false;
    GameObject[] objects;
    Vector2 scrollPos;

    [MenuItem("GameObject/Rename Selected")]
    public static void Init()
    {
        EditorWindow window = GetWindow<RenameSelected>();
        window.minSize = size;
        window.maxSize = size;
    }

    private void OnSelectionChange()
    {
        objects = Selection.gameObjects;
    }

    private void OnEnable()
    {
        objects = Selection.gameObjects;
    }

    public void OnGUI()
    {
        if (objects.Length == 0)
        {
            showPosition = false;
        }
        GUILayout.Space(20);
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.GetControlRect(true, 16f, EditorStyles.foldout);
        Rect foldRect = GUILayoutUtility.GetLastRect();
        if (Event.current.type == EventType.MouseUp && foldRect.Contains(Event.current.mousePosition))
        {
            showPosition = !showPosition;
            GUI.changed = true;
            Event.current.Use();
        }

        showPosition = EditorGUI.Foldout(foldRect, showPosition, "Objects");
        GUILayout.Space(2);

        if (showPosition)
        {
            EditorGUI.indentLevel  ;

            scrollPos =
            EditorGUILayout.BeginScrollView(scrollPos);
            for (int i = 0; i < objects.Length; i  )
            {
                
                objects[i] = (GameObject)EditorGUILayout.ObjectField(i.ToString() ,objects[i], typeof(GameObject), true);
            }

            EditorGUILayout.EndScrollView();

            EditorGUI.indentLevel--;
        }

        Repaint();
    }
}

I tried to add EditorGUILayout.BeginHorizontal(); and GUILayout.FlexibleSpace(); and EditorGUILayout.EndHorizontal(); but nothing helped to remove the long space between the string label and the object.

EditorGUILayout.BeginHorizontal();
            for (int i = 0; i < objects.Length; i  )
            {
                
                objects[i] = (GameObject)EditorGUILayout.ObjectField(i.ToString() ,objects[i], typeof(GameObject), true);
                GUILayout.FlexibleSpace();
            }
            
            EditorGUILayout.EndHorizontal();

CodePudding user response:

Solution is to add the line :

EditorGUIUtility.labelWidth = 50;

before each ObjectField :

for (int i = 0; i < objects.Length; i  )
            {
                EditorGUIUtility.labelWidth = 50;
                objects[i] = (GameObject)EditorGUILayout.ObjectField(i.ToString() ,objects[i], typeof(GameObject), true);
            }

spaces

CodePudding user response:

Use the Label function instead of using the ObjectField's label parameter. This allows for more control of the layout.

EditorGUILayout.BeginHorizontal();
{
    GUILayout.Space(20);
    GUILayout.Label(i.ToString(), GUILayout.Width(20));
                
    objects[i] = (GameObject)EditorGUILayout.ObjectField(objects[i], typeof(GameObject), true);
}
EditorGUILayout.EndHorizontal();
  • Related