75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
|
|
using System;
|
|||
|
|
using System.Linq;
|
|||
|
|
using AlicizaX.Resource.Runtime;
|
|||
|
|
using AlicizaX.Runtime;
|
|||
|
|
using Cysharp.Threading.Tasks;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using Object = UnityEngine.Object;
|
|||
|
|
|
|||
|
|
namespace AlicizaX.UI.Runtime
|
|||
|
|
{
|
|||
|
|
public static class UILoaderFactory
|
|||
|
|
{
|
|||
|
|
private static readonly IResourceManager ResourceManager;
|
|||
|
|
|
|||
|
|
static UILoaderFactory()
|
|||
|
|
{
|
|||
|
|
ResourceManager = SysModuleCenter.GetModule<IResourceManager>();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static async UniTask LoadUIResourcesAsync(UIMetadata meta, Transform parent, UIBase owner = null)
|
|||
|
|
{
|
|||
|
|
if (meta.State != UIState.CreatedUI) return;
|
|||
|
|
|
|||
|
|
GameObject obj = meta.LoadType == EUIResLoadType.AssetBundle
|
|||
|
|
? await ResourceManager.LoadGameObjectAsync(meta.ResLocation, parent)
|
|||
|
|
: await InstantiateResourceAsync(meta, parent);
|
|||
|
|
if (owner is null)
|
|||
|
|
{
|
|||
|
|
ValidateAndBind(meta, obj);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
ValidateAndBindWidget(meta, obj, owner);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static void LoadUIResourcesSync(UIMetadata meta, Transform parent, UIBase owner = null)
|
|||
|
|
{
|
|||
|
|
if (meta.State != UIState.CreatedUI) return;
|
|||
|
|
|
|||
|
|
GameObject obj = meta.LoadType == EUIResLoadType.AssetBundle
|
|||
|
|
? ResourceManager.LoadGameObject(meta.ResLocation, parent)
|
|||
|
|
: Object.Instantiate(Resources.Load<GameObject>(meta.ResLocation), parent);
|
|||
|
|
|
|||
|
|
if (owner is null)
|
|||
|
|
{
|
|||
|
|
ValidateAndBind(meta, obj);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
ValidateAndBindWidget(meta, obj, owner);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void ValidateAndBind(UIMetadata meta, GameObject obj)
|
|||
|
|
{
|
|||
|
|
if (!obj) throw new NullReferenceException($"UI resource load failed: {meta.ResLocation}");
|
|||
|
|
(meta.View as UIWindow)?.BindUIHolder(obj);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static void ValidateAndBindWidget(UIMetadata meta, GameObject obj, UIBase owner)
|
|||
|
|
{
|
|||
|
|
if (!obj) throw new NullReferenceException($"UI resource load failed: {meta.ResLocation}");
|
|||
|
|
(meta.View as UIWidget)?.BindUIHolder(obj, owner);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static async UniTask<GameObject> InstantiateResourceAsync(UIMetadata meta, Transform parent)
|
|||
|
|
{
|
|||
|
|
GameObject prefab = (GameObject)await Resources.LoadAsync<GameObject>(meta.ResLocation);
|
|||
|
|
GameObject[] instance = await Object.InstantiateAsync(prefab, 1, parent).ToUniTask();
|
|||
|
|
return instance.FirstOrDefault();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|