//往一个弹窗上面挂载子弹窗 //prefabName:预设名称,parent:所需要挂载的母弹窗的gameObj,showPriority:显示优先级,id:id private GameObject AddPopUpInParent(string prefabName, GameObject parent, int showPriority, string id = "") { //判定预设是否存在,否则返回null GameObject prefab = (GameObject)Resources.Load(prefabName); if (prefab == null) { Debug.Log("AddPopup, Cannot find prefab!"); return(null); } Canvas canvas = null; GameObject popUp = null; //创建PopupInfo对象,准备插入到母弹窗的childObjs里面 PopupInfo popInfo = new PopupInfo(); popInfo.showPriority = showPriority; popInfo.strId = id; //母弹窗的相关信息:需要从uiLayerPopUps和popLayerPopUps两个队列里面去找 PopupInfo parentPopUpInfo = null; //从uiLayerPopUps队列里面去找母弹窗信息 for (int idx = 0; idx < uiLayerPopUps.Count; ++idx) { if (uiLayerPopUps[idx].gameObj == parent) { parentPopUpInfo = uiLayerPopUps[idx]; } } //如果uiLayerPopUps里面没找到,则再去popLayerPopUps队列里面去找母弹窗信息 if (parentPopUpInfo == null) { for (int idx = 0; idx < popLayerPopUps.Count; ++idx) { if (popLayerPopUps[idx].gameObj == parent) { parentPopUpInfo = popLayerPopUps[idx]; } } } //uiLayerPopUps和popLayerPopUps队列里面都没有找到,则输出错误日志并返回 if (parentPopUpInfo == null) { Debug.Log("Show Pop in paretn, can not find parent in popups!"); return(null); } //获取母弹窗父节点的canvas信息,用来设定子弹窗的一些位置信息 canvas = parentPopUpInfo.gameObj.transform.parent.gameObject.GetComponent <Canvas>(); if (canvas == null) { //母弹窗不是挂在canvas上的?输出错误日志并返回 Debug.Log("AddPopupInParent, parent father is not Canvas!"); return(null); } //母弹窗存在,且母弹窗的父节点canvas也存在,则创建子窗口组件 popUp = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; //j将子窗口插入到母窗口的childObjs队列里面 popInfo.gameObj = popUp; parentPopUpInfo.childObjs.Add(popInfo); //设置子窗口组件的位置信息 popUp.transform.SetParent(canvas.transform, false); popUp.transform.localPosition = Vector3.zero; popUp.transform.localScale = Vector3.one; (popUp.transform as RectTransform).SetAsLastSibling(); //给所有的物件都挂上DestroyEventUI:该脚本的OnDestory函数里面会通知到PopUpMgr.OnDestroy //所以实现了PopUpMgr监听弹窗的销毁事件 DestroyEventUI destroyUi = popUp.GetComponent <DestroyEventUI>(); if (destroyUi == null) { destroyUi = popUp.AddComponent <DestroyEventUI>(); } destroyUi.onDestroy += OnDestroy; //刚插入进去,等下一帧update里面会执行checkshow逻辑,如果该弹窗优先级是最高的,则她会在下一帧弹出来 popUp.GetComponent <BaseScene>().SetVisable(false); needCheckToShowPopup = true; return(popUp); }
// for lua public void AddUiLayerPopup2(GameObject popUp) { //判定预设是否存在,否则返回null Canvas canvas = null; // added by jackmo at 2017-2-6,扫描bug,popUp判空 if (popUp == null) { Debug.Log("PopupManager, AddPopupNew, Cannot Instantiate the prefab!"); return; } // added end //GetCanvasByLayer:获取对应的canvas:UICanvas or PopCanvas:如果有则直接取值,如果没有则会执行创建过程 canvas = GetCanvasByLayer(PopCanvasLayer.E_UILayer); //设置popUp位置:居中显示在屏幕上 popUp.transform.SetParent(canvas.transform, false); popUp.transform.localPosition = Vector3.zero; popUp.transform.localScale = Vector3.one; (popUp.transform as RectTransform).SetAsLastSibling(); //给所有的物件都挂上DestroyEventUI:该脚本的OnDestory函数里面会通知到PopUpMgr.OnDestroy //所以实现了PopUpMgr监听弹窗的销毁事件 DestroyEventUI destroyUi = popUp.GetComponent <DestroyEventUI>(); if (destroyUi == null) { destroyUi = popUp.AddComponent <DestroyEventUI>(); } destroyUi.onDestroy += OnDestroy; //创建popInfo,准备插入到显示队列里面去 PopupInfo popInfo = new PopupInfo(); popInfo.gameObj = popUp; popInfo.showPriority = 0; popInfo.strId = ""; //插入过程:根据showPriority从队列尾部遍历插入 bool insertSuc = false; for (int idx = uiLayerPopUps.Count - 1; idx >= 0; --idx) { if (uiLayerPopUps[idx].showPriority <= 0) { uiLayerPopUps.Insert(idx + 1, popInfo); insertSuc = true; break; } } //没找到合适的位置插入,说明该弹窗的showPriority是最小的,所以往顶部插 if (!insertSuc) { uiLayerPopUps.Insert(0, popInfo); } //刚插入进去,等下一帧update里面会执行checkshow逻辑,如果该弹窗优先级是最高的,则她会在下一帧弹出来 popUp.GetComponent <BaseScene>().SetVisable(false); needCheckToShowPopup = true; }
//增加一个新的弹窗:prefabName:预设路径,canvasLayer:指定是在UI层还是Pop层 //insertInfo:如果是ui层,则是uiLayerPopUps,如果是Pop层,则是popLayerPopUps //showPriority:显示优先级 private GameObject AddPopupNew(string prefabName, PopCanvasLayer canvasLayer, ref List <PopupInfo> insertInfo, int showPriority, string id = "") { //jony add //GameObject prefab = MySceneManager.instance.getPreloadedGameObject(prefabName); GameObject prefab = null; if (prefab == null) { Debug.Log("No preload, use resouce.load:" + prefabName); prefab = (GameObject)Resources.Load(prefabName); if (prefab == null) { Debug.Log("AddPopup, Cannot find prefab!"); return(null); } } //add end Canvas canvas = null; GameObject popUp = null; //创建PopUp popUp = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; // added by jackmo at 2017-2-6,扫描bug,popUp判空 if (popUp == null) { Debug.Log("PopupManager, AddPopupNew, Cannot Instantiate the prefab!"); return(null); } // added end //GetCanvasByLayer:获取对应的canvas:UICanvas or PopCanvas:如果有则直接取值,如果没有则会执行创建过程 canvas = GetCanvasByLayer(canvasLayer); //设置popUp位置:居中显示在屏幕上 popUp.transform.SetParent(canvas.transform, false); popUp.transform.localPosition = Vector3.zero; popUp.transform.localScale = Vector3.one; (popUp.transform as RectTransform).SetAsLastSibling(); //给所有的物件都挂上DestroyEventUI:该脚本的OnDestory函数里面会通知到PopUpMgr.OnDestroy //所以实现了PopUpMgr监听弹窗的销毁事件 DestroyEventUI destroyUi = popUp.GetComponent <DestroyEventUI>(); if (destroyUi == null) { destroyUi = popUp.AddComponent <DestroyEventUI>(); } destroyUi.onDestroy += OnDestroy; //创建popInfo,准备插入到显示队列里面去 PopupInfo popInfo = new PopupInfo(); popInfo.gameObj = popUp; popInfo.showPriority = showPriority; if (id != null && !id.Equals("")) { popInfo.strId = id; } else { popInfo.strId = prefabName; } //插入过程:根据showPriority从队列尾部遍历插入 bool insertSuc = false; for (int idx = insertInfo.Count - 1; idx >= 0; --idx) { if (insertInfo[idx].showPriority <= showPriority) { insertInfo.Insert(idx + 1, popInfo); insertSuc = true; break; } } //没找到合适的位置插入,说明该弹窗的showPriority是最小的,所以往顶部插 if (!insertSuc) { insertInfo.Insert(0, popInfo); } //刚插入进去,等下一帧update里面会执行checkshow逻辑,如果该弹窗优先级是最高的,则她会在下一帧弹出来 popUp.GetComponent <BaseScene>().SetVisable(false); needCheckToShowPopup = true; return(popUp); }