/// <summary> /// 自动给Forms层级排序 /// </summary> private int AutoCalculateDepth(UIBaseFormsRoot forms, int curDepth) { UIPanel parentPanel = forms.GetComponent <UIPanel>(); if (parentPanel == null) { Debug.LogWarning("=============自动给Forms层级排序========parentPanel是null======"); return(curDepth); } UIPanel[] childrenPanel = forms.GetComponentsInChildren <UIPanel>(true); //这里的下标0也是parentPanel int parentPanelDepth = parentPanel.depth; int maxDis = 0; for (int i = 0; i < childrenPanel.Length; i++) { int disDepth = childrenPanel[i].depth - parentPanelDepth; childrenPanel[i].depth = curDepth + disDepth; if (disDepth > maxDis) { maxDis = disDepth; } } return(curDepth + maxDis + 1); }
/// <summary> /// 关闭单个窗口 /// </summary> public void CloseForms(string formsName) { if (string.IsNullOrEmpty(formsName)) { return; } UIBaseFormsRoot forms = null; if (mFormsDic.TryGetValue(formsName, out forms)) { switch (forms.mFormsLayerType) { case UIFormsLayer.CommonUILayer: { if (forms.mFormsType == UIFormsType.Normal) { if (forms.mFormsLifeType == UIFormsLifeType.GoldLife) { forms.Hide(); } else if (forms.mFormsLifeType == UIFormsLifeType.HumanLife) { DestroyForms(formsName); } PopForms(formsName); } else if (forms.mFormsType == UIFormsType.Popup) { DestroyForms(formsName); } } break; default: if (forms.mFormsLifeType == UIFormsLifeType.GoldLife) { forms.Hide(); } else if (forms.mFormsLifeType == UIFormsLifeType.HumanLife) { DestroyForms(formsName); } break; } } }
/// <summary> /// 销毁窗口 /// </summary> /// <param name="formsName"></param> private void DestroyForms(string formsName) { if (string.IsNullOrEmpty(formsName)) { return; } UIBaseFormsRoot forms = null; if (mFormsDic.TryGetValue(formsName, out forms)) { forms.End(); mFormsDic.Remove(formsName); GameObject.DestroyImmediate(forms.gameObject); } }
/// <summary> /// 显示窗口 /// </summary> public T ShowForms <T>(string formsName, bool IsAutoDepth = true) where T : UIBaseFormsRoot { if (string.IsNullOrEmpty(formsName)) { return(null); } UIBaseFormsRoot forms = null; if (!mFormsDic.TryGetValue(formsName, out forms)) { forms = _CreateForms <T>(formsName); if (forms != null) { if (!forms.gameObject.activeSelf) { forms.gameObject.SetActive(true); } forms.Init(); } } else { if (!forms.gameObject.activeSelf) { forms.gameObject.SetActive(true); } forms.Show(); } if (forms != null) { switch (forms.mFormsLayerType) { case UIFormsLayer.CommonUILayer: { if (forms.mFormsType == UIFormsType.Normal) { //1.隐藏栈顶forms if (mFormsStackIndex.ContainsKey(mCurStackFormsIndex)) { string name = mFormsStackIndex[mCurStackFormsIndex]; if (mFormsDic.ContainsKey(name)) { mFormsDic[name].Hide(); } } //2.如果存在栈中且不是位于栈顶(是否考虑栈顶的话就不做后面的操作),需要"Top"到栈顶 if (mFormsStackName.ContainsKey(formsName)) { if (mFormsStackName[formsName] != mCurStackFormsIndex) { int tIndex = mFormsStackName[formsName]; for (int i = tIndex; i < mCurStackFormsIndex; i++) { mFormsStackIndex[i] = mFormsStackIndex[i + 1]; mFormsStackName[mFormsStackIndex[i]] = i; } mFormsStackIndex[mCurStackFormsIndex] = formsName; mFormsStackName[formsName] = mCurStackFormsIndex; } } else { mCurStackFormsIndex++; PushForms(formsName); } //3.移除Stack类型的窗口 if (mPopupFormsStack.Count > 0) { foreach (var item in mPopupFormsStack) { this.DestroyForms(item); } mPopupFormsStack.Clear(); } //4.重置depth, //这里可以考虑做成:假栈为空的时候,界面只剩下主界面的时候重置depth //目前是做成:只显示一个normal型窗口,底下normal窗口隐藏,所以这里可以重置depth mCurUILayerDepth = (int)UIFormsLayer.CommonUILayer; } else if (forms.mFormsType == UIFormsType.Popup) { if (!mPopupFormsStack.Contains(formsName)) { mPopupFormsStack.Push(formsName); } else { Debug.LogWarning("*****************栈窗口已经有此窗口*******************"); } } if (IsAutoDepth) { mCurUILayerDepth = AutoCalculateDepth(forms, mCurUILayerDepth); } } break; default: if (IsAutoDepth) { AutoCalculateDepth(forms, (int)forms.mFormsLayerType); } break; } return(forms as T); } return(null); }
/// <summary> /// 关闭单个窗口 /// </summary> public void CloseForms(string formsName) { if (string.IsNullOrEmpty(formsName)) { return; } UIBaseFormsRoot forms = null; if (mFormsDic.TryGetValue(formsName, out forms)) { switch (forms.mFormsLayerType) { case UIFormsLayer.CommonUILayer: { if (forms.mFormsType == UIFormsType.Normal) { if (forms.mFormsLifeType == UIFormsLifeType.GoldLife) { forms.Hide(); } else if (forms.mFormsLifeType == UIFormsLifeType.HumanLife) { DestroyForms(formsName); } PopForms(formsName); // 显示下一层窗口 string nextFormsName; mFormsStackIndex.TryGetValue(mCurStackFormsIndex, out nextFormsName); UIBaseFormsRoot nextForms = GetForms(nextFormsName); if (nextForms != null) { if (!nextForms.gameObject.activeSelf) { nextForms.gameObject.SetActive(true); } } } else if (forms.mFormsType == UIFormsType.Popup) { DestroyForms(formsName); } } break; default: if (forms.mFormsLifeType == UIFormsLifeType.GoldLife) { forms.Hide(); } else if (forms.mFormsLifeType == UIFormsLifeType.HumanLife) { DestroyForms(formsName); } break; } } }