#if UNITY_EDITOR using UnityEditor; using UnityEngine; using System.IO; using System; public class WindowsLowercaseRenamer : EditorWindow { [MenuItem("Assets/Windows安全重命名")] private static void SafeRenameAll() { string dataPath = Global.dataPath; // 新增:处理根目录名称 ProcessRootDirectory(dataPath); // 处理子目录和文件 ProcessDirectoriesRecursive(dataPath); ProcessFilesRecursive(dataPath); AssetDatabase.Refresh(); Debug.Log("安全重命名完成!"); } // 新增方法:处理根目录 private static void ProcessRootDirectory(string originalPath) { try { DirectoryInfo rootInfo = new DirectoryInfo(originalPath); string targetRootName = rootInfo.Name.ToLowerInvariant(); // 如果根目录名称不符合规范 if (rootInfo.Name != targetRootName) { string parentPath = rootInfo.Parent.FullName; string newRootPath = Path.Combine(parentPath, targetRootName); // 特殊处理:根目录重命名需要额外权限 if (Directory.Exists(newRootPath)) { string tempPath = Path.Combine(parentPath, Guid.NewGuid().ToString()); Directory.Move(originalPath, tempPath); Directory.Move(tempPath, newRootPath); } else { Directory.Move(originalPath, newRootPath); } Debug.Log($"根目录重命名成功: {originalPath} → {newRootPath}"); // 更新全局路径(如果需要) Global.dataPath = newRootPath; // 根据实际Global类的实现可能需要调整 } } catch (Exception ex) { Debug.LogError($"根目录重命名失败: {ex.Message}"); } } private static void ProcessDirectoriesRecursive(string path) { foreach (var dirPath in Directory.GetDirectories(path)) { // 递归处理子目录 ProcessDirectoriesRecursive(dirPath); DirectoryInfo dirInfo = new DirectoryInfo(dirPath); string targetName = dirInfo.Name.ToLowerInvariant(); // 仅当实际目录名不符合规范时才重命名 if (dirInfo.Name != targetName) { string parentPath = dirInfo.Parent.FullName; string newPath = Path.Combine(parentPath, targetName); try { // 特殊处理:Windows需要先重命名到临时名称 //if (Directory.Exists(newPath)) //{ // string tempPath = Path.Combine(parentPath, Guid.NewGuid().ToString()); // Directory.Move(dirPath, tempPath); // Directory.Move(tempPath, newPath); //} //else //{ Directory.Move(dirPath, newPath); //} Debug.Log($"目录重命名成功: {dirPath} → {newPath}"); } catch (Exception ex) { Debug.LogError($"目录重命名失败: {dirPath}\n{ex.Message}"); } } } } private static void ProcessFilesRecursive(string path) { foreach (var filePath in Directory.GetFiles(path)) { FileInfo fileInfo = new FileInfo(filePath); string targetName = Path.GetFileNameWithoutExtension(fileInfo.Name).ToLowerInvariant(); string targetExt = fileInfo.Extension.ToLowerInvariant(); string newFileName = $"{targetName}{targetExt}"; // 比较实际文件名 if (fileInfo.Name != newFileName) { string newPath = Path.Combine(fileInfo.DirectoryName, newFileName); try { // Windows特殊处理:如果目标存在但大小写不同 if (File.Exists(newPath)) { string tempPath = Path.Combine(fileInfo.DirectoryName, Guid.NewGuid().ToString() + fileInfo.Extension); File.Move(filePath, tempPath); File.Move(tempPath, newPath); } else { File.Move(filePath, newPath); } Debug.Log($"文件重命名成功: {filePath} → {newPath}"); } catch (Exception ex) { Debug.LogError($"文件重命名失败: {filePath}\n{ex.Message}"); } } } // 递归子目录 foreach (var dirPath in Directory.GetDirectories(path)) { ProcessFilesRecursive(dirPath); } } } #endif