- 最后登录
- 2021-7-6
- 注册时间
- 2012-12-27
- 阅读权限
- 90
- 积分
- 76145
- 纳金币
- 53488
- 精华
- 316
|
NGUI 打开窗口时根据打开的顺序分配Depth,后打开的窗口depth逐渐升高调用方法- using UnityEngine;
- using System.Collections.Generic;
- public class BagUI : MonoBehaviour {
- bool isShow =false;
- public void TransformShow()
- {
- if(isShow==false)//打开窗口
- {
- transform.parent.localPosition =Vector3.zero;
- isShow =true;
- UIWindowsDepthManger.GetInstance.AddOpenWindow(transform);
- }
- else//关闭窗口
- {
- transform.parent.localPosition =new Vector3(1999,0,0);
- UIWindowsDepthManger.GetInstance.CloseWindow(transform);
- isShow =false;
- }
- }
- }
复制代码 实现代码- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// 打开窗口时根据打开的顺序分配Depth,后打开的窗口depth逐渐升高,.
- /// 窗口打开后,搜索全部Panel,记录Depth,然后把depth提高,关闭时恢复记录的depth
- /// 恢复时,在子物体里根据Panel搜索,恢复depth
- /// </summary>
- public class UIWindowsDepthManger : MonoBehaviour {
- public class DepthPanel
- {
- public int depth;//深度
- public UIPanel panel;
- public DepthPanel(int depth,UIPanel panel)
- {
- this.depth=depth;
- this.panel=panel;
- }
- }
- public class DepthP
- {
- public Transform transform;//父Panel
- public List<DepthPanel> depthPanel_list;//父Panel下的所有panel深度数据列表
- public DepthP(Transform transform)
- {
- this.transform = transform;
- depthPanel_list =new List<DepthPanel>();
- }
- public void Add(DepthPanel depthPanel)
- {
- depthPanel_list.Add (depthPanel);
- }
- }
- List<DepthP> dp_List = new List<DepthP> ();
- static UIWindowsDepthManger _instance;
- public static UIWindowsDepthManger GetInstance
- {
- get
- {
- if(_instance==null)
- {
- GameObject go = new GameObject("UIWindowsDepthManger");
- _instance = go.AddComponent<UIWindowsDepthManger>();
- }
- return _instance;
- }
- }
- void Awake()
- {
- if(_instance!=null)
- {
- Debug.LogError("many UIWindowsDepthManger");
- }
- _instance = this;
- }
- static int currentMaxDepth = 1000;//当前最高depth
- /// <summary>
- /// 把打开的窗口,UIpanel.depth提升.在窗口打开时调用
- /// </summary>
- /// <param name="transform">Transform.</param>
- public void AddOpenWindow (Transform transform)
- {
- //检测transform是否是上一个窗口,是就返回
- if(dp_List.Count>0)
- {
- if(dp_List[dp_List.Count-1].transform ==transform)
- {
- return;
- }
- currentMaxDepth += 30;
- }
- //根据t搜索到最顶层的panel,搜索全部Panel,记录Depth,然后把depth提高,关闭时恢复记录的depth
- //找到最顶层的panel
- transformResult = null;
- FindUpPanel (transform);
- Transform panrent = transformResult;
- DepthP depthP = new DepthP (panrent);
- //搜索全部Panel
- childArrayList.Clear ();
- FindAllChilds (panrent);
- foreach(Transform t in childArrayList)
- {
- UIPanel panel = t.GetComponent<UIPanel>();
- if(panel!=null)
- {
- //记录depth
- DepthPanel depthPanel =new DepthPanel(panel.depth,panel);
- depthP.Add(depthPanel);
- //修改depth
- panel.depth += currentMaxDepth;
- }
- }
- dp_List.Add(depthP);
- }
- /// <summary>
- ///把关闭窗口,UIpanel.depth恢复.在窗口关闭前调用
- /// </summary>
- /// <param name="t">T.</param>
- public void CloseWindow(Transform t)
- {
- /*
- * 找到t的父Panel , 从 dp_List 里找到t
- * 从 dp_List 恢复depth,removeITem
- * 如果窗口全部关闭时,就重置currentMaxDepth
- */
- //找到最顶层的panel
- transformResult = null;
- FindUpPanel (t);
- Transform panrent = transformResult;
- int index = -1;
- for(int i=0;i<dp_List.Count;i++)
- {
- if(dp_List[i].transform ==panrent)
- {
- index =i;
- }
- }
- //从 dp_List 恢复depth,removeITem
- if(index!=-1)
- {
- foreach(DepthPanel depthPanel in dp_List[index].depthPanel_list)
- {
- depthPanel.panel.depth = depthPanel.depth;
- }
- dp_List.RemoveAt(index);
- }
- if(dp_List.Count==0)
- {
- currentMaxDepth =1000;
- }
- }
- /// 找到最顶层的panel.存入 transformResult
- Transform transformResult;
- void FindUpPanel(Transform transf)
- {
- if(transf.parent== UIRoot2DReference.Instance.transform)
- {
- transformResult =transf;
- return;
- }
- FindUpPanel (transf.parent);
- }
- //找寻所有子物体,存入childArrayList
- List<Transform> childArrayList = new List<Transform> ();
- void FindAllChilds (Transform treeSource)
- {
- if (treeSource.childCount>0)
- {
- int i;
- for (i=0;i<treeSource.childCount;i++)
- {
- FindAllChilds (treeSource.GetChild(i));
- }
- }
- childArrayList.Add(treeSource);
- }
- }
复制代码 |
|