Unity一键修改物体AB名称为该物体名称及后缀

概要

使用该工具可以一键修改该物体AB包名称为该物体名称,改代码支持物体拖拽功能,但是删除功能还没加,有空加,有大佬也可以进行魔改,别忘了发我一份哦。这属于编辑器代码,记得放Editor文件夹下!!!!!

直接上代码

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

public class AssetBundleRenamer : EditorWindow
{
    private Object[] selectedObjects;
    string directoryName = null;
    private string bundleSuffix = "bundle";
    private int count = 0;
    [MenuItem("Tools/Asset Bundle Renamer")]
    static void Init()
    {
        AssetBundleRenamer window = (AssetBundleRenamer)EditorWindow.GetWindow(typeof(AssetBundleRenamer));
        window.Show();
    }

    void OnGUI()
    {
        GUILayout.Label("统一修改物体AB名称", EditorStyles.boldLabel);

        GUILayout.Space(10);        

        DragAndDropArea();

        GUILayout.Label("所选物体:");
        if (selectedObjects != null && selectedObjects.Length > 0)
        {
            foreach (Object obj in selectedObjects)
            {

                GUIStyle redTextStyle = new GUIStyle(EditorStyles.helpBox);
                redTextStyle.normal.textColor = Color.red;

                // 显示物体名称,并添加点击事件
                if (GUILayout.Button(obj.name, redTextStyle))
                {
                    // 获取物体的Asset路径
                    string assetPath = AssetDatabase.GetAssetPath(obj);
                    if (!string.IsNullOrEmpty(assetPath))
                    {
                        // 在Unity编辑器中打开对应的Asset并高亮显示
                        UnityEditor.EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath<Object>(assetPath));
                    }
                }
            }
        }
        else
        {
            GUILayout.Label("拖拽物体放到此区域");
        }

        GUILayout.Space(10);
        directoryName = EditorGUILayout.TextField("DirectoryName:", directoryName);
        bundleSuffix = EditorGUILayout.TextField("Bundle Suffix:", bundleSuffix);
        count = EditorGUILayout.IntField("count:",count);
        GUILayout.Space(20);

        if (GUILayout.Button("Rename Asset Bundles"))
        {
            RenameAssetBundles();
        }
    }

    void DragAndDropArea()
    {
        Event currentEvent = Event.current;
        Rect dropArea = GUILayoutUtility.GetRect(0.0f, 50.0f, GUILayout.ExpandWidth(true));
        // 设置拖拽的物体显示区域的背景色为蓝色
        EditorGUI.DrawRect(dropArea, Color.blue);
        GUI.Box(dropArea, "将物体单个或全部拖拽此区域");

        switch (currentEvent.type)
        {
            case EventType.DragUpdated:
            case EventType.DragPerform:
                if (!dropArea.Contains(currentEvent.mousePosition))
                    return;

                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (currentEvent.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();

                    selectedObjects = DragAndDrop.objectReferences;
                    Repaint();
                }
                break;
        }
    }

    void RenameAssetBundles()
    {
        if (selectedObjects != null && selectedObjects.Length > 0)
        {
            foreach (Object obj in selectedObjects)
            {
                string assetPath = AssetDatabase.GetAssetPath(obj);
                if (!string.IsNullOrEmpty(assetPath))
                {
                    string newAssetBundleName = string.IsNullOrEmpty(directoryName) ? obj.name.Remove(0,count) : directoryName + "/" + obj.name.Remove(0, count);

                    AssetImporter importer = AssetImporter.GetAtPath(assetPath);
                    if (importer != null)
                    {
                        importer.SetAssetBundleNameAndVariant(newAssetBundleName, bundleSuffix);
                        importer.SaveAndReimport();
                    }
                }
            }

            Debug.Log("Asset Bundles Renamed Successfully!");
        }
        else
        {
            Debug.LogWarning("No objects selected.");
        }
    }
}

工具长这个熊样子

在这里插入图片描述
DirectoryName这个变量就是’'的功能(不进行修改也没事),count变量是去除该名称前几个字符的数量(不想去除就为0即可,因为我有这个功能,也懒得改代码了,你们直接用也不影响)

小结

请给我说谢谢,我会给你说不客气

有创造力的可以魔改

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高达打架用铁锹

谢谢老板赏饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值