123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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<string> pathes = new List<string>();
- List<string> bundleNames = new List<string>();
- List<string> bundleVariants = new List<string>();
- 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<string> pathes, List<string> bundleNames, List<string> 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;
- }
- }
- }
|