NGUI中处理层级问题的几个方法总结

1、获得ui界面的UIPanel的最大层级:

 1  static int GetUIMaxDepth(Transform root)
 2         {
 3             UIPanel[] panels = root.GetComponentsInChildren<UIPanel>(false);
 4             if (panels == null || panels.Length < 1)
 5                 return 0;
 6 
 7             Array.Sort(panels, (a, b) => a.depth - b.depth);
 8             UIPanel lastPanel = panels.LastOrDefault();
 9             return lastPanel != null ? lastPanel.depth : 0;
10         }

2、设置ui界面中UIPanel的层级:

 1 static void SetUIDepth(Transform root, int depth)
 2         {
 3             UIPanel[] panels = root.GetComponentsInChildren<UIPanel>(true);
 4             if (panels == null || panels.Length < 1)
 5                 return;
 6 
 7             Array.Sort(panels, (a, b) => a.depth - b.depth);
 8             for (int i = 0; i < panels.Length; i++)
 9             {
10                 UIPanel p = panels[i];
11                 if (p != null)
12                     p.depth = i + depth;
13             }
14         }

3、游戏中打开的UI一般是保存在一个集合中的。当需要给新的UI设置层级的时候,只需要通过一个循环遍历,找出已打开的UI中的最大层级,然后在此基础上加1,即 max + 1,然后将 max +1赋值给新打开的UI即可

猜你喜欢

转载自www.cnblogs.com/luguoshuai/p/9892070.html
今日推荐