WXGame/Blacksmith/Assets/Script/AssetBundleManager.cs
2024-12-04 17:26:27 +08:00

197 lines
5.9 KiB
C#

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;
private ResourcePackage package;
private Dictionary<AtlasType, SpriteAtlas> _atlasData;
private void Awake()
{
}
/// <summary>
/// 初始化ab包
/// </summary>
public void InitAsset(Action action)
{
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);
}
// WebGL运行模式
if (playMode == EPlayMode.WebPlayMode)
{
var createParameters = new WebPlayModeParameters();
createParameters.WebFileSystemParameters = FileSystemParameters.CreateDefaultWebFileSystemParameters();
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;
LoadAtlas();
action.Invoke();
}
/// <summary>
/// 加载ab资源
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public void LoadAsset(string path,Action<GameObject> action)
{
var obj = package.LoadAssetSync<GameObject>(path);
StartCoroutine(LodObj(obj.AssetObject as GameObject, action));
}
IEnumerator LodObj(GameObject obj,Action<GameObject> action)
{
yield return null;
obj.SetActive(false);
var lodObj = Instantiate(obj, transform);
action(lodObj);
}
private void LoadAtlas()
{
_atlasData = new Dictionary<AtlasType, SpriteAtlas>();
LoadAsset(AtlasPath(AtlasType.ItemIcon), (obj) =>
{
_atlasData.Add(AtlasType.ItemIcon,obj);
});
LoadAsset(AtlasPath(AtlasType.NpcIcon), (obj) =>
{
_atlasData.Add(AtlasType.NpcIcon,obj);
});
}
public void LoadAsset(string path, Action<SpriteAtlas> action)
{
var obj = package.LoadAssetSync<SpriteAtlas>(path);
// StartCoroutine(LodObj(obj.AssetObject as SpriteAtlas, action));
action(obj.AssetObject as SpriteAtlas);
}
private string AtlasPath(AtlasType atlasType)
{
switch (atlasType)
{
case AtlasType.ItemIcon:
return "SpriteAtlas_AtlasIcon";
case AtlasType.NpcIcon:
return "SpriteAtlas_AtlasNpc";
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<Sprite> action)
{
SubAssetsHandle handle = package.LoadSubAssetsAsync<Sprite>(path);
var sprite = handle.GetSubAssetObject<Sprite>("path");
action(sprite);
}
public void LoadAsset<T0>(string path, Action<T0> action)
{
}
public void LoadAsset(string path, Action<AudioClip> action)
{
var obj = package.LoadAssetSync<AudioClip>(path);
action(obj.AssetObject as AudioClip);
}
public void GetPoolItem(string path, Action<AudioClip> action)
{
}
}
/// <summary>
/// 运行模式
/// </summary>
public enum EPlayMode
{
/// <summary>
/// 编辑器下的模拟模式
/// </summary>
EditorSimulateMode,
/// <summary>
/// 离线运行模式
/// </summary>
OfflinePlayMode,
/// <summary>
/// WebGL运行模式
/// </summary>
WebPlayMode,
}
public enum AtlasType
{
ItemIcon,
NpcIcon,
}