UnityEditor批量替换文件夹下所有问价的后缀名

时间:2024-03-30 20:46:37

UnityEditor批量替换文件夹下所有问价的后缀名

using UnityEditor;
using UnityEngine;
using System.IO;
using System.Collections.Generic;

public class ReplaceByFileDir : EditorWindow
{
    string filePath;
    Rect fileRect;
    string postfixName;
    string deleteFilePostfixName;

    string tipMsg = null;
    MessageType tipMsgType = MessageType.Info;

    bool isReplace = true;
    bool isAdd = false;


    [MenuItem("LuaFramework/批量修改文本后缀")]
    public static void OpenWindow()
    {
        ReplaceByFileDir window = (ReplaceByFileDir)EditorWindow.GetWindow<ReplaceByFileDir>(false, "批量修改文本后缀");
        window.Show();
    }

    void OnGUI()
    {
        EditorGUILayout.Space();
        EditorGUILayout.Space();

        GUILayout.Label("文件夹路径:");

        //获得一个框
        fileRect = EditorGUILayout.GetControlRect(GUILayout.Width(position.width - 25));
        //将上面的框作为文本输入框 
        filePath = EditorGUI.TextField(fileRect, filePath);

        //拖拽文件中
        if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform)
        {
            string[] paths = DragAndDrop.paths;

            if (fileRect.Contains(Event.current.mousePosition) && paths != null && paths.Length > 0)
            {
                if (Directory.Exists(paths[0]))
                {
                    //更改鼠标外观
                    DragAndDrop.visualMode = DragAndDropVisualMode.Generic;

                    //拖拽丢弃时才进行赋值操作
                    if (Event.current.type == EventType.DragPerform)
                        filePath = paths[0];
                }
            }
        }

        EditorGUILayout.Space();
        postfixName = EditorGUILayout.TextField("后缀名:", postfixName);

        EditorGUILayout.Space();

        deleteFilePostfixName = EditorGUILayout.TextField("删除以下后缀文件(空格分隔):", deleteFilePostfixName);

        EditorGUILayout.Space();
        if (isReplace != EditorGUILayout.Toggle("替换", isReplace) && isReplace == false)
        {
            isReplace = !isReplace;
            isAdd = !isReplace;
        }

        if (isAdd != EditorGUILayout.Toggle("追加", isAdd) && isAdd == false)
        {
            isAdd = !isAdd;
            isReplace = !isAdd;
        }

        EditorGUILayout.Space();

        if (GUILayout.Button("批量替换", GUILayout.Height(30)))
        {
            Replace();
        }
        EditorGUILayout.Space();
        if (GUILayout.Button("重置", GUILayout.Height(30)))
        {
            Reset();
        }

        //提示信息
        if (!string.IsNullOrEmpty(tipMsg))
        {
            EditorGUILayout.HelpBox(tipMsg, tipMsgType);
        }


    }

    void Replace()
    {
        if (!Directory.Exists(filePath))
        {
            tipMsg = "错误路径!";
            tipMsgType = MessageType.Error;
            return;
        }
        if (string.IsNullOrEmpty(postfixName))
        {
            tipMsg = "文件后缀为空!";
            tipMsgType = MessageType.Error;
            return;
        }

        //删除文件的后缀
        List<string> delList = new List<string>();
        if (!string.IsNullOrEmpty(deleteFilePostfixName))
        {
            string[] delStrs = deleteFilePostfixName.Split(' ');
            delList.AddRange(delStrs);
        }

        var dir = new DirectoryInfo(filePath);
        //var files = dir.GetFiles("*." + postfixName, SearchOption.AllDirectories);
        var files = dir.GetFiles("*", SearchOption.AllDirectories);
        int count = 0;
        int length = files.Length;
        for (int i = 0; i < length; i++)
        {
            UpdateProgress(i + 1, length, "操作中...");
            var path = files[i].FullName;
            path = path.Replace('\\', '/');
            string fileFullName = files[i].FullName;
            int lastPointIndex = fileFullName.LastIndexOf('.');
            string prePostfixName = fileFullName.Substring(lastPointIndex + 1);

            //是否删除文件
            if (delList.Contains(prePostfixName))
            {
                files[i].Delete();
                continue;
            }

            //相同后缀不替换
            if (prePostfixName.Equals(postfixName))
            {
                continue;
            }

            if (isAdd)   //追加后缀
            {
                string newPath = fileFullName + "_." + postfixName;
                Debug.Log(newPath);
                files[i].MoveTo(newPath);
            }
            else    //替换后缀
            {
                string newPath = fileFullName.Substring(0, lastPointIndex)+ "_." + postfixName;
                files[i].MoveTo(newPath);
            }
        }
        AssetDatabase.Refresh();
        AssetDatabase.SaveAssets();
        tipMsg = "修改成功!修改了" + count + "个文本";
        tipMsgType = MessageType.Info;

        EditorUtility.ClearProgressBar();
    }

    void UpdateProgress(int progress, int progressMax, string desc)
    {
        string title = "Processing...[" + progress + " - " + progressMax + "]";
        float value = (float)progress / (float)progressMax;
        EditorUtility.DisplayProgressBar(title, desc, value);
    }

    void Reset()
    {
        tipMsg = null;
        filePath = null;
        postfixName = null;
        deleteFilePostfixName = null;
        isReplace = true;
        isAdd = false;
    }
}