From 02bf56b08904d159e87399275bf3e2f1276b1ea7 Mon Sep 17 00:00:00 2001
From: yyl <yyl>
Date: 星期三, 04 六月 2025 16:26:39 +0800
Subject: [PATCH] 18 子 2D卡牌客户端搭建 / 2D卡牌客户端搭建 OpenWindow 没提供 界面名(字符串)接口 【个人测试已过】
---
Main/UI/UIManager.cs | 193 +++++++++++++++++++++--------------------------
1 files changed, 86 insertions(+), 107 deletions(-)
diff --git a/Main/UI/UIManager.cs b/Main/UI/UIManager.cs
index 7babf03..aef1a1c 100644
--- a/Main/UI/UIManager.cs
+++ b/Main/UI/UIManager.cs
@@ -448,9 +448,8 @@
#endregion
#region UI璧勬簮绠$悊
-
- // 鍔犺浇UI棰勫埗浣�
- private T LoadUIResource<T>(string uiName) where T : UIBase
+
+ private UIBase LoadUIResource(string uiName)
{
// 浠庤祫婧愮鐞嗗櫒鍔犺浇UI棰勫埗浣�
GameObject prefab = ResManager.Instance.LoadAsset<GameObject>("UI", uiName);
@@ -467,34 +466,45 @@
GameObject uiObject = GameObject.Instantiate(prefab);
// 璁剧疆瀵硅薄鍚嶇О
uiObject.name = uiName;
+
+ // 閫氳繃uiName鏄犲皠Type
+ Type uiType = Type.GetType(uiName);
+ if (uiType == null)
+ {
+ Debug.LogError($"鎵句笉鍒癠I绫诲瀷: {uiName}");
+ return null;
+ }
+
// 鑾峰彇UI鍩虹被缁勪欢
- T uiBase = uiObject.GetComponent<T>();
+ UIBase uiBase = uiObject.GetComponent(uiType) as UIBase;
// 妫�鏌I鍩虹被缁勪欢鏄惁瀛樺湪
if (uiBase == null)
{
// 璁板綍閿欒鏃ュ織
- Debug.LogError($"UI棰勫埗浣� {uiName} 娌℃湁 UIBase 缁勪欢");
+ Debug.LogError($"UI棰勫埗浣� {uiName} 娌℃湁 UIBase 缁勪欢鎴栫被鍨嬩笉鍖归厤");
return null;
}
// 璁剧疆UI鍚嶇О
uiBase.uiName = uiName;
- // // 璁剧疆UI瀹炰緥ID
- // uiBase.instanceID = GenerateUIInstanceID(uiName);
// 璁剧疆鐖惰妭鐐逛负UI鏍硅妭鐐�
Transform parentTrans = GetTransForLayer(uiBase.uiLayer);
uiObject.transform.SetParent(parentTrans, false);
- // Debug.LogError("鍔犺浇UI璧勬簮-SetParent " + uiName + " transName is " + parentTrans.gameObject.name);
-
// 璁剧疆鎺掑簭椤哄簭
int baseSortingOrder = GetBaseSortingOrderForLayer(uiBase.uiLayer);
uiBase.SetSortingOrder(baseSortingOrder);
return uiBase;
+ }
+
+ // 鍔犺浇UI棰勫埗浣�
+ private T LoadUIResource<T>(string uiName) where T : UIBase
+ {
+ return LoadUIResource(uiName) as T;
}
#endregion
@@ -539,133 +549,92 @@
#endregion
#region UI鎿嶄綔
-
- /// <summary>
- /// 鎵撳紑UI
- /// </summary>
- public T OpenWindow<T>(UIBase parentUI = null) where T : UIBase
+
+
+
+ private UIBase GetLastSupportParentChildRelationUI()
{
- // 鑾峰彇UI绫诲瀷鍚嶇О
- string uiName = typeof(T).Name;
-
+ List<UIBase> tempList = new List<UIBase>();
+
+ UIBase target = null;
+
+ while (target == null && uiStack.Count > 0)
+ {
+ UIBase uiBase = uiStack.Pop();
+
+ if (uiBase != null && uiBase.supportParentChildRelation && !uiBase.isMainUI)
+ {
+ target = uiBase;
+ }
+
+ tempList.Add(uiBase);
+ }
+
+ for (int i = tempList.Count - 1; i >= 0; i--)
+ {
+ uiStack.Push(tempList[i]);
+ }
+
+ return target;
+ }
+
+ public UIBase OpenWindow(string uiName)
+ {
// 浼樺厛浠巆losedUIDict涓幏鍙�
+ UIBase parentUI = null;
+
+ UIBase returnValue = null;
+
if (closedUIDict.TryGetValue(uiName, out List<UIBase> closedUIList) && closedUIList.Count > 0)
{
- T recycledUI = closedUIList[0] as T;
+ returnValue = closedUIList[0] as UIBase;
closedUIList.RemoveAt(0);
if (closedUIList.Count == 0)
{
closedUIDict.Remove(uiName);
}
-
- recycledUI.gameObject.SetActive(true);
-
- // 鑷姩璁剧疆鐖剁骇UI锛堝鏋滄湭鎸囧畾涓旀敮鎸佺埗瀛愬叧绯伙級
- if (parentUI == null && recycledUI.supportParentChildRelation && uiStack.Count > 0)
- {
- // 鑾峰彇鏍堥《UI
- UIBase topUI = uiStack.Peek();
- // 濡傛灉鏍堥《UI涔熸敮鎸佺埗瀛愬叧绯讳笖涓嶆槸涓籙I锛屽垯灏嗗叾璁句负鐖剁骇
- if (topUI != null && topUI.supportParentChildRelation && !topUI.isMainUI)
- {
- parentUI = topUI;
- }
- }
-
- // 璁剧疆鐖剁骇UI
- if (parentUI != null && recycledUI.supportParentChildRelation && !parentUI.isMainUI)
- {
- // 璁剧疆鐖跺瓙鍏崇郴
- recycledUI.parentUI = parentUI;
- if (parentUI.childrenUI == null)
- {
- // 鍒濆鍖栫埗绾I鐨勫瓙UI鍒楄〃
- parentUI.childrenUI = new List<UIBase>();
- }
- // 娣诲姞鍒扮埗绾I鐨勫瓙UI鍒楄〃
- parentUI.childrenUI.Add(recycledUI);
- }
-
- // 鏇存柊鍥炲悎鏁�
- currentRound++;
- // 璁剧疆UI鐨勬渶鍚庝娇鐢ㄥ洖鍚堟暟
- recycledUI.lastUsedRound = currentRound;
- // 鏇存柊鐖剁骇UI鐨勫洖鍚堟暟
- UpdateParentUIRounds(recycledUI);
-
- // 灏哢I娣诲姞鍒板瓧鍏镐腑
- if (!uiDict.ContainsKey(uiName))
- {
- // 濡傛灉瀛楀吀涓笉瀛樺湪璇ョ被鍨嬬殑UI锛屽垱寤烘柊鍒楄〃
- uiDict[uiName] = new List<UIBase>();
- }
- // 娣诲姞鍒癠I鍒楄〃
- uiDict[uiName].Add(recycledUI);
-
- // 灏哢I娣诲姞鍒版爤涓�
- uiStack.Push(recycledUI);
-
- // 鏇存柊UI鎺掑簭椤哄簭
- UpdateUISortingOrder();
-
- // 鎵撳紑UI
- recycledUI.HandleOpen();
-
- OnOpenWindow?.Invoke(recycledUI);
-
- // 妫�鏌ュ苟鍏抽棴闀挎椂闂存湭浣跨敤鐨刄I
- CheckAndCloseIdleUI();
-
- return recycledUI;
}
-
- // 濡傛灉closedUIDict涓病鏈夊彲鐢ㄧ殑UI瀹炰緥锛屽垯鍔犺浇鏂扮殑UI璧勬簮
- // Debug.LogError("鎵撳紑ui " + uiName);
-
- // 鍔犺浇UI璧勬簮
- T ui = LoadUIResource<T>(uiName);
- if (ui == null)
+ else
{
- // 璁板綍閿欒鏃ュ織
- Debug.LogError($"鎵撳紑UI澶辫触: {uiName}");
- return null;
+ returnValue = LoadUIResource(uiName);
+ if (returnValue == null)
+ {
+ // 璁板綍閿欒鏃ュ織
+ Debug.LogError($"鎵撳紑UI澶辫触: {uiName}");
+ return null;
+ }
}
-
- // Debug.LogError("鍔犺浇UI璧勬簮 " + uiName);
+
+ returnValue.gameObject.SetActive(true);
// 鑷姩璁剧疆鐖剁骇UI锛堝鏋滄湭鎸囧畾涓旀敮鎸佺埗瀛愬叧绯伙級
- if (parentUI == null && ui.supportParentChildRelation && uiStack.Count > 0)
+ if (returnValue.supportParentChildRelation && uiStack.Count > 0)
{
// 鑾峰彇鏍堥《UI
- UIBase topUI = uiStack.Peek();
- // 濡傛灉鏍堥《UI涔熸敮鎸佺埗瀛愬叧绯讳笖涓嶆槸涓籙I锛屽垯灏嗗叾璁句负鐖剁骇
- if (topUI != null && topUI.supportParentChildRelation && !topUI.isMainUI)
- {
- parentUI = topUI;
- }
+ parentUI = GetLastSupportParentChildRelationUI();
}
// 璁剧疆鐖剁骇UI
- if (parentUI != null && ui.supportParentChildRelation && !parentUI.isMainUI)
+ if (parentUI != null)
{
// 璁剧疆鐖跺瓙鍏崇郴
- ui.parentUI = parentUI;
+ returnValue.parentUI = parentUI;
if (parentUI.childrenUI == null)
{
// 鍒濆鍖栫埗绾I鐨勫瓙UI鍒楄〃
parentUI.childrenUI = new List<UIBase>();
}
// 娣诲姞鍒扮埗绾I鐨勫瓙UI鍒楄〃
- parentUI.childrenUI.Add(ui);
+ parentUI.childrenUI.Add(returnValue);
}
// 鏇存柊鍥炲悎鏁�
currentRound++;
// 璁剧疆UI鐨勬渶鍚庝娇鐢ㄥ洖鍚堟暟
- ui.lastUsedRound = currentRound;
+ returnValue.lastUsedRound = currentRound;
// 鏇存柊鐖剁骇UI鐨勫洖鍚堟暟
- UpdateParentUIRounds(ui);
+ UpdateParentUIRounds(returnValue);
// 灏哢I娣诲姞鍒板瓧鍏镐腑
if (!uiDict.ContainsKey(uiName))
@@ -674,23 +643,33 @@
uiDict[uiName] = new List<UIBase>();
}
// 娣诲姞鍒癠I鍒楄〃
- uiDict[uiName].Add(ui);
+ uiDict[uiName].Add(returnValue);
// 灏哢I娣诲姞鍒版爤涓�
- uiStack.Push(ui);
+ uiStack.Push(returnValue);
// 鏇存柊UI鎺掑簭椤哄簭
UpdateUISortingOrder();
// 鎵撳紑UI
- ui.HandleOpen();
+ returnValue.HandleOpen();
- OnOpenWindow?.Invoke(ui);
+ OnOpenWindow?.Invoke(returnValue);
// 妫�鏌ュ苟鍏抽棴闀挎椂闂存湭浣跨敤鐨刄I
CheckAndCloseIdleUI();
- return ui;
+ return returnValue;
+ }
+
+ /// <summary>
+ /// 鎵撳紑UI
+ /// </summary>
+ public T OpenWindow<T>() where T : UIBase
+ {
+ // 鑾峰彇UI绫诲瀷鍚嶇О
+ string uiName = typeof(T).Name;
+ return OpenWindow(uiName) as T;
}
/// <summary>
--
Gitblit v1.8.0