ZK_Framework/Assets/Scripts/Mono/ResourceComponent/ResourceComponent.cs

93 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Game;
using YooAsset;
namespace Mono
{
//
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;
}
}
}