/// <summary> /// 当创建完界面时,调用一次 /// </summary> /// <param name="args">打开界面时,传递的参数</param> public virtual void OnCreated(object args) { if (View == null) { View = GetComponent <BaseUIView>(); } RegisterNotification(); }
/// <summary> /// 界面取消注册 /// </summary> /// <param name="ui"></param> public void UnRegisterUI(BaseUIView ui) { if (!m_AllRegisterUI.Contains((UIInstanceIDEnum)ui.UIInstanceID)) { LogManager.LogError("移除不存在的界面:" + ui.UIInstanceID); } else { m_AllRegisterUI.Remove((UIInstanceIDEnum)ui.UIInstanceID); m_AllInstantiateUI.Remove((UIInstanceIDEnum)ui.UIInstanceID); } }
/// <summary> /// 视窗改变 /// </summary> private static void OnHierarchyChanged() { if (!EditorUtil.CheckIsPrefabMode(out var prefabStage)) { return; } dicSelectObj.Clear(); baseUIComponent = null; baseUIView = null; GameObject root = prefabStage.prefabContentsRoot; baseUIComponent = root.GetComponent <BaseUIComponent>(); baseUIView = root.GetComponent <BaseUIView>(); if (baseUIComponent == null && baseUIView == null) { return; } //设置初始化数据 Dictionary <string, Type> dicData = null; if (baseUIComponent != null) { dicData = ReflexUtil.GetAllNameAndType(baseUIComponent); } if (baseUIView != null) { dicData = ReflexUtil.GetAllNameAndType(baseUIView); } foreach (var itemData in dicData) { string itemKey = itemData.Key; Type itemValue = itemData.Value; if (itemKey.Contains("ui_")) { string componentName = itemKey.Replace("ui_", ""); if (itemValue != null) { Component[] listRootComponent = root.GetComponentsInChildren(itemValue); foreach (Component itemRootComponent in listRootComponent) { if (itemRootComponent.name.Equals(componentName)) { dicSelectObj.Add(componentName, itemRootComponent); } } } } } return; }
/// <summary> /// 隐藏UI界面 /// </summary> /// <param name="ui"></param> public void Hide(UIInstanceIDEnum uiInstanceID) { if (m_AllRegisterUI.Contains(uiInstanceID)) //如果界面有注册 { if (m_AllInstantiateUI.ContainsKey(uiInstanceID)) //如果界面生成过 { BaseUIView view = m_AllInstantiateUI[uiInstanceID].View; view.OnHide(); BaseUIController controller = view.GetComponent <BaseUIController>(); controller.OnHide(); view.gameObject.SetActive(false); } } else { LogManager.LogError("显示没有注册的界面:" + uiInstanceID); } }
/// <summary> /// 显示UI /// </summary> /// <param name="uiInstanceID">UI唯一实例ID</param> /// <param name="uiGameObject">UI界面游戏物体</param> /// <param name="args">传递参数</param> public void ShowUI(UIInstanceIDEnum uiInstanceID, object args = null) { if (m_AllRegisterUI.Contains(uiInstanceID)) //如果界面有注册 { if (!m_AllInstantiateUI.ContainsKey((UIInstanceIDEnum)uiInstanceID)) //如果界面没有生成过 { //根据界面枚举找到对应加载预制体的路径 //todo:这边应该是配置单独的模块进行配置加载和读取,而不是和其他关联 //if (FarmGameManager.Instance.m_UIPathConfig == null) //{ // LogManager.LogError("没有读取到界面配置"); // return; //} //foreach (var item in FarmGameManager.Instance.m_UIPathConfig.allUIPath) //{ // if (item.id == (int)uiInstanceID) // { // LogManager.Log("path=" + item.path); // GameObject uiGameObject = ResourceLoadManager.Instance.Load(item.path) as GameObject; // BaseUIView view = Instantiate(uiGameObject).GetComponent<BaseUIView>(); // view.UIInstanceID = (int)uiInstanceID; // view.OnCreated(args); // view.OnShow(args); // BaseUIController controller = view.GetComponent<BaseUIController>(); // controller.OnCreated(args); // controller.OnShow(args); //todo:在这边也进行model层的初始化 // m_AllInstantiateUI.Add((UIInstanceIDEnum)view.UIInstanceID, controller); // } //} } else//已经生成过界面 { BaseUIView view = m_AllInstantiateUI[uiInstanceID].View; view.OnShow(args); BaseUIController controller = view.GetComponent <BaseUIController>(); controller.OnShow(args); } } else { LogManager.LogError("显示没有注册的界面:" + uiInstanceID); } }
/// <summary> /// 视窗元素 /// </summary> /// <param name="instanceid"></param> /// <param name="selectionrect"></param> private static void OnHierarchyShowSelect(int instanceid, Rect selectionrect) { //如果不是编辑模式则不进行操作 if (!EditorUtil.CheckIsPrefabMode(out var prefabStage)) { return; } //如果不是UI也不进行操作 if (baseUIComponent == null && baseUIView == null) { return; } //获取当前obj var go = EditorUtility.InstanceIDToObject(instanceid) as GameObject; if (go == null) { return; } if (baseUIComponent == null) { baseUIComponent = go.GetComponent <BaseUIComponent>(); } if (baseUIView == null) { baseUIView = go.GetComponent <BaseUIView>(); } //控制开关 var selectBox = new Rect(selectionrect); selectBox.x = selectBox.xMax - 30; selectBox.width = 10; //检测是否选中 bool hasGo = false; Component selectComonent = null; if (dicSelectObj.TryGetValue(go.name, out selectComonent)) { hasGo = true; } hasGo = GUI.Toggle(selectBox, hasGo, string.Empty); if (hasGo) { if (!dicSelectObj.ContainsKey(go.name)) { dicSelectObj.Add(go.name, null); } } else { if (dicSelectObj.ContainsKey(go.name)) { dicSelectObj.Remove(go.name); } } //如果选中了 if (hasGo) { //下拉选择 var selectType = new Rect(selectionrect); selectType.x = selectBox.xMax - 160; selectType.width = 150; //获取该obj下所拥有的所有comnponent Component[] componentList = go.GetComponents <Component>(); string[] listData = new string[componentList.Length]; int selectComonentIndex = 0; //初始化所有可选component; for (int i = 0; i < componentList.Length; i++) { listData[i] = componentList[i].GetType().Name; if (selectComonent != null && selectComonent.GetType().Name.Equals(listData[i])) { selectComonentIndex = i; } } //默认选择 if (selectComonent == null) { //如果有设置控件 if (listData.Length > 2) { selectComonentIndex = componentList.Length - 1; } dicSelectObj[go.name] = componentList[selectComonentIndex]; } //设置下拉数据 使用此方法需要连续点2次 //int newSelectComonentIndex = EditorGUI.Popup(selectType,selectComonentIndex, listData); //int newSelectComonentIndex = GUI.Toolbar(selectType, selectComonentIndex, listData); //如果下拉数据改变 //dicSelectObj[go.name] = componentList[selectComonentIndex]; //自定义弹窗 if (GUI.Button(selectType, listData[selectComonentIndex])) { Rect popupRect = GUILayoutUtility.GetLastRect(); popupRect.x = selectType.x; popupRect.y = selectType.y + selectType.height; PopupWindow.Show(popupRect, new HierarchySelectPopupSelect((popupSelectIndex) => { dicSelectObj[go.name] = componentList[popupSelectIndex]; }, listData)); } } }