yyl
2026-03-31 0fa617a09eedf6bdb25eda55fac1d3344859fd93
Main/Manager/UIManager.cs
@@ -775,6 +775,16 @@
    /// </summary>
    public async UniTask<T> OpenWindowAsync<T>(int functionOrder = 0) where T : UIBase
    {
        if (typeof(T).Name == "MainWin")
        {
            //MainWin 比较关键且唯一,做安全防范
            var ui = GetUI<MainWin>();
            if (ui != null)
            {
                ui.ClickFunc(0);
                return ui as T;
            }
        }
        string uiName = typeof(T).Name;
        var result = await OpenWindowAsync(uiName, functionOrder);
        return result as T;
@@ -976,127 +986,6 @@
        return null;
    }
    public async UniTask<UIBase> OpenWindow(string uiName, int functionOrder = 0)
    {
        // 优先从closedUIDict中获取
        UIBase parentUI = null;
        UIBase returnValue = null;
        List<UIBase> closedUIList = new List<UIBase>();
        if (closedUIDict.TryGetValue(uiName, out closedUIList) && closedUIList.Count > 0)
        {
            #if UNITY_EDITOR
            Debug.Log("OpenWindow getFromClosedDict " + uiName);
            #endif
            returnValue = closedUIList[0] as UIBase;
            closedUIList.RemoveAt(0);
            if (closedUIList.Count == 0)
            {
                closedUIDict.Remove(uiName);
            }
        }
        else
        {
            #if UNITY_EDITOR
            Debug.Log("OpenWindow getNewLoad " + uiName);
            #endif
            returnValue = await LoadUIResourceAsync(uiName);
            if (returnValue == null)
            {
                // 记录错误日志
                Debug.LogError($"打开UI失败: {uiName}");
                return null;
            }
        }
        returnValue.gameObject.SetActive(true);
        // 自动设置父级UI, 如果勾选了ismainui 则不需要找父级UI
        if (returnValue.supportParentChildRelation && uiStack.Count > 0 && !returnValue.isMainUI)
        {
            // 获取栈顶UI
            parentUI = GetLastSupportParentChildRelationUI();
        }
        // 设置父级UI
        if (parentUI != null)
        {
            // 设置父子关系
            returnValue.parentUI = parentUI;
            if (parentUI.childrenUI == null)
            {
                // 初始化父级UI的子UI列表
                parentUI.childrenUI = new List<UIBase>();
            }
            // 添加到父级UI的子UI列表
            parentUI.childrenUI.Add(returnValue);
        }
        // 更新回合数
        currentRound++;
        // 设置UI的最后使用回合数
        returnValue.lastUsedRound = currentRound;
        // 更新父级UI的回合数
        UpdateParentUIRounds(returnValue);
        // 将UI添加到字典中
        if (!uiDict.ContainsKey(uiName))
        {
            // 如果字典中不存在该类型的UI,创建新列表
            uiDict[uiName] = new List<UIBase>();
        }
        // 添加到UI列表
        uiDict[uiName].Add(returnValue);
#if UNITY_EDITOR
        if (uiDict[uiName].Count > 5)
        {
            Debug.LogError("已打开" + uiDict[uiName].Count + "个界面:" + uiName);
        }
#endif
        // 将UI添加到栈中
        uiStack.Push(returnValue);
        // 更新UI排序顺序
        UpdateUISortingOrder();
        // 打开UI
        returnValue.functionOrder = functionOrder;
        returnValue.HandleOpen();
        OnOpenWindow?.Invoke(returnValue);
        // 检查并关闭长时间未使用的UI
        CheckAndCloseIdleUI();
        return returnValue;
    }
    /// <summary>
    /// 打开UI
    /// </summary>
    public T OpenWindow<T>(int functionOrder = 0) where T : UIBase
    {
        if (typeof(T).Name == "MainWin")
        {
            //MainWin 比较关键且唯一,做安全防范
            var ui = GetUI<MainWin>();
            if (ui != null)
            {
                ui.ClickFunc(0);
                return ui as T;
            }
        }
        // 获取UI类型名称
        string uiName = typeof(T).Name;
        return OpenWindow(uiName, functionOrder) as T;
    }
    /// <summary>
    /// 关闭UI
    /// </summary>