/// <summary> /// 动态创建粒子将粒子放在ui之上或者下面 /// </summary> /// <param name="index">正数在上,负数在下</param> /// <param name="parent">在其上添加粒子 (如果该节点上设计上不需要添加canvas,该对象也可以传rootObj进来这里只是为了获取一个sortOder值设置特效)</param> /// <param name="rootObj">uibase </param> /// <param name="particleObj">粒子对象</param> public static void setParticleOrder(int index, GameObject parent, UIBase rootObj, GameObject particleObj) { Canvas canvas = parent.GetComponent <Canvas>(); if (canvas == null) { canvas = parent.AddComponent <Canvas>(); canvas.overrideSorting = true; parent.AddComponent <GraphicRaycaster>(); parent.GetComponent <GraphicRaycaster>().ignoreReversedGraphics = true; } //动态修改layer Transform[] particleTran = particleObj.GetComponentsInChildren <Transform>(); for (int i = 0; i < particleTran.Length; i++) { particleTran[i].gameObject.layer = LayerMask.NameToLayer("UI"); } Renderer[] particleRenders = particleObj.GetComponentsInChildren <Renderer>(); //找到最大层级和最小层级 int particleMaxOder = 0; int particleMinOder = particleRenders[0].sortingOrder; for (int i = 0; i < particleRenders.Length; i++) { System.Array.Sort(particleRenders, (a, b) => { int powerA = a.sortingOrder; int powerB = b.sortingOrder; if (powerA == powerB) { return(0); } if (powerA < powerB) { return(-1); } else { return(1); } }); if (particleRenders[i].sortingOrder <= particleMinOder) { particleMinOder = particleRenders[i].sortingOrder; } if (particleRenders[i].sortingOrder >= particleMaxOder) { particleMaxOder = particleRenders[i].sortingOrder; } } //加减10 是为了校正美术做特效时 特效中本来就有层的变化 if (canvas.name == rootObj.name) { canvas.sortingOrder = rootObj.GetCurrentCanvasOder(); } else { //加1 针对的是特效夹在ui之间的情况这样使得特效最底层也在ui地板最上面 在指定对象下面(10特效相对层级) // if(canvas.sortingOrder == 0) { canvas.sortingOrder = rootObj.GetCurrentCanvasOder() + (particleMaxOder - particleMinOder) + 10; } } if (index > 0) { for (int i = 0; i < particleRenders.Length; i++) { //加一是为了防止特效最小值为0的情况 particleRenders[i].sortingOrder = canvas.sortingOrder + (particleRenders[i].sortingOrder - particleMinOder + 1); } } else if (index < 0) { //特效最大值减去当前层级 for (int i = 0; i < particleRenders.Length; i++) { particleRenders[i].sortingOrder = canvas.sortingOrder - (particleMaxOder - particleRenders[i].sortingOrder + 1); } } }
public void openUIInRoot(string className, eUIType rUIType = eUIType.None, bool isSwitchDes = true, string perfabName = "") { if (string.IsNullOrEmpty(perfabName)) { perfabName = className; } UIBase ui = null; _mdicUIUnderRoot.TryGetValue(perfabName, out ui); if (ui == null) { addUIToRoot(perfabName, className); _mdicUIUnderRoot.TryGetValue(perfabName, out ui);; } else { switch (ui.mUIType) { case eUIType.SuperpositionUI: ui.recoverOder(); // _SuperpositionUIList.Remove(ui); break; case eUIType.StackUI: // _StackUIList.Remove(ui.uiTypeName); ui.recoverOder(); break; case eUIType.TopUI: case eUIType.BaseUI: ui.recoverOder(); break; default: ui.recoverOder(); break; } ui.mUIType = rUIType; } if (ui != null) { if (_StackUIList.Contains(ui.uiTypeName)) { _StackUIList.Remove(ui.uiTypeName); } if (_SuperpositionUIList.Contains(ui)) { _SuperpositionUIList.Remove(ui); } if (rUIType != eUIType.None) { ui.mUIType = rUIType; } ui.isSwichSceneDestroy = isSwitchDes; //设置UI层级 ui.freshCanvasOder(); switch (ui.mUIType) { case eUIType.SuperpositionUI: //叠加UI if (!_SuperpositionUIList.Contains(ui)) { _SuperpositionUIList.Add(ui); } break; case eUIType.StackUI: //堆栈UI _StackUIList.Remove(ui.uiTypeName); if (_StackUIList.Count > 0) { UIBase lastUI; _mdicUIUnderRoot.TryGetValue(_StackUIList[_StackUIList.Count - 1], out lastUI); if (lastUI != null) { DesUI(lastUI); } } _StackUIList.Add(ui.uiTypeName); break; } if (!_OpenUiList.Contains(ui)) { // UTLog.Log("添加UI"+ ui.GetType()); _OpenUiList.Add(ui); } SetUIActive(ui, true); if (ui.refreshFuncCallBack) { MessageDispatcher.SendMessage(null, ui.gameObject, "refresh", ui.refreshFuncData); ui.refreshFuncData = null; ui.refreshFuncCallBack = false; } } else { Debug.LogError("openUI failed! ==" + perfabName + "== not found!"); } }
private void _registUI(string uiname, UIBase uiobj) { removeUIFromUIRoot(uiname); _mdicUIUnderRoot.Add(uiname, uiobj); }
public void SetUIActive(UIBase rUI, bool rActive) { rUI.isActive = rActive; rUI.gameObject.SetActive(rActive); }