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("*", 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;
}
}