Home > OS >  Why the scrollbar in the editor window is too short and small and why the array object name is also
Why the scrollbar in the editor window is too short and small and why the array object name is also

Time:07-07

scroll bar

When i scroll up/down the Objects also scrolling and i want only the items in the array to be scrolling not the title Objects.

Second problem is how to make the scrolling to be wider ? i tried to change the width from 100 to 500 but it changed the position pushed to the right by 500 didn't make it wider.

scrollPos =
            EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(100), GUILayout.Height(100));

The script :

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(250, 100);
    private string childrenPrefix;
    private int startIndex;
    private static SerializedObject _serializedObject;
    public GameObject[] objects;
    Vector2 scrollPos;

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

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

    private void OnEnable()
    {
        ScriptableObject target = this;
        _serializedObject = new SerializedObject(target);

        objects = Selection.gameObjects;
    }

    private void OnGUI()
    {
        childrenPrefix = EditorGUILayout.TextField("Children prefix", childrenPrefix);
        startIndex = EditorGUILayout.IntField("Start index", startIndex);

        
        _serializedObject.Update();

        EditorGUILayout.BeginHorizontal();
        scrollPos =
            EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(100), GUILayout.Height(100));

        SerializedProperty serializedProperty = _serializedObject.FindProperty("objects");
        EditorGUILayout.PropertyField(serializedProperty, true);

        EditorGUILayout.EndScrollView();
        EditorGUILayout.EndHorizontal();

        Repaint();

        if (GUILayout.Button("Rename children"))
        {
            for (int objectI = 0; objectI < objects.Length; objectI  )
            {
                Transform selectedObjectT = objects[objectI].transform;
                for (int childI = 0, i = startIndex; childI < selectedObjectT.childCount; childI  ) selectedObjectT.GetChild(childI).name = $"{childrenPrefix}{i  }";
            }
        }
    }
}

I tried this and the result is almost as i wanted but now the parent Objects is gone. i want to see the parent of the array items and to be able to expand the array items but when scrolling that only the items in the array to be scrolled not the parent !

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);
    private string childrenPrefix;
    private int startIndex;
    private static SerializedObject _serializedObject;
    public GameObject[] objects;
    Vector2 scrollPos;
    SerializedProperty serializedProperty;

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

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

    private void OnEnable()
    {
        ScriptableObject target = this;
        _serializedObject = new SerializedObject(target);

        serializedProperty = _serializedObject.FindProperty("objects");

        objects = Selection.gameObjects;
    }

    private void OnGUI()
    {
        childrenPrefix = EditorGUILayout.TextField("Children prefix", childrenPrefix);
        startIndex = EditorGUILayout.IntField("Start index", startIndex);

        _serializedObject.Update();

        scrollPos =
            EditorGUILayout.BeginScrollView(scrollPos);

        for(int i = 0; i < serializedProperty.arraySize; i  )
        {
            var obj = serializedProperty.GetArrayElementAtIndex(i);
            EditorGUILayout.PropertyField(obj, new GUIContent("Object "   i), true);
        }

        EditorGUILayout.EndScrollView();

        Repaint();

        GUILayout.Space(5);
        if (GUILayout.Button("Rename children"))
        {
            for (int objectI = 0; objectI < objects.Length; objectI  )
            {
                Transform selectedObjectT = objects[objectI].transform;
                for (int childI = 0, i = startIndex; childI < selectedObjectT.childCount; childI  ) selectedObjectT.GetChild(childI).name = $"{childrenPrefix}{i  }";
            }
        }
    }
}

scrolling

CodePudding user response:

Scrolling an array with the title is the default behavior of the list serialized in the inspector. The only way I know how to make what you want is to create a custom field drawer. I mean, you can draw a label with the list name, then create your scroll view and include only list elements in the scrolling area.

About width - I used Screen.width - 50 in my custom editors and it did the trick for me. You need to subtract this weird 50 as there is no adequate option to get if your window has the scrollbar or not at the moment (or at least, I didn't find it).

CodePudding user response:

This is a solution working for me just like i wanted with the children problem.

I'm going to work on it more and add stuff but for this specific problem this is working great.

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].transform.position =
                    EditorGUILayout.Vector3Field(i   " Position", objects[i].transform.position);
            }

            EditorGUILayout.EndScrollView();

            EditorGUI.indentLevel--;
        }

        Repaint();
    }
}
  • Related