WXGame/Blacksmith/Assets/Script/AssetBundleManager.cs

250 lines
7.6 KiB
C#
Raw Normal View History

2024-11-29 21:37:01 +08:00
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;
2024-12-11 20:35:20 +08:00
public string CDN = "https://qq.tencent.com/";
2024-11-29 21:37:01 +08:00
private ResourcePackage package;
2024-12-04 17:26:27 +08:00
private Dictionary<AtlasType, SpriteAtlas> _atlasData;
2024-11-29 21:37:01 +08:00
private void Awake()
{
}
/// <summary>
/// 初始化ab包
/// </summary>
public void InitAsset(Action action)
{
2024-12-11 20:35:20 +08:00
ins = this;
return;
2024-11-29 21:37:01 +08:00
YooAssets.Initialize();
// 设置默认的资源包
var gamePackage = YooAssets.TryGetPackage("DefaultPackage");
YooAssets.SetDefaultPackage(gamePackage);
ins = this;
StartCoroutine(InitPackage(action));
}
2024-12-04 17:26:27 +08:00
private IEnumerator InitPackage(Action action)
{
var playMode = PlayMode;
var packageName = "DefaultPackage";
var buildPipeline = EDefaultBuildPipeline.BuiltinBuildPipeline;
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
// 创建资源包裹类
package = YooAssets.TryGetPackage(packageName);
if (package == null)
package = YooAssets.CreatePackage(packageName);
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
// 编辑器下的模拟模式
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);
}
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
// 单机运行模式
if (playMode == EPlayMode.OfflinePlayMode)
{
var createParameters = new OfflinePlayModeParameters();
createParameters.BuildinFileSystemParameters = FileSystemParameters.CreateDefaultBuildinFileSystemParameters();
initializationOperation = package.InitializeAsync(createParameters);
}
2024-12-11 20:35:20 +08:00
Debug.LogError(CDN);
#if UNITY_WEBGL && WEIXINMINIGAME
2024-12-04 17:26:27 +08:00
// WebGL运行模式
if (playMode == EPlayMode.WebPlayMode)
{
2024-12-11 20:35:20 +08:00
var createParameters = new WebPlayModeParameters();
IRemoteServices sever = new RemoveServer(CDN);
createParameters.WebFileSystemParameters = WechatFileSystemCreater.CreateWechatFileSystemParameters(sever);
2024-12-04 17:26:27 +08:00
initializationOperation = package.InitializeAsync(createParameters);
}
2024-12-11 20:35:20 +08:00
#endif
//WebGL运行模式
// if (playMode == EPlayMode.WebPlayMode)
// {
// var createParameters = new WebPlayModeParameters();
// createParameters.WebFileSystemParameters = FileSystemParameters.CreateDefaultWebFileSystemParameters(CDN);
// initializationOperation = package.InitializeAsync(createParameters);
// }
2024-12-04 17:26:27 +08:00
yield return initializationOperation;
var operationver =package.RequestPackageVersionAsync();
yield return operationver;
if (operationver.Status != EOperationStatus.Succeed)
yield break;
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
// 3. 传入的版本信息更新资源清单
var operationManifestAsync =package.UpdatePackageManifestAsync(operationver.PackageVersion);
yield return operationManifestAsync;
if (operationManifestAsync.Status != EOperationStatus.Succeed)
yield break;
2024-12-11 20:35:20 +08:00
var download=package.CreateResourceDownloader(3, 10);
download.BeginDownload();
2024-12-04 17:26:27 +08:00
LoadAtlas();
action.Invoke();
}
/// <summary>
/// 加载ab资源
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public void LoadAsset(string path,Action<GameObject> action)
{
2024-12-11 20:35:20 +08:00
// var obj = package.LoadAssetSync<GameObject>(path);
var obj = Resources.Load<GameObject>(path);
StartCoroutine(LodObj(obj, action));
2024-12-04 17:26:27 +08:00
}
IEnumerator LodObj(GameObject obj,Action<GameObject> action)
{
yield return null;
obj.SetActive(false);
var lodObj = Instantiate(obj, transform);
action(lodObj);
}
2024-12-11 20:35:20 +08:00
public void LoadAtlas()
2024-12-04 17:26:27 +08:00
{
_atlasData = new Dictionary<AtlasType, SpriteAtlas>();
LoadAsset(AtlasPath(AtlasType.ItemIcon), (obj) =>
2024-11-29 21:37:01 +08:00
{
2024-12-04 17:26:27 +08:00
_atlasData.Add(AtlasType.ItemIcon,obj);
});
LoadAsset(AtlasPath(AtlasType.NpcIcon), (obj) =>
2024-11-29 21:37:01 +08:00
{
2024-12-04 17:26:27 +08:00
_atlasData.Add(AtlasType.NpcIcon,obj);
});
2024-12-11 23:46:58 +08:00
LoadAsset(AtlasPath(AtlasType.ButtleIcon), (obj) =>
{
_atlasData.Add(AtlasType.ButtleIcon,obj);
});
2024-12-04 17:26:27 +08:00
}
public void LoadAsset(string path, Action<SpriteAtlas> action)
{
2024-12-11 20:35:20 +08:00
// var obj = package.LoadAssetSync<SpriteAtlas>(path);
var obj = Resources.Load<SpriteAtlas>(path);
2024-12-04 17:26:27 +08:00
// StartCoroutine(LodObj(obj.AssetObject as SpriteAtlas, action));
2024-12-11 20:35:20 +08:00
action(obj);
2024-12-04 17:26:27 +08:00
}
private string AtlasPath(AtlasType atlasType)
{
switch (atlasType)
2024-11-29 21:37:01 +08:00
{
2024-12-04 17:26:27 +08:00
case AtlasType.ItemIcon:
2024-12-11 20:35:20 +08:00
return "UI/AtlasIcon";
2024-12-04 17:26:27 +08:00
case AtlasType.NpcIcon:
2024-12-11 20:35:20 +08:00
return "UI/AtlasNpc";
2024-12-11 23:46:58 +08:00
case AtlasType.ButtleIcon:
return "UI/AtlasButtle";
2024-12-04 17:26:27 +08:00
default:
throw new ArgumentOutOfRangeException(nameof(atlasType), atlasType, null);
2024-11-29 21:37:01 +08:00
}
2024-12-04 17:26:27 +08:00
}
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
private SpriteAtlas targetAtlas;
public Sprite Sprite(string spriteName,AtlasType type)
{
if (_atlasData.TryGetValue(type, out targetAtlas))
2024-11-29 21:37:01 +08:00
{
2024-12-04 17:26:27 +08:00
return targetAtlas.GetSprite(spriteName);
2024-11-29 21:37:01 +08:00
}
2024-12-04 17:26:27 +08:00
return null;
}
public void LoadAsset(string path, Action<Sprite> action)
{
SubAssetsHandle handle = package.LoadSubAssetsAsync<Sprite>(path);
var sprite = handle.GetSubAssetObject<Sprite>("path");
action(sprite);
}
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
public void LoadAsset<T0>(string path, Action<T0> action)
{
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
}
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
public void LoadAsset(string path, Action<AudioClip> action)
{
var obj = package.LoadAssetSync<AudioClip>(path);
action(obj.AssetObject as AudioClip);
}
2024-11-29 21:37:01 +08:00
2024-12-04 17:26:27 +08:00
public void GetPoolItem(string path, Action<AudioClip> action)
{
}
2024-12-11 20:35:20 +08:00
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;
}
}
2024-11-29 21:37:01 +08:00
}
/// <summary>
/// 运行模式
/// </summary>
public enum EPlayMode
{
/// <summary>
/// 编辑器下的模拟模式
/// </summary>
EditorSimulateMode,
/// <summary>
/// 离线运行模式
/// </summary>
OfflinePlayMode,
/// <summary>
/// WebGL运行模式
/// </summary>
WebPlayMode,
2024-12-04 17:26:27 +08:00
}
public enum AtlasType
{
ItemIcon,
NpcIcon,
2024-12-11 23:46:58 +08:00
ButtleIcon,
2024-11-29 21:37:01 +08:00
}