From 4911ec074cfaecb0c853aed2e9cca04bd7c6722c Mon Sep 17 00:00:00 2001
From: ALEXTANGXIAO <574809918@qq.com>
Date: Sun, 11 Aug 2024 10:24:27 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EGetUIAsync=EF=BC=8CShowUIAsyn?=
=?UTF-8?q?cAwait?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
新增GetUIAsync,ShowUIAsyncAwait
---
.../Runtime/Modules/UIModule/UIModule.cs | 125 ++++++++++++++++++
1 file changed, 125 insertions(+)
diff --git a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs
index 18bf7a1b..cd9e8ad5 100644
--- a/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs
+++ b/UnityProject/Assets/TEngine/Runtime/Modules/UIModule/UIModule.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
using YooAsset;
@@ -267,6 +268,16 @@ namespace TEngine
{
ShowUIImp(typeof(T), false, userDatas);
}
+
+ ///
+ /// 异步打开窗口。
+ ///
+ /// 用户自定义数据。
+ /// 打开窗口操作句柄。
+ public async UniTask ShowUIAsyncAwait(params System.Object[] userDatas) where T : UIWindow
+ {
+ return await ShowUIAwaitImp(typeof(T), true, userDatas);
+ }
///
/// 同步打开窗口。
@@ -298,6 +309,38 @@ namespace TEngine
window.InternalLoad(window.AssetName, OnWindowPrepare, isAsync, userDatas).Forget();
}
}
+
+ private async UniTask ShowUIAwaitImp(Type type, bool isAsync, params System.Object[] userDatas)
+ {
+ string windowName = type.FullName;
+
+ // 如果窗口已经存在
+ if (IsContains(windowName))
+ {
+ UIWindow window = GetWindow(windowName);
+ Pop(window); //弹出窗口
+ Push(window); //重新压入
+ window.TryInvoke(OnWindowPrepare, userDatas);
+ return window;
+ }
+ else
+ {
+ UIWindow window = CreateInstance(type);
+ Push(window); //首次压入
+ window.InternalLoad(window.AssetName, OnWindowPrepare, isAsync, userDatas).Forget();
+ float time = 0f;
+ while (!window.IsLoadDone)
+ {
+ time += Time.time;
+ if (time > 60f)
+ {
+ break;
+ }
+ await UniTask.Yield();
+ }
+ return window;
+ }
+ }
///
/// 关闭窗口
@@ -459,6 +502,88 @@ namespace TEngine
return window;
}
+
+ ///
+ /// 异步获取窗口。
+ ///
+ /// 打开窗口操作句柄。
+ public async UniTask GetUIAsyncAwait() where T : UIWindow
+ {
+ string windowName = typeof(T).FullName;
+ var window = GetWindow(windowName);
+ if (window == null)
+ {
+ return null;
+ }
+
+ var ret = window as T;
+
+ if (ret == null)
+ {
+ return null;
+ }
+
+ if (ret.IsLoadDone)
+ {
+ return ret;
+ }
+
+ float time = 0f;
+ while (!ret.IsLoadDone)
+ {
+ time += Time.time;
+ if (time > 60f)
+ {
+ break;
+ }
+ await UniTask.Yield();
+ }
+ return ret;
+ }
+
+ ///
+ /// 异步获取窗口。
+ ///
+ /// 回调。
+ /// 打开窗口操作句柄。
+ public void GetUIAsync(Action callback) where T : UIWindow
+ {
+ string windowName = typeof(T).FullName;
+ var window = GetWindow(windowName);
+ if (window == null)
+ {
+ return;
+ }
+
+ var ret = window as T;
+
+ if (ret == null)
+ {
+ return;
+ }
+
+ if (ret.IsLoadDone)
+ {
+ return;
+ }
+
+ GetUIAsyncImp(callback).Forget();
+
+ async UniTaskVoid GetUIAsyncImp(Action ctx)
+ {
+ float time = 0f;
+ while (!ret.IsLoadDone)
+ {
+ time += Time.time;
+ if (time > 60f)
+ {
+ break;
+ }
+ await UniTask.Yield();
+ }
+ ctx?.Invoke(ret);
+ }
+ }
private UIWindow GetWindow(string windowName)
{