Ngui之UI框架的层级处理

 1         #region 处理层级问题
 2 
 3         void DepthIncrease(UIWndBase uiWnd)
 4         {
 5             DepthIncrease(uiWnd.transform, UIFlag);
 6         }
 7 
 8         public static void DepthIncrease(Transform root, UIFlag selfUIFlag)
 9         {
10             if (UIManager.SingleUIMgr.AllUIWnd == null)
11                 return;
12 
13             int maxDepth = 0;
14             foreach (var pair in UIManager.SingleUIMgr.AllUIWnd)
15             {
16                 if (pair.Value.UIID != selfUIFlag && pair.Value.UIID != UIFlag.ui_novice_guide)
17                 {
18                     int d = GetUIMaxDepth(pair.Value.transform);
19                     if (d > maxDepth)
20                         maxDepth = d;
21                 }
22             }
23 
24             // lua wnd
25             GameObject objNguiRoot = NGUIAssetHelp.NGUIRoot;
26             if (objNguiRoot)
27             {
28                 LuaComponentLoader[] luaLoaders = objNguiRoot.transform.GetComponentsInChildren<LuaComponentLoader>();
29                 for (int i = 0; i < luaLoaders.Length; i++)
30                 {
31                     LuaComponentLoader luaLoader = luaLoaders[i];
32                     if (luaLoader && !string.IsNullOrEmpty(luaLoader.ComponentName) && luaLoader.ComponentName.ToLower().Contains("wnd"))
33                     {
34                         int d = GetUIMaxDepth(luaLoader.transform);
35                         if (d > maxDepth)
36                             maxDepth = d;
37                     }
38                 }
39             }
40 
41             SetUIDepth(root, maxDepth + 1);
42         }
43 
44         // 设置ui界面中UIPanel的层级
45         static void SetUIDepth(Transform root, int depth)
46         {
47             UIPanel[] panels = root.GetComponentsInChildren<UIPanel>(true);
48             if (panels == null || panels.Length < 1)
49                 return;
50 
51             Array.Sort(panels, (a, b) => a.depth - b.depth);
52             for (int i = 0; i < panels.Length; i++)
53             {
54                 UIPanel p = panels[i];
55                 if (p != null)
56                     p.depth = i + depth;
57             }
58         }
59 
60         // 获得ui界面的UIPanel的最大层级
61         static int GetUIMaxDepth(Transform root)
62         {
63             UIPanel[] panels = root.GetComponentsInChildren<UIPanel>(false);
64             if (panels == null || panels.Length < 1)
65                 return 0;
66 
67             Array.Sort(panels, (a, b) => a.depth - b.depth);
68             UIPanel lastPanel = panels.LastOrDefault();
69             return lastPanel != null ? lastPanel.depth : 0;
70         }
71 
72         #endregion

转载请注明出处:https://www.cnblogs.com/jietian331/p/10911991.html

posted @ 2019-05-23 15:06  孤独の巡礼  阅读(577)  评论(0编辑  收藏  举报