using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using UnityEditor; using UnityEngine; public class ReplaceFile { static string selectFolderPath; [MenuItem("Tool/ReplaceFile")] public static void Start() { selectFolderPath = SelectFolder(); if (!string.IsNullOrEmpty(selectFolderPath)) { if (Application.platform == RuntimePlatform.OSXEditor) { Debug.LogError("目标:" + selectFolderPath); } else { Debug.LogError("目标:" + selectFolderPath); GetList(); } } else Debug.LogError("未选择路径"); } public static void GetList() { Debug.LogError(selectFolderPath); DirectoryInfo folder = new DirectoryInfo(selectFolderPath); DirectoryInfo[] infos = folder.GetDirectories(); for (int i = 0; i < infos.Length; i++) { EditorUtility.DisplayProgressBar("获取图片", "获取图片中:" + (float)i + "/" + infos.Length, (float)i / infos.Length); //每个文件夹路径 string path = selectFolderPath + "\\" + infos[i].Name; if (Directory.Exists(path)) { DirectoryInfo direction = new DirectoryInfo(path); FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories); for (int j = 0; j < files.Length; j++) { if (files[j].Name.EndsWith(".meta")) { continue; } string[] type = files[j].Name.Split('.'); if (type.Length == 2) { if (type[1] == "img" | type[1] == "jpg") { Debug.Log(files[j].Name); Replace(files[j].Name, path + "//" + files[j].Name); } } } } } EditorUtility.ClearProgressBar(); } public static void Replace(string name, string srcPath) { string allFolderPath = Application.dataPath + "\\Resources"; DirectoryInfo folder = new DirectoryInfo(allFolderPath); DirectoryInfo[] infos = folder.GetDirectories(); for (int i = 0; i < infos.Length; i++) { //每个文件夹路径 string path = allFolderPath + "\\" + infos[i].Name; Debug.Log(path); if (Directory.Exists(path)) { DirectoryInfo direction = new DirectoryInfo(path); FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories); for (int j = 0; j < files.Length; j++) { if (files[j].Name.EndsWith(".meta")) { continue; } Debug.Log(files[j].Name); if (files[j].Name == name) { File.Copy(srcPath, path + "//" + files[j].Name, true); return; } } } } } /// /// 选择文件夹 /// /// 目标路径 private static string SelectFolder() { string path = EditorUtility.OpenFolderPanel("选择目标路径", System.Environment.CurrentDirectory, ""); return path; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OpenDialogFile { public int structSize = 0; public IntPtr dlgOwner = IntPtr.Zero; public IntPtr instance = IntPtr.Zero; public String filter = null; public String customFilter = null; public int maxCustFilter = 0; public int filterIndex = 0; public String file = null; public int maxFile = 0; public String fileTitle = null; public int maxFileTitle = 0; public String initialDir = null; public String title = null; public int flags = 0; public short fileOffset = 0; public short fileExtension = 0; public String defExt = null; public IntPtr custData = IntPtr.Zero; public IntPtr hook = IntPtr.Zero; public String templateName = null; public IntPtr reservedPtr = IntPtr.Zero; public int reservedInt = 0; public int flagsEx = 0; } [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] public class OpenDialogDir { public IntPtr hwndOwner = IntPtr.Zero; public IntPtr pidlRoot = IntPtr.Zero; public String pszDisplayName = null; public String lpszTitle = null; public UInt32 ulFlags = 0; public IntPtr lpfn = IntPtr.Zero; public IntPtr lParam = IntPtr.Zero; public int iImage = 0; } public class DllOpenFileDialog { [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetOpenFileName([In, Out] OpenDialogFile ofn); [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool GetSaveFileName([In, Out] OpenDialogFile ofn); [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn); [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName); } public class MessageBoxTip { [DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)] public static extern int MessageBox(IntPtr handle, String message, String title, int type); } }