using System; using System.Collections; using System.Collections.Generic; using System.Collections.Generic; using UnityEngine; using UnityEngine.U2D; using YooAsset; using Object = UnityEngine.Object; public class AssetBundleManager : MonoBehaviour { public static AssetBundleManager ins; public EPlayMode PlayMode = EPlayMode.EditorSimulateMode; public string CDN = "https://qq.tencent.com/"; private ResourcePackage package; private Dictionary _atlasData; private void Awake() { } /// /// 初始化ab包 /// public void InitAsset(Action action) { ins = this; return; YooAssets.Initialize(); // 设置默认的资源包 var gamePackage = YooAssets.TryGetPackage("DefaultPackage"); YooAssets.SetDefaultPackage(gamePackage); ins = this; StartCoroutine(InitPackage(action)); } private IEnumerator InitPackage(Action action) { var playMode = PlayMode; var packageName = "DefaultPackage"; var buildPipeline = EDefaultBuildPipeline.BuiltinBuildPipeline; // 创建资源包裹类 package = YooAssets.TryGetPackage(packageName); if (package == null) package = YooAssets.CreatePackage(packageName); // 编辑器下的模拟模式 InitializationOperation initializationOperation = null; if (playMode == EPlayMode.EditorSimulateMode) { var simulateBuildResult = EditorSimulateModeHelper.SimulateBuild(buildPipeline, packageName); var createParameters = new EditorSimulateModeParameters(); createParameters.EditorFileSystemParameters = FileSystemParameters.CreateDefaultEditorFileSystemParameters(simulateBuildResult); initializationOperation = package.InitializeAsync(createParameters); } // 单机运行模式 if (playMode == EPlayMode.OfflinePlayMode) { var createParameters = new OfflinePlayModeParameters(); createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters(); initializationOperation = package.InitializeAsync(createParameters); } Debug.LogError(CDN); #if UNITY_WEBGL && WEIXINMINIGAME // WebGL运行模式 if (playMode == EPlayMode.WebPlayMode) { var createParameters = new WebPlayModeParameters(); IRemoteServices sever = new RemoveServer(CDN); createParameters.WebFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(sever); initializationOperation = package.InitializeAsync(createParameters); } #endif //WebGL运行模式 // if (playMode == EPlayMode.WebPlayMode) // { // var createParameters = new WebPlayModeParameters(); // createParameters.WebFileSystemParameters = FileSystemParameters.CreateDefaultWebFileSystemParameters(CDN); // initializationOperation = package.InitializeAsync(createParameters); // } yield return initializationOperation; var operationver =package.RequestPackageVersionAsync(); yield return operationver; if (operationver.Status != EOperationStatus.Succeed) yield break; // 3. 传入的版本信息更新资源清单 var operationManifestAsync =package.UpdatePackageManifestAsync(operationver.PackageVersion); yield return operationManifestAsync; if (operationManifestAsync.Status != EOperationStatus.Succeed) yield break; var download=package.CreateResourceDownloader(3, 10); download.BeginDownload(); LoadAtlas(); action.Invoke(); } /// /// 加载ab资源 /// /// /// public void LoadAsset(string path,Action action) { // var obj = package.LoadAssetSync(path); var obj = Resources.Load(path); StartCoroutine(LodObj(obj, action)); } IEnumerator LodObj(GameObject obj,Action action) { yield return null; obj.SetActive(false); var lodObj = Instantiate(obj, transform); action(lodObj); } public void LoadAtlas() { _atlasData = new Dictionary(); LoadAsset(AtlasPath(AtlasType.ItemIcon), (obj) => { _atlasData.Add(AtlasType.ItemIcon,obj); }); LoadAsset(AtlasPath(AtlasType.NpcIcon), (obj) => { _atlasData.Add(AtlasType.NpcIcon,obj); }); LoadAsset(AtlasPath(AtlasType.ButtleIcon), (obj) => { _atlasData.Add(AtlasType.ButtleIcon,obj); }); LoadAsset(AtlasPath(AtlasType.DrawingIcon), (obj) => { _atlasData.Add(AtlasType.DrawingIcon,obj); }); LoadAsset(AtlasPath(AtlasType.AtlasBattleBack), (obj) => { _atlasData.Add(AtlasType.AtlasBattleBack,obj); }); } public void LoadAsset(string path, Action action) { // var obj = package.LoadAssetSync(path); var obj = Resources.Load(path); // StartCoroutine(LodObj(obj.AssetObject as SpriteAtlas, action)); action(obj); } private string AtlasPath(AtlasType atlasType) { switch (atlasType) { case AtlasType.ItemIcon: return "UI/AtlasIcon"; case AtlasType.NpcIcon: return "UI/AtlasNpc"; case AtlasType.ButtleIcon: return "UI/AtlasButtle"; case AtlasType.DrawingIcon: return "UI/AtlasDrawing"; case AtlasType.AtlasBattleBack: return "UI/AtlasBattleBack"; default: throw new ArgumentOutOfRangeException(nameof(atlasType), atlasType, null); } } private SpriteAtlas targetAtlas; public Sprite Sprite(string spriteName,AtlasType type) { if (_atlasData.TryGetValue(type, out targetAtlas)) { return targetAtlas.GetSprite(spriteName); } return null; } public void LoadAsset(string path, Action action) { SubAssetsHandle handle = package.LoadSubAssetsAsync(path); var sprite = handle.GetSubAssetObject("path"); action(sprite); } public void LoadAsset(string path, Action action) { } public void LoadAsset(string path, Action action) { var obj = package.LoadAssetSync(path); action(obj.AssetObject as AudioClip); } public void GetPoolItem(string path, Action action) { } class RemoveServer : IRemoteServices { //注意CDN地址与Yoo远端加载地址需一致,才会触发缓存 //https://wechat-miniprogram.github.io/minigame-unity-webgl-transform/Design/FileCache.html string CDN; public RemoveServer(string cdn) { CDN = cdn; } //如果不一致,需要修改缓存目录,_fileCacheRoot //远端目录结构为: //CDN: //webgl: // StreamingAssets // xxwebgl.wasm.code.unityweb.wasm.br // xxx.version // xxx.hash // xx/bundle public string GetRemoteFallbackURL(string fileName) { return CDN + fileName; } public string GetRemoteMainURL(string fileName) { return CDN + fileName; } } } /// /// 运行模式 /// public enum EPlayMode { /// /// 编辑器下的模拟模式 /// EditorSimulateMode, /// /// 离线运行模式 /// OfflinePlayMode, /// /// WebGL运行模式 /// WebPlayMode, } public enum AtlasType { ItemIcon, NpcIcon, ButtleIcon, DrawingIcon, AtlasBattleBack }