概要
使用该工具可以一键修改该物体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即可,因为我有这个功能,也懒得改代码了,你们直接用也不影响)
小结
请给我说谢谢,我会给你说不客气
有创造力的可以魔改