Unity修改批量修改名字工具
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text.RegularExpressions;
using System;
namespace BRK.EffectExpand
{
public class AddPrefixWindow : EditorWindow
{
private string prefix = "";//前缀
private string postfix = "";//后缀
private string removeString = "";//移除字符串
private string matching, modify;//匹配修改
private string reName;
private int reNameCount;
private int startCount = 0; //添加的起始序号
public static void Init()
{
AddPrefixWindow myWindow = (AddPrefixWindow)EditorWindow.GetWindow(typeof(AddPrefixWindow), true, "BRK命名工具", true);//创建窗口
myWindow.maxSize = new Vector2(350, 350);
myWindow.minSize = new Vector2(100, 350);
myWindow.Show();//展示
}
private void OnGUI()
{
using (new GUILayout.VerticalScope("Box"))
{
prefix = EditorGUILayout.TextField("前缀", prefix);
if (GUILayout.Button("确定更改"))
{
NamingNotations.AddCustomPrefix(prefix);
}
}
using (new GUILayout.VerticalScope("Box"))
{
postfix = EditorGUILayout.TextField("后缀", postfix);
if (GUILayout.Button("确定更改"))
{
NamingNotations.AddCustomPostfix(postfix);
}
}
using (new GUILayout.VerticalScope("Box"))
{
startCount = EditorGUILayout.IntField("后缀添加的起始序号", startCount);
if (GUILayout.Button("确定更改"))
{
NamingNotations.AddCustomPostfixNumber(startCount);
}
}
using (new GUILayout.VerticalScope("Box"))
{
removeString = EditorGUILayout.TextField("移除的字符串", removeString);
if (GUILayout.Button("确定更改"))
{
NamingNotations.Remove(removeString);
}
}
using (new GUILayout.VerticalScope("Box"))
{
matching = EditorGUILayout.TextField("匹配字符串", matching);
modify = EditorGUILayout.TextField("替换字符串", modify);
if (GUILayout.Button("确定更改"))
{
NamingNotations.MatchingModifyString(matching, modify);
}
}
using (new GUILayout.VerticalScope("Box"))
{
reName = EditorGUILayout.TextField("批量重命名", reName);
reNameCount = EditorGUILayout.IntField("起始序号", reNameCount);
if (GUILayout.Button("确定更改"))
{
NamingNotations.ReDefine(reName, reNameCount);
}
}
}
}
public class NamingNotations : Editor
{
private delegate string NamingNotationsDelegate(string name, string addPrefix = null);
private static NamingNotationsDelegate namingNotationsDelegate;
[MenuItem("Assets/特效命名规范/一键规范化", false, 100)]
public static void NameNormalize()
{
namingNotationsDelegate = Criterion;
ReName(namingNotationsDelegate);
}
[MenuItem("Assets/特效命名规范/加Fx前缀", false, 101)]
public static void AddFXPrefix()
{
namingNotationsDelegate = AddPrefix;
ReName(namingNotationsDelegate, "Fx");
}
[MenuItem("Assets/特效命名规范/加UI前缀", false, 102)]
public static void AddUIPrefix()
{
namingNotationsDelegate = AddPrefix;
ReName(namingNotationsDelegate, "UI");
}
[MenuItem("Assets/特效命名规范/自定义前后缀", false, 103)]
public static void AddCustomPrefix()
{
AddPrefixWindow.Init();
}
public static void AddCustomPrefix(string pName)
{
namingNotationsDelegate = AddPrefix;
ReName(namingNotationsDelegate, pName);
}
public static void AddCustomPostfix(string pName)
{
namingNotationsDelegate = AddPostfix;
ReName(namingNotationsDelegate, pName);
}
public static void Remove(string pName)
{
namingNotationsDelegate = ReMoveString;
ReName(namingNotationsDelegate, pName);
}
public static void AddCustomPostfixNumber(int addCount)
{
ReName(addCount);
}
/// <summary>
/// 匹配修改
/// </summary>
/// <param name="name"></param>
/// <param name="removeString"></param>
/// <returns></returns>
public static void MatchingModifyString(string Matching, string Modifty)
{
string s = Matching + "/" + Modifty;
namingNotationsDelegate = delegate (string s, string s1)
{
string[] matchingModify = s1.Split('/');
return s.Replace(matchingModify[0], matchingModify[1]);
};
ReName(namingNotationsDelegate, s);
}
public static void ReDefine(string reString, int count)
{
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered);
for (int index = 0; index < arr.Length; index++)
{
string filePath = AssetDatabase.GetAssetPath(arr[index]);
FileInfo fileInfo = new FileInfo(filePath);
string fileName = filePath.Split('.')[0];
string[] fileNamePath = fileName.Split('/');
AssetDatabase.RenameAsset(filePath, "BRK_"+ index);
}
for (int index = 0; index < arr.Length; index++)
{
string filePath = AssetDatabase.GetAssetPath(arr[index]);
FileInfo fileInfo = new FileInfo(filePath);
string fileName = filePath.Split('.')[0];
string[] fileNamePath = fileName.Split('/');
AssetDatabase.RenameAsset(filePath, reString + (count+ index));
}
AssetDatabase.Refresh();
}
private static void ReName(NamingNotationsDelegate fun,string s = null)
{
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered);
for (int index = 0; index < arr.Length; index++)
{
string filePath = AssetDatabase.GetAssetPath(arr[index]);
FileInfo fileInfo = new FileInfo(filePath);
string fileName = filePath.Split('.')[0];
string[] fileNamePath = fileName.Split('/');
//fileNamePath[fileNamePath.Length - 1] = fun(fileNamePath[fileNamePath.Length - 1]);//更改的名字
//string destFileName = ArrayToString(fileNamePath) + fileInfo.Extension;
System.IO.File.Move(filePath, destFileName);
AssetDatabase.RenameAsset(filePath, fun(fileNamePath[fileNamePath.Length - 1], s));
}
AssetDatabase.Refresh();
}
private static void ReName(int addCount)
{
UnityEngine.Object[] arr = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered);
for (int index = 0; index < arr.Length; index++)
{
string filePath = AssetDatabase.GetAssetPath(arr[index]);
FileInfo fileInfo = new FileInfo(filePath);
string fileName = filePath.Split('.')[0];
string[] fileNamePath = fileName.Split('/');
AssetDatabase.RenameAsset(filePath, fileNamePath[fileNamePath.Length - 1] + (addCount+1+ index));
}
AssetDatabase.Refresh();
}
/// <summary>
/// 添加Fx前缀
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private static string AddPrefix(string name, string addPrefix)
{
if (name.Length < 2)
{
return addPrefix + name;
}
string prefix = name.Substring(0, 2);
if (prefix.Equals(addPrefix, StringComparison.InvariantCultureIgnoreCase))//已存在Fx前缀
{
name = char.ToUpper(name[0]) + name.Substring(1);//首字母大写
}
else
{
name = addPrefix + name;
}
return name;
}
/// <summary>
/// 添加后缀
/// </summary>
/// <param name="name"></param>
/// <param name="addPrefix"></param>
/// <returns></returns>
private static string AddPostfix(string name, string addPrefix)
{
return name + addPrefix;
}
/// <summary>
/// 移除对应字符串
/// </summary>
/// <param name="name"></param>
/// <param name="removeString"></param>
/// <returns></returns>
private static string ReMoveString(string name, string removeString)
{
return name.Replace(removeString, "");//去除空格
}
/// <summary>
/// 标准化
/// </summary>
private static string Criterion(string name , string addPrefix = null)
{
string criterionName = name.Replace(" ", "");//去除空格
criterionName = Regex.Replace(criterionName, @"[^a-zA-Z0-9\u4e00-\u9fa5\s]", "");//去除符号
foreach (var c in criterionName)//移除前面为数字的情况
{
if (c >= '0' && c <= '9')
{
criterionName = criterionName.Substring(1);
}
else
{
break;
}
}
criterionName = char.ToUpper(criterionName[0]) + criterionName.Substring(1);//首字母大写
return criterionName;
}
/ <summary>
/ 字符串数组转字符串
/ </summary>
/ <param name="array"></param>
/ <returns></returns>
//private static string ArrayToString(string[] array)
//{
// string s = "";
// foreach (string item in array)
// {
// s += item + "/";
// }
// return s.Substring(0, s.Length - 1);
//}
}
}