Skip to content

Add Bulk Renamer Editor Utility #2

Description

@zuedev

Integrate a utility window script to allow bulk renaming (prepending or appending custom strings) of assets inside a selected folder. This speeds up asset management and organization workflows directly within the Unity Editor without breaking asset GUID connections.

Prototype

using UnityEngine;
using UnityEditor;
using System.IO;

public class BulkRenamer : EditorWindow
{
    private string customString = "";
    private bool isPrepend = true;

    [MenuItem("Tools/Bulk Renamer")]
    public static void ShowWindow()
    {
        GetWindow<BulkRenamer>("Bulk Renamer");
    }

    private void OnGUI()
    {
        GUILayout.Label("Bulk Rename Assets", EditorStyles.boldLabel);
        EditorGUILayout.Space();

        string selectedFolderPath = GetSelectedFolderPath();
        if (string.IsNullOrEmpty(selectedFolderPath))
        {
            EditorGUILayout.HelpBox("Please select a folder in your Project window.", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox($"Target Folder: {selectedFolderPath}", MessageType.Info);
        }

        EditorGUILayout.Space();
        customString = EditorGUILayout.TextField("Custom String", customString);

        string[] options = { "Prepend (Prefix)", "Append (Suffix)" };
        isPrepend = EditorGUILayout.Popup("Placement", isPrepend ? 0 : 1, options) == 0;

        EditorGUILayout.Space();

        EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(selectedFolderPath) || string.IsNullOrEmpty(customString));
        if (GUILayout.Button("Rename Assets Inside Folder"))
        {
            RenameAssets(selectedFolderPath);
        }
        EditorGUI.EndDisabledGroup();
    }

    private string GetSelectedFolderPath()
    {
        var selectedObject = Selection.activeObject;
        if (selectedObject == null) return null;

        string path = AssetDatabase.GetAssetPath(selectedObject);
        if (AssetDatabase.IsValidFolder(path))
        {
            return path;
        }

        return null;
    }

    private void RenameAssets(string folderPath)
    {
        string[] guids = AssetDatabase.FindAssets("", new[] { folderPath });
        int renamedCount = 0;

        AssetDatabase.StartAssetEditing();

        try
        {
            foreach (string guid in guids)
            {
                string assetPath = AssetDatabase.GUIDToAssetPath(guid);

                if (AssetDatabase.IsValidFolder(assetPath))
                    continue;

                string parentFolder = Path.GetDirectoryName(assetPath).Replace('\\', '/');
                if (parentFolder != folderPath)
                    continue;

                string filename = Path.GetFileNameWithoutExtension(assetPath);
                string newName = isPrepend ? customString + filename : filename + customString;

                string error = AssetDatabase.RenameAsset(assetPath, newName);
                if (string.IsNullOrEmpty(error))
                {
                    renamedCount++;
                }
                else
                {
                    Debug.LogError($"Failed to rename {filename}: {error}");
                }
            }
        }
        finally
        {
            AssetDatabase.StopAssetEditing();
        }

        AssetDatabase.Refresh();
        EditorUtility.DisplayDialog("Success", $"Successfully renamed {renamedCount} assets.", "OK");
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions