//获取对应的canvas:UICanvas or PopCanvas:如果有则直接取值,如果没有则会执行创建过程 private Canvas GetCanvasByLayer(PopCanvasLayer canvasLayer) { GameObject tStandardCanvs = GameObject.Find("Canvas");//用来赋值 //依据不同的层来确定Canvas的名称 string strCanvasName = canvasLayer == PopCanvasLayer.E_UILayer ? "UICanvas" : "PopCanvas"; //依据名称去查找Canvas是否已经被创建了,如果没有被创建,则创建之 GameObject tPopCanvs = GameObject.Find(strCanvasName); if (tPopCanvs == null) { //创建Canvas并设置摄像机信息,Layer信息,位置信息,RenderMode信息,planeDistance信息,scaler信息 tPopCanvs = new GameObject(); Canvas tCanvas = tPopCanvs.AddComponent <Canvas>(); tPopCanvs.name = strCanvasName; tCanvas.sortingLayerName = canvasLayer == PopCanvasLayer.E_UILayer ? "UIPOP" : "pop"; tCanvas.worldCamera = GameObject.Find("UICamera").GetComponent <Camera>(); tCanvas.renderMode = RenderMode.ScreenSpaceCamera; // added by jackmo at 2016-12-22,美术要求 tCanvas.planeDistance = 400; // added end if (tStandardCanvs != null) { CanvasScaler tCanvasScaler = tPopCanvs.AddComponent <CanvasScaler>(); tCanvasScaler.uiScaleMode = CanvasScaler.ScaleMode.ScaleWithScreenSize; tCanvasScaler.referenceResolution = tStandardCanvs.GetComponent <CanvasScaler>().referenceResolution; tCanvasScaler.matchWidthOrHeight = tStandardCanvs.GetComponent <CanvasScaler>().matchWidthOrHeight; } tPopCanvs.AddComponent <GraphicRaycaster>(); } return(tPopCanvs.GetComponent <Canvas>()); }
//增加一个新的弹窗: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 = PrefabManager.Instance.GetPrefabs(prefabName); if (prefab == null) { Lucky.LuckyUtils.Log("No preload, use resouce.load:" + prefabName); prefab = (GameObject)Resources.Load(prefabName); if (prefab == null) { Lucky.LuckyUtils.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) { Lucky.LuckyUtils.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); }