namespace AtlasUtility { using UnityEditor; using UnityEngine; using System; using System.Collections; using System.Collections.Generic; public class AssetBundleManager { public static void SwitchAssetBundle(string editorPath, string mobilePath, string desktopPath, Platform toPlatform) { AssetImporter mobileImporter = AssetImporter.GetAtPath(mobilePath); AssetImporter desktopImporter = AssetImporter.GetAtPath(desktopPath); AssetImporter editorImporter = AssetImporter.GetAtPath(editorPath); if (toPlatform == Platform.Editor) { if (editorImporter == null) { Debug.Log("没有编辑器版本"); return; } } else if (toPlatform == Platform.Mobile) { if (mobileImporter == null) { Debug.Log("没有移动版本"); return; } } else if (toPlatform == Platform.Desktop) { if (desktopImporter == null) { Debug.LogWarning("没有桌面版本"); return; } } else { throw new Exception(); } List pathes = new List(); List bundleNames = new List(); List bundleVariants = new List(); if (mobileImporter != null) { if (mobileImporter.assetBundleName != "" || mobileImporter.assetBundleVariant != "") { pathes.Add(mobileImporter.assetPath); } } if (desktopImporter != null) { if (desktopImporter.assetBundleName != "" || desktopImporter.assetBundleVariant != "") { pathes.Add(desktopImporter.assetPath); } } if (editorImporter != null) { if (editorImporter.assetBundleName != "" || editorImporter.assetBundleVariant != "") { pathes.Add(editorImporter.assetPath); } } if (pathes.Count == 0) { return; } if (!IsSameAssetBundle(pathes, bundleNames, bundleVariants)) { Debug.LogWarning($"{editorPath} 的不同版本有不同的AssetBundle"); return; } if (editorImporter != null) { editorImporter.SetAssetBundleNameAndVariant("", ""); editorImporter.SaveAndReimport(); } if (mobileImporter != null) { mobileImporter.SetAssetBundleNameAndVariant("", ""); mobileImporter.SaveAndReimport(); } if (desktopImporter != null) { desktopImporter.SetAssetBundleNameAndVariant("", ""); desktopImporter.SaveAndReimport(); } if (toPlatform == Platform.Editor) { editorImporter.assetBundleName = bundleNames[0]; editorImporter.assetBundleVariant = bundleVariants[0]; editorImporter.SaveAndReimport(); } else if (toPlatform == Platform.Mobile) { mobileImporter.assetBundleName = bundleNames[0]; mobileImporter.assetBundleVariant = bundleVariants[0]; mobileImporter.SaveAndReimport(); } else if (toPlatform == Platform.Desktop) { desktopImporter.assetBundleName = bundleNames[0]; desktopImporter.assetBundleVariant = bundleVariants[0]; desktopImporter.SaveAndReimport(); } else { throw new Exception(); } } public static bool IsSameAssetBundle(List pathes, List bundleNames, List bundleVariants) { for (int i = 0; i < pathes.Count; i++) { if (bundleNames.Count > 0 && !bundleNames.Contains(AssetImporter.GetAtPath(pathes[i]).assetBundleName)) { return false; } else { bundleNames.Add(AssetImporter.GetAtPath(pathes[i]).assetBundleName); } if (bundleVariants.Count > 0 && !bundleVariants.Contains(AssetImporter.GetAtPath(pathes[i]).assetBundleVariant)) { return false; } else { bundleVariants.Add(AssetImporter.GetAtPath(pathes[i]).assetBundleVariant); } } return true; } } }