/// <summary> /// 检测所有损坏的预制体文件 /// </summary> public static void CheckCorruptionPrefab() { // 获取所有的打包路径 List <string> collectDirectorys = AssetBundleCollectorSettingData.GetAllCollectDirectory(); if (collectDirectorys.Count == 0) { throw new Exception("打包路径列表不能为空"); } // 获取所有资源列表 int checkCount = 0; int invalidCount = 0; string[] findAssets = EditorTools.FindAssets(EAssetSearchType.Prefab, collectDirectorys.ToArray()); foreach (string assetPath in findAssets) { UnityEngine.Object prefab = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityEngine.Object)); if (prefab == null) { invalidCount++; Debug.LogError($"发现损坏预制件:{assetPath}"); } EditorTools.DisplayProgressBar("检测预制件文件是否损坏", ++checkCount, findAssets.Length); } EditorTools.ClearProgressBar(); if (invalidCount == 0) { Debug.Log($"没有发现损坏预制件"); } }
/// <summary> /// 清理无用的材质球属性 /// </summary> private void ClearMaterialUnusedProperty() { // 获取所有的打包路径 List <string> collectDirectorys = AssetBundleCollectorSettingData.GetAllCollectDirectory(); if (collectDirectorys.Count == 0) { throw new Exception("[BuildPackage] 打包路径列表不能为空"); } // 获取所有资源列表 int checkCount = 0; int removedCount = 0; string[] guids = AssetDatabase.FindAssets(string.Empty, collectDirectorys.ToArray()); foreach (string guid in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); string ext = System.IO.Path.GetExtension(assetPath); if (ext == $".{EAssetFileExtension.mat}") { Material mat = AssetDatabase.LoadAssetAtPath <Material>(assetPath); bool removed = EditorTools.ClearMaterialUnusedProperty(mat); if (removed) { removedCount++; Debug.LogWarning($"[Build] 材质球已被处理:{assetPath}"); } } // 进度条相关 checkCount++; EditorUtility.DisplayProgressBar("进度", $"清理无用的材质球属性:{checkCount}/{guids.Length}", (float)checkCount / guids.Length); } EditorUtility.ClearProgressBar(); if (removedCount == 0) { Debug.Log($"没有发现冗余的材质球属性"); } else { AssetDatabase.SaveAssets(); } }
/// <summary> /// 检测预制件是否损坏 /// </summary> private void CheckAllPrefabValid() { // 获取所有的打包路径 List <string> collectDirectorys = AssetBundleCollectorSettingData.GetAllCollectDirectory(); if (collectDirectorys.Count == 0) { throw new Exception("[BuildPackage] 打包路径列表不能为空"); } // 获取所有资源列表 int checkCount = 0; int invalidCount = 0; string[] guids = AssetDatabase.FindAssets(string.Empty, collectDirectorys.ToArray()); foreach (string guid in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(guid); string ext = System.IO.Path.GetExtension(assetPath); if (ext == $".{EAssetFileExtension.prefab}") { UnityEngine.Object prefab = AssetDatabase.LoadAssetAtPath(assetPath, typeof(UnityEngine.Object)); if (prefab == null) { invalidCount++; Debug.LogError($"[Build] 发现损坏预制件:{assetPath}"); } } // 进度条相关 checkCount++; EditorUtility.DisplayProgressBar("进度", $"检测预制件文件是否损坏:{checkCount}/{guids.Length}", (float)checkCount / guids.Length); } EditorUtility.ClearProgressBar(); if (invalidCount == 0) { Debug.Log($"没有发现损坏预制件"); } }
/// <summary> /// 清理无用的材质球属性 /// </summary> public static void ClearMaterialUnusedProperty() { // 获取所有的打包路径 List <string> collectDirectorys = AssetBundleCollectorSettingData.GetAllCollectDirectory(); if (collectDirectorys.Count == 0) { throw new Exception("打包路径列表不能为空"); } // 获取所有资源列表 int checkCount = 0; int removedCount = 0; string[] findAssets = EditorTools.FindAssets(EAssetSearchType.Material, collectDirectorys.ToArray()); foreach (string assetPath in findAssets) { Material mat = AssetDatabase.LoadAssetAtPath <Material>(assetPath); bool removed = EditorTools.ClearMaterialUnusedProperty(mat); if (removed) { removedCount++; Debug.LogWarning($"材质球已被处理:{assetPath}"); } EditorTools.DisplayProgressBar("清理无用的材质球属性", ++checkCount, findAssets.Length); } EditorTools.ClearProgressBar(); if (removedCount == 0) { Debug.Log($"没有发现冗余的材质球属性"); } else { AssetDatabase.SaveAssets(); } }
/// <summary> /// 准备工作 /// </summary> private List <AssetInfo> GetBuildMap() { int progressBarCount = 0; Dictionary <string, AssetInfo> allAsset = new Dictionary <string, AssetInfo>(); // 获取所有的收集路径 List <string> collectDirectorys = AssetBundleCollectorSettingData.GetAllCollectDirectory(); if (collectDirectorys.Count == 0) { throw new Exception("[BuildPatch] 配置的资源收集路径为空"); } // 获取所有资源 string[] guids = AssetDatabase.FindAssets(string.Empty, collectDirectorys.ToArray()); foreach (string guid in guids) { string mainAssetPath = AssetDatabase.GUIDToAssetPath(guid); if (AssetBundleCollectorSettingData.IsIgnoreAsset(mainAssetPath)) { continue; } if (ValidateAsset(mainAssetPath) == false) { continue; } List <AssetInfo> depends = GetDependencies(mainAssetPath); for (int i = 0; i < depends.Count; i++) { AssetInfo assetInfo = depends[i]; if (allAsset.ContainsKey(assetInfo.AssetPath)) { allAsset[assetInfo.AssetPath].DependCount++; } else { allAsset.Add(assetInfo.AssetPath, assetInfo); } } // 进度条 progressBarCount++; EditorUtility.DisplayProgressBar("进度", $"依赖文件分析:{progressBarCount}/{guids.Length}", (float)progressBarCount / guids.Length); } EditorUtility.ClearProgressBar(); progressBarCount = 0; // 移除零依赖的资源 List <string> removeList = new List <string>(); foreach (KeyValuePair <string, AssetInfo> pair in allAsset) { if (pair.Value.IsCollectAsset) { continue; } if (pair.Value.DependCount == 0) { removeList.Add(pair.Value.AssetPath); } } for (int i = 0; i < removeList.Count; i++) { allAsset.Remove(removeList[i]); } // 设置资源标签 foreach (KeyValuePair <string, AssetInfo> pair in allAsset) { SetAssetBundleLabelAndVariant(pair.Value); // 进度条 progressBarCount++; EditorUtility.DisplayProgressBar("进度", $"设置资源标签:{progressBarCount}/{allAsset.Count}", (float)progressBarCount / allAsset.Count); } EditorUtility.ClearProgressBar(); // 返回结果 return(allAsset.Values.ToList()); }