Unity修改批量修改名字工具

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);
        //}
    }

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值