93 lines
3.0 KiB
C#
93 lines
3.0 KiB
C#
using System.Collections.Generic;
|
||
using Cysharp.Threading.Tasks;
|
||
using YooAsset;
|
||
using ZGame;
|
||
|
||
namespace ZGame
|
||
{
|
||
//
|
||
public class ResourceComponent
|
||
{
|
||
private static ResourceComponent instance;
|
||
|
||
public static ResourceComponent Instance
|
||
{
|
||
get
|
||
{
|
||
if (instance == null)
|
||
{
|
||
instance = new ResourceComponent();
|
||
}
|
||
|
||
return instance;
|
||
}
|
||
}
|
||
|
||
private bool isLoadFinish;
|
||
private EPlayMode playMode;
|
||
|
||
ResourceUpdatePackageHandle handle;
|
||
ResourcePackage _defaultPackage;
|
||
|
||
public ResourceComponent()
|
||
{
|
||
isLoadFinish = false;
|
||
EventManager.Instance.Subscribe(UpdaterDoneEventArgs.EventId, UpdaterDoneEvent);
|
||
YooAssets.Initialize();
|
||
}
|
||
|
||
private void UpdaterDoneEvent(object sender, GameEventArgs e)
|
||
{
|
||
var args = e as UpdaterDoneEventArgs;
|
||
isLoadFinish = true;
|
||
}
|
||
|
||
|
||
public async UniTask InitLoadModeAsync(EPlayMode playMode, string packageName)
|
||
{
|
||
this.playMode = playMode;
|
||
handle = new ResourceUpdatePackageHandle(playMode, packageName, EDefaultBuildPipeline.BuiltinBuildPipeline.ToString());
|
||
await handle.InitPackageAsync();
|
||
|
||
while (!this.isLoadFinish)
|
||
{
|
||
await UniTask.Yield();
|
||
}
|
||
|
||
_defaultPackage = YooAssets.GetPackage("DefaultPackage");
|
||
YooAssets.SetDefaultPackage(_defaultPackage);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 主要用来加载dll config aotdll,因为这时候纤程还没创建,无法使用ResourcesLoaderComponent。
|
||
/// 游戏中的资源应该使用ResourcesLoaderComponent来加载
|
||
/// </summary>
|
||
public async UniTask<T> LoadAssetAsync<T>(string location) where T : UnityEngine.Object
|
||
{
|
||
AssetHandle handle = YooAssets.LoadAssetAsync<T>(location);
|
||
await handle.Task;
|
||
T t = (T)handle.AssetObject;
|
||
handle.Release();
|
||
return t;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 主要用来加载dll config aotdll,因为这时候纤程还没创建,无法使用ResourcesLoaderComponent。
|
||
/// 游戏中的资源应该使用ResourcesLoaderComponent来加载
|
||
/// </summary>
|
||
public async UniTask<Dictionary<string, T>> LoadAllAssetsAsync<T>(string location) where T : UnityEngine.Object
|
||
{
|
||
AllAssetsHandle allAssetsOperationHandle = YooAssets.LoadAllAssetsAsync<T>(location);
|
||
await allAssetsOperationHandle.Task;
|
||
Dictionary<string, T> dictionary = new Dictionary<string, T>();
|
||
foreach (UnityEngine.Object assetObj in allAssetsOperationHandle.AllAssetObjects)
|
||
{
|
||
T t = assetObj as T;
|
||
dictionary.Add(t.name, t);
|
||
}
|
||
|
||
allAssetsOperationHandle.Release();
|
||
return dictionary;
|
||
}
|
||
}
|
||
} |