批量设置
/// <summary> /// 批量设置某路径下的资源 /// </summary> [MenuItem("AssetBundle/Set Asset Bundle Name With Selected Folder (设置某个路径的资源可多选递归)")] public static void SetSelectFolderBundleName() { EditorUtility.ClearProgressBar(); UnityEngine.Object[] selObj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered); int total = selObj.Length; int curr = 0; selectedPicsPathList.Clear(); foreach (UnityEngine.Object item in selObj) { string objPath = AssetDatabase.GetAssetPath(item); DirectoryInfo dirInfo = new DirectoryInfo(objPath); if (dirInfo == null) { UnityEngine.Debug.LogError("******请检查,是否选中了非文件夹对象******"); return; } string[] dirs = System.IO.Directory.GetFileSystemEntries(objPath); for (int j = 0; j < dirs.Length; j++) { string tarPath = dirs[j]; if (System.IO.Directory.Exists(tarPath)) { //文件夹 selectedPicsPathList.AddRange(GetFilesRecursively(tarPath)); } if (System.IO.File.Exists(tarPath)) { //文件 if (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath)) continue; else { if (!selectedPicsPathList.Contains(tarPath)) selectedPicsPathList.Add(tarPath); } } } } for (int i = 0; i < selectedPicsPathList.Count; i++) { DirectoryInfo dirInfo = new DirectoryInfo(selectedPicsPathList[i]); if (dirInfo == null) { UnityEngine.Debug.LogError("******请检查,是否选中了非文件夹对象******"); return; } _dirName = dirInfo.Name; _dirName = dirInfo.Name; string filePath = dirInfo.FullName.Replace('\', '/'); filePath = filePath.Replace(Application.dataPath, "Assets"); AssetImporter ai = AssetImporter.GetAtPath(filePath); curr++; UpdateProgress(curr, total, filePath); UnityEngine.Debug.Log(filePath); string bundle_name; bundle_name = filePath.Replace('/', '-'); ai.assetBundleName = bundle_name.Split('.')[0] + ".assetbundle"; } AssetDatabase.Refresh(); UnityEngine.Debug.Log("******批量设置AssetBundle名称成功******"); EditorUtility.ClearProgressBar(); }
批量清空
/// <summary> /// 批量清空所选文件夹下资源的AssetBundleName.(可递归) /// </summary> [MenuItem("AssetBundle/Reset Asset Bundle Name With Selected Folders(清空所选目录,可多选递归)")] public static void ResetSelectedFoldersFileBundleName() { EditorUtility.ClearProgressBar(); UnityEngine.Object[] selObj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered); int total = selObj.Length; int curr = 0; selectedPicsPathList.Clear(); foreach (UnityEngine.Object item in selObj) { string objPath = AssetDatabase.GetAssetPath(item); DirectoryInfo dirInfo = new DirectoryInfo(objPath); if (dirInfo == null) { UnityEngine.Debug.LogError("******请检查,是否选中了非文件夹对象******"); return; } string[] dirs = System.IO.Directory.GetFileSystemEntries(objPath); for (int j = 0; j < dirs.Length; j++) { string tarPath = dirs[j]; if (System.IO.Directory.Exists(tarPath)) { //文件夹 selectedPicsPathList.AddRange(GetFilesRecursively(tarPath)); } if (System.IO.File.Exists(tarPath)) { //文件 if (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath)) continue; else { if (!selectedPicsPathList.Contains(tarPath)) selectedPicsPathList.Add(tarPath); } } } } _dirName = null; for (int i = 0; i < selectedPicsPathList.Count; i++) { string tarPath = selectedPicsPathList[i]; DirectoryInfo dirInfo = new DirectoryInfo(tarPath); string filePath = dirInfo.FullName.Replace('\', '/'); filePath = filePath.Replace(Application.dataPath, "Assets"); UnityEngine.Debug.Log("filePath=" + filePath); curr++; UpdateProgress(curr, total, filePath); AssetImporter ai = AssetImporter.GetAtPath(filePath); ai.assetBundleName = _dirName; } AssetDatabase.Refresh(); UnityEngine.Debug.Log("******批量清除AssetBundle名称成功******"); EditorUtility.ClearProgressBar(); selectedPicsPathList.Clear(); }
递归方法
/// <summary> /// 递归 /// </summary> /// <param name="folder"></param> /// <returns></returns> static List<string> GetFilesRecursively(string folder) { List<string> tar = new List<string>(); string[] dirs = System.IO.Directory.GetFileSystemEntries(folder); for(int j = 0;j<dirs.Length;j++) { string tarPath = dirs[j]; if (System.IO.Directory.Exists(tarPath)) { //文件夹 tar.AddRange(GetFilesRecursively(tarPath)); } if (System.IO.File.Exists(tarPath)) { //文件 if (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath)) continue; else { if (!tar.Contains(tarPath)) tar.Add(tarPath); } } } return tar; }