유니티에서 Assets의 이름을 직관적이고 규칙적으로 관리하는것은 매우 중요합니다. 연속적인 같은 이름을 가지는 에셋들의 이름을 편리하게 지어주는 Tool을 구현하였습니다.

 

📺 미리 보기

 

📖 구현 내용

  • Project 폴더 내 에셋들의 이름을 변경할 수 있습니다.
  • 하이어라키에 내 오브젝트들의 이름을 변경할 수 있습니다.
  • 시작 인덱스 값을 지정하여 이 값부터 시작할 수 있습니다. (예시, 12부터 새롭게 넘버링)
  • 하이어라키 오브젝트의 이름을 변경한 건에 대하여 undo가 가능합니다.

 

⚒️ 구현

  • Editor Tool로 구현하였습니다.

· 구현 내용

#if UNITY_EDITOR

using UnityEditor;
using UnityEngine;

public class EasyRenameWindow : EditorWindow
{
    private string mBaseName = "NewName"; // Default name
    private int mStartIdx = 0; // Start index
    private int mAssetsCount = 0; // Number of selected assets

    [MenuItem("Tools/Bonnate/Editor Tools/Easy Rename")]
    public static void ShowWindow()
    {
        GetWindow<EasyRenameWindow>("Easy Rename");
    }

    private void OnGUI()
    {
        mBaseName = EditorGUILayout.TextField("Base Name", mBaseName);
        mStartIdx = EditorGUILayout.IntField("Start Index", mStartIdx);

        GUILayout.Space(10);

        // Check conditions to enable/disable the button
        bool canRename = !string.IsNullOrEmpty(mBaseName) && Selection.objects.Length > 0;

        GUI.enabled = canRename; // Set button's enabled state

        if (GUILayout.Button("Rename Selected Assets"))
            RenameSelectedAssets();

        GUI.enabled = true; // Reset enabled state for other GUI elements

        // Add more space
        GUILayout.Space(20);

        // Begin a horizontal layout group
        GUILayout.BeginHorizontal();

        // Display a label with the text "Powered by: Bonnate" at the bottom of the window
        EditorGUILayout.LabelField("Powered by: Bonnate");

        // Create a button with the label "Github" that opens a URL when clicked
        if (GUILayout.Button("Github", GetHyperlinkLabelStyle()))
        {
            // Call the method to open the GitHub URL
            OpenURL("https://github.com/bonnate");
        }

        // Create a button with the label "Blog" that opens a URL when clicked
        if (GUILayout.Button("Blog", GetHyperlinkLabelStyle()))
        {
            // Call the method to open the Blog URL
            OpenURL("https://bonnate.tistory.com/");
        }

        // End the horizontal layout group
        GUILayout.EndHorizontal();
    }

    private void RenameSelectedAssets()
    {
        Object[] selectedAssets = Selection.objects;

        if (selectedAssets.Length == 0)
        {
            Debug.LogWarning("No assets selected.");
            return;
        }

        mAssetsCount = selectedAssets.Length;

        for (int i = 0; i < mAssetsCount; i++)
        {
            Object asset = selectedAssets[i];
            string newName = mBaseName + (mStartIdx + i);

            // Rename Assets
            string assetPath = AssetDatabase.GetAssetPath(asset);

            // Record Undo
            Undo.RecordObject(asset, "Rename Asset");

            AssetDatabase.RenameAsset(assetPath, newName);

            // Rename
            asset.name = newName;

            // SetDirty
            EditorUtility.SetDirty(asset);
        }

        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }

    // Returns a GUI style for a hyperlink-like label
    private GUIStyle GetHyperlinkLabelStyle()
    {
        // Create a new GUIStyle based on the default label style
        GUIStyle style = new GUIStyle(GUI.skin.label);
        style.normal.textColor = new Color(0f, 0.5f, 1f); // Blue color
        style.stretchWidth = false;
        style.wordWrap = false;
        return style;
    }

    // Opens the provided URL with the default application
    private void OpenURL(string url)
    {
        EditorUtility.OpenWithDefaultApp(url);
    }
}

#endif

 

private string mBaseName = "NewName"; // Default name
  • Window에서 입력할 이름을 저장할 변수입니다.

 

private int mStartIdx = 0; // Start index
  • 이름을 바꿀 오브젝트들의 번호를 부여할 변수입니다. Window에서 입력합니다.

 

private void OnGUI()
  • Window가 열리면 각 라벨을 생성하고, 입력값을 받습니다.
  • 버튼은 !string.IsNullOrEmpty(mBaseName) && Selection.objects.Length > 0;가 참일때만 활성화 됩니다.

 

private void RenameSelectedAssets()
  • 버튼을 눌러 호출되는 함수로 선택한 오브젝트들의 이름을 변경합니다.
  • Undo.Record를 이용하여 Undo 기능을 구현합니다.

 

✅ 사용 방법

  • Tools/Editor Tools/Easy Rename을 클릭하여 다이얼로그 창을 엽니다.

 

  • Base Name에 공통적으로 적용할 이름을 적습니다.
  • StartIndex에 넘버링을 시작 할 번호를 입력합니다.
  • 하이어라키 또는 Project 폴더에서 이름을 변경할 대상을 클릭합니다.
  • Rename 버튼을 클릭합니다.
    • Rename 버튼은 두개의 입력필드가 채워져야하고, 하이어라키 또는 Project 폴더에서 선택이 되어야합니다.

 

 

🕹️ Unity Affiliate

  • Unity Affiliate Program 파트너로서 아래의 배너를 통해 접속하신 경우 수수료를 받을 수 있습니다.
  • 아래 배너의 에셋들은 '실시간 무료 에셋 랭킹'을 나타냅니다.
bonnate