[유니티 Unity]오브젝트 이름 한번에 변경하는 스크립트

 

 

오브젝트의 종류가 많아 졌을 때

하나하나 이름 바꾸는게 귀찮아서 검색을 하다가 

어떤 개발자 분이 만들어두신게 있어서 주워왔습니다^^

 

 

 

스크립트를 넣어주고

빈오브젝트를 넣어서 적용하는 건 줄 알았는데

MonoBehaviour가 아니라서 컴포넌트 추가가 안되서 찾아보다가

EditorWindow 라서 컴포넌트에 추가가 안되는것을 발견했고 

 

 

 

상단의 Tool 창에 ECNameing이 생긴것을 볼 수 있다

클릭하면  아래와 같은 창이 나온다

 

 

 

오브젝트 리스트안에 다음과 같이 이름을 변경할 오브젝트를 넣어주고

변경하기를 누르면 쉽게 이름이 바뀌는 것 까지 확인하였다 

 

 

 

<출처> https://github.com/hahahohohun/PublicCode/blob/main/README.md#ecnamingcs

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;
        }
    }
}