2024-07-04 20:18:43 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using YooAsset;
|
2024-07-05 13:34:17 +08:00
|
|
|
|
using ZGame;
|
2024-07-04 20:18:43 +08:00
|
|
|
|
|
2024-07-05 13:34:17 +08:00
|
|
|
|
namespace ZGame
|
2024-07-04 20:18:43 +08:00
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|