/// <summary> /// 删除自身UI /// </summary> public void DestoryUI() { BaseUI currentUI = GetBaseUI(CurrentId); Destroy(currentUI.gameObject); RemoveUiId(CurrentId); BaseUI parentUI = currentUI.transform.GetComponentUpwards <BaseUI>(); if (parentUI != null) { currentUI = parentUI; CurrentId = parentUI.UiId; } }
//供外界调用的,显示窗体的方法 public BaseUI ShowUI(E_UiId uiId, bool isSaveBeforeUiId = true) { if (uiId == E_UiId.NullUI) { uiId = E_UiId.MainUI; } BaseUI baseUI = JudgeShowUI(uiId); if (baseUI != null) { baseUI.ShowUI(); } if (isSaveBeforeUiId) { baseUI.BeforeUiId = beforeUiId; } return(baseUI); }
private BaseUI JudgeShowUI(E_UiId uiId) { //判断将要显示的窗体是否已经正在显示了 if (dicShowUI.ContainsKey(uiId)) { //如果已经正在显示了,就不需要处理其他逻辑了 return(null); } //判断窗体是否有加载过 BaseUI baseUI = GetBaseUI(uiId); if (baseUI == null) { //说明这个窗体没显示过(没有加载过),要去动态加载 string path = GameDefine.dicPath[uiId]; GameObject theUI = Resources.Load <GameObject>(path); if (theUI != null) { //把该窗体生成出来 GameObject willShowUI = Instantiate(theUI); //窗体生成出来后,要确保有挂对应的UI脚本 baseUI = willShowUI.GetComponent <BaseUI>(); if (baseUI == null) { //说明生成出来的这个窗体上面没有挂载对应的UI脚本 //那么就需要给这个窗体自动添加对应的脚本 Type type = GameDefine.GetUIScriptType(uiId); baseUI = willShowUI.AddComponent(type) as BaseUI; } //判断这个窗体是属于哪个父节点的 Transform uiRoot = GetTheUIRoot(baseUI); GameTool.AddChildToParent(uiRoot, willShowUI.transform); willShowUI.GetComponent <RectTransform>().sizeDelta = Vector2.zero; //这个窗体是第一次加载显示出来的,那么就需要缓存起来 dicAllUI.Add(uiId, baseUI); } else { Debug.LogError("指定路径下面找不到对应的预制体"); } } UpdateDicShowUIAndHideUI(baseUI); return(baseUI); }
public BaseUI JudgeShowUI(EUiId uiId, Transform parent) { if (dicShowUI.ContainsKey(uiId)) { return(null); } BaseUI baseUI = GetBaseUI(uiId); if (baseUI == null) { if (GameDefine.dicPath.ContainsKey(uiId)) { string path = GameDefine.dicPath[uiId]; GameObject theUI = Resources.Load <GameObject>(path); if (theUI != null) { GameObject willShowUI = Instantiate(theUI); baseUI = willShowUI.GetComponent <BaseUI>(); if (baseUI == null) { Type type = GetTypeByUiId(uiId); baseUI = willShowUI.AddComponent(type) as BaseUI; } //把生成出来的窗体放在UiRoot下面 willShowUI.SetParent(parent == null ? uiRoot : parent); willShowUI.GetComponent <RectTransform>().sizeDelta = Vector2.zero; willShowUI.GetComponent <RectTransform>().anchoredPosition3D = Vector3.zero; dicAllUI.Add(uiId, baseUI); } else { Debug.LogError("在路径" + path + "下面找不到预制体" + uiId + ",请检查路径下面是否有该预制体"); } } else { Debug.LogError("GameDefine下面没有窗体ID为" + uiId + "的加载路径"); } } UpdateDicShowUI(baseUI, !parent); return(baseUI); }
//更新缓存正在显示的窗体的字典并且隐藏对应的窗体 private void UpdateDicShowUIAndHideUI(BaseUI baseUI) { //判断是否需要隐藏其他窗体 if (baseUI.IsHideOtherUI()) { //如果返回值为true, E_ShowUIMode.HideOther与 E_ShowUIMode.HideAll //需要隐藏其他窗体 if (dicShowUI.Count > 0) { //有窗体正在显示,就要隐藏对应的窗体 if (baseUI.uiType.showMode == E_ShowUIMode.HideOther) { HideAllUI(false, baseUI); } else { HideAllUI(true, baseUI); } } } //更新缓存正在显示的窗体的字典 dicShowUI.Add(baseUI.GetUiId, baseUI); }
public void ShowUI(EUiId nextUiId, SceneTransType transType = SceneTransType.Null, Transform parent = null, string EventTypeName = null, params object[] param) { Resources.UnloadUnusedAssets(); GC.Collect(); if (isInit) { AudioManager.Instance.PlayAudio(); } if (!isInit) { isInit = true; } BaseUI currentUI = GetBaseUI(CurrentId); SceneTransition.ShowTranstion(transType, () => { if (transType == SceneTransType.Newspaper) { } }, () => { if (currentUI != null && parent == null) { //父级baseUI EUiId parentUiId = EUiId.NullUI; BaseUI parentUI = currentUI.transform.GetComponentUpwards <BaseUI>(); if (parentUI != null) { parentUiId = parentUI.UiId; } BaseUI[] childUI = currentUI.GetComponentsInFirstHierarchyChildren <BaseUI>(true); //如果父级有baseUI并且直接打开的是别的UI的话就删除 if (parentUiId != EUiId.NullUI && nextUiId != parentUiId) { Destroy(parentUI.gameObject); } else { Destroy(currentUI.gameObject); } RemoveUiId(CurrentId); if (childUI != null) { for (int i = 0; i < childUI.Length; i++) { EUiId chiidUiId = childUI[i].UiId; RemoveUiId(chiidUiId); } } if (parentUiId != EUiId.NullUI && nextUiId != parentUiId) { RemoveUiId(parentUiId); } } BaseUI baseUI = JudgeShowUI(nextUiId, parent); if (baseUI != null) { baseUI.HideUI(); if (EventTypeName != null) { SwanEngine.Events.Dispatcher.Instance.DispathEvent(EventTypeName, param); } //CurrentId = nextUiId; baseUI.ShowUI(); /* 到时候可能要改成这样,因为在OnEnable的时候Enable还为false,不能调用协程 * baseUI.ShowUI(); * if (EventTypeName != null) * SwanEngine.Events.Dispatcher.Instance.DispathEvent(EventTypeName, param); */ } CurrentId = nextUiId; }); Resources.UnloadUnusedAssets(); GC.Collect(); }