参考
编辑器窗口
GUILayout.Space
【Unity】8.3 布局模式(GUILayout)
Unity:资源文件批处理名称修改
原理
- 读取选中的所有资源对象
- 资源对象统一重命名格式
- 实现编辑器工具一键操作
用法
- 选择好要改名的文件
- 打开工具,填好前缀和ID,点击 reName
运行结果:
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class ReNameTool:EditorWindow {
public static string preFix = "Texture_";
public static string nameId = "10000";
[MenuItem("Window/ReNameTool")]
public static void ShowWindow()
{
//显示现有窗口实例,如果没有,创建一个
EditorWindow.GetWindow(typeof(ReNameTool));
}
void OnGUI()
{
//组
GUI.BeginGroup(new Rect(0, 5, Screen.width, 180));
//标识组
GUI.Box(new Rect(0, 5, Screen.width, 180), "ReName Setting");
//也可以用这种方式显示 begin
//GUILayout.Space(30);
//preFix = EditorGUILayout.TextField("PreFix", preFix);
//GUILayout.Space(20);
//nameId = EditorGUILayout.TextField("NameId", nameId);
//也可以用这种方式显示 end
//前缀
GUI.Label(new Rect(5, 48, 60, 20), "PreFix :");
preFix = GUI.TextField(new Rect(70, 50, Screen.width, 20),preFix);
//后面的ID
GUI.Label(new Rect(5, 78, 60, 20), "NameId :");
nameId = GUI.TextField(new Rect(70, 80, Screen.width, 20),nameId);
if (GUI.Button(new Rect(Screen.width/2 - 40, 130, 100, 20),"reName"))
{
Rename();
}
GUI.EndGroup();
}
/// <summary>
/// 重命名文件
/// </summary>
/// <returns></returns>
public static void Rename()
{
Object[] m_objects = Selection.GetFiltered(typeof(Object), SelectionMode.Assets);//选择资源对象
int index = 0;
for (int i = 0; i < m_objects.Length; i++)
{
if (System.IO.Path.GetExtension(AssetDatabase.GetAssetPath(m_objects[i])) != "") //判断路径是否为空
{
string path = AssetDatabase.GetAssetPath(m_objects[i]);
string newName = preFix + nameId + index;
AssetDatabase.RenameAsset(path, newName);
index++;
}
}
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}