特效替换名字

using UnityEngine;
using UnityEditor;


    public class EffectNameReplace : EditorWindow
    {
        private Object targetFile;
        private string oldStr = string.Empty;
        private string newStr = string.Empty;
        private string prefix = string.Empty;

        [MenuItem("自定义工具/特效替换名字", false, 110)]
        public static void ShowWindow()
        {
            EditorWindow window = EditorWindow.GetWindow(typeof(EffectNameReplace));
            window.titleContent = new GUIContent("EffectNameReplace");
        }

        private void OnGUI()
        {
            targetFile = EditorGUILayout.ObjectField("添加文件:", targetFile, typeof(Object), true) as Object;

            GUILayout.Label("输入要替换的字符串(只替换材质球和贴图):");
            GUILayout.BeginHorizontal();
            oldStr = GUILayout.TextField(oldStr);
            newStr = GUILayout.TextField(newStr);
            if (GUILayout.Button("开始替换", GUILayout.Width(100)))
            {
                StartReplace(string.Empty, oldStr, newStr);
            }
            GUILayout.EndHorizontal();

            GUILayout.Label("输入要增加的前缀字符串(只处理材质球和贴图):");
            GUILayout.BeginHorizontal();
            prefix = GUILayout.TextField(prefix);
            if (GUILayout.Button("增加前缀", GUILayout.Width(100)))
            {
                StartReplace(prefix, string.Empty, string.Empty);
            }
            GUILayout.EndHorizontal();
        }

        private void StartReplace(string prefix, string oldStr, string newStr)
        {
            if (null == targetFile)
            {
                this.ShowNotification(new GUIContent("请选择文件夹!"));
                return;
            }

            string[] checkDirs = new string[] { AssetDatabase.GetAssetPath(targetFile) };

            string[] guids = AssetDatabase.FindAssets("t:texture", checkDirs);
            foreach (var guid in guids)
            {
                ReName(AssetDatabase.GUIDToAssetPath(guid), prefix, oldStr, newStr);
            }

            guids = AssetDatabase.FindAssets("t:material", checkDirs);
            foreach (var guid in guids)
            {
                ReName(AssetDatabase.GUIDToAssetPath(guid), prefix, oldStr, newStr);
            }

            AssetDatabase.SaveAssets();
        }

        private void ReName(string path, string prefix, string oldStr, string newStr)
        {
            int index = path.LastIndexOf("/");
            string old_name = path.Substring(index + 1);
            string new_name = old_name;

            if (!string.IsNullOrEmpty(prefix) && !new_name.StartsWith(prefix))
            {
                new_name = prefix + old_name;
            }

            if (!string.IsNullOrEmpty(oldStr))
            {
                new_name = old_name.Replace(oldStr, newStr);
            }


            if (new_name != old_name)
            {
                AssetDatabase.RenameAsset(path, new_name);
            }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值