[UNITY::북마]하이어라키상(hierarchy)의 오브젝트들의 이름을 한번에 변경

https://funfunhanblog.tistory.com/402

 

[유니티 커스텀 에디터] 하이어라키상(hierarchy)의 오브젝트들의 이름을 한번에 변경하기

오브젝트에 인덱스를 추가하여 이름을 변경이 필요할때! ctrl + D 를 통해 생성한 오브젝트는 (1),(2),(3)~~~ 이름 마지막에 이렇게 숫자가 붙는다. 하나 하나 바꾸기 번거롭기때문에 이것을 한꺼번에

funfunhanblog.tistory.com

https://github.com/hahahohohun/PublicCode/blob/main/README.md#ecnamingcs

 

PublicCode/README.md at main · hahahohohun/PublicCode

Contribute to hahahohohun/PublicCode development by creating an account on GitHub.

github.com

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

public class ECNaming : EditorWindow
{
    private bool _bEditOn = true;

    [SerializeField]
    public List<GameObject> ins_GameObjects = new List<GameObject>();

    [MenuItem("Tools/ECNaming")]
    private static void Open()
    {
        ECNaming win = GetWindow<ECNaming>();
        win.titleContent = new GUIContent("Naming Tool");
        win.Show();
    }
    private void OnEnable()
    {
        _bEditOn = true;
    }
    private void OnDisable()
    {
        _bEditOn = false;
        _nCount = 0;
    }

    private Editor _editor;
    private string _strName = null;
    private int _nCount = 0;

    private void OnGUI()
    {
        if (!_editor) { _editor = Editor.CreateEditor(this); }
        if (_editor) { _editor.OnInspectorGUI(); }

        GUILayout.BeginVertical("BOX");

        GUILayout.Space(10);
        GUILayout.Label("변경 할 이름", GUILayout.Width(75));
        _strName = EditorGUILayout.TextField(_strName, GUILayout.ExpandWidth(true));
        GUILayout.Label("시작 인덱스(-1이면 인덱스를 안붙임)", GUILayout.ExpandWidth(true));
        _nCount = EditorGUILayout.IntField(_nCount, GUILayout.ExpandWidth(true));
        GUILayout.Space(10);

        GUILayout.EndHorizontal();

        if (GUILayout.Button("변경"))
        {
            int nIdx = _nCount;
            string strName = _strName;
            for (int i = 0; i < ins_GameObjects.Count; i++)
            {
                strName = _strName;
                if (_nCount != -1)
                {
                    strName += nIdx;
                    nIdx++;
                }
                ins_GameObjects[i].name = strName;
            }

            if (ins_GameObjects.Count == 0)
            {
                Debug.LogError("리스트가 비어있음");
            }
            else
            {
                Debug.LogError("변경 완료");
            }
        }
    }
}

[CustomEditor(typeof(ECNaming), true)]
public class ECNamingEditor : Editor
{
    public override void OnInspectorGUI()
    {
        var list = serializedObject.FindProperty("ins_GameObjects");
        EditorGUILayout.PropertyField(list, new GUIContent("오브젝트 리스트"), true);

        serializedObject.ApplyModifiedProperties();

        if (GUILayout.Button("초기화"))
        {
            list.arraySize = 0;
        }
    }
}