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
轉載請註明出處:http://www.javashuo.com/article/p-hnekcrxf-gm.htmlhtml