特效替換名字

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章