查看: 631|回复: 0
打印 上一主题 下一主题

[其他] NGUI 打开窗口时根据打开的顺序分配Depth

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53464
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-11-25 05:47:55 |只看该作者 |倒序浏览
NGUI 打开窗口时根据打开的顺序分配Depth,后打开的窗口depth逐渐升高调用方法
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. public class BagUI : MonoBehaviour {
  4.         bool isShow =false;
  5.         public void TransformShow()
  6.         {
  7.                 if(isShow==false)//打开窗口
  8.                 {
  9.                         transform.parent.localPosition =Vector3.zero;
  10.                         isShow =true;
  11.                         UIWindowsDepthManger.GetInstance.AddOpenWindow(transform);
  12.                 }
  13.                 else//关闭窗口
  14.                 {
  15.                         transform.parent.localPosition =new Vector3(1999,0,0);
  16.                         UIWindowsDepthManger.GetInstance.CloseWindow(transform);
  17.                         isShow =false;
  18.                 }
  19.         }
  20. }
复制代码
实现代码
  1. using UnityEngine;
  2. using System.Collections.Generic;

  3. /// <summary>
  4. /// 打开窗口时根据打开的顺序分配Depth,后打开的窗口depth逐渐升高,.
  5. /// 窗口打开后,搜索全部Panel,记录Depth,然后把depth提高,关闭时恢复记录的depth
  6. /// 恢复时,在子物体里根据Panel搜索,恢复depth
  7. /// </summary>
  8. public class UIWindowsDepthManger : MonoBehaviour {
  9.     public class DepthPanel
  10.     {
  11.         public int depth;//深度
  12.         public UIPanel panel;
  13.         public DepthPanel(int depth,UIPanel panel)
  14.         {
  15.             this.depth=depth;
  16.             this.panel=panel;
  17.         }
  18.     }
  19.     public class DepthP
  20.     {
  21.         public Transform transform;//父Panel
  22.         public List<DepthPanel> depthPanel_list;//父Panel下的所有panel深度数据列表
  23.         public DepthP(Transform transform)
  24.         {
  25.             this.transform = transform;
  26.             depthPanel_list =new List<DepthPanel>();
  27.         }
  28.         public void Add(DepthPanel depthPanel)
  29.         {
  30.             depthPanel_list.Add (depthPanel);
  31.         }

  32.     }

  33.     List<DepthP> dp_List = new List<DepthP> ();
  34.     static UIWindowsDepthManger _instance;
  35.     public static UIWindowsDepthManger GetInstance
  36.     {
  37.         get
  38.         {
  39.             if(_instance==null)
  40.             {
  41.                 GameObject go = new GameObject("UIWindowsDepthManger");
  42.                 _instance = go.AddComponent<UIWindowsDepthManger>();
  43.             }
  44.             return _instance;
  45.         }
  46.     }

  47.     void Awake()
  48.     {
  49.         if(_instance!=null)
  50.         {
  51.             Debug.LogError("many UIWindowsDepthManger");
  52.         }
  53.         _instance = this;
  54.     }

  55.     static int currentMaxDepth = 1000;//当前最高depth
  56.     /// <summary>
  57.     /// 把打开的窗口,UIpanel.depth提升.在窗口打开时调用
  58.     /// </summary>
  59.     /// <param name="transform">Transform.</param>
  60.     public void AddOpenWindow (Transform transform)
  61.     {
  62.         //检测transform是否是上一个窗口,是就返回
  63.         if(dp_List.Count>0)
  64.         {
  65.             if(dp_List[dp_List.Count-1].transform ==transform)
  66.             {
  67.                 return;
  68.             }
  69.             currentMaxDepth += 30;
  70.         }
  71.         //根据t搜索到最顶层的panel,搜索全部Panel,记录Depth,然后把depth提高,关闭时恢复记录的depth
  72.         //找到最顶层的panel
  73.         transformResult = null;
  74.         FindUpPanel (transform);
  75.         Transform panrent = transformResult;
  76.         DepthP depthP = new DepthP (panrent);
  77.         //搜索全部Panel
  78.         childArrayList.Clear ();
  79.         FindAllChilds (panrent);
  80.         foreach(Transform t in childArrayList)
  81.         {
  82.             UIPanel panel = t.GetComponent<UIPanel>();
  83.             if(panel!=null)
  84.             {
  85.                 //记录depth
  86.                 DepthPanel  depthPanel =new DepthPanel(panel.depth,panel);
  87.                 depthP.Add(depthPanel);
  88.                 //修改depth
  89.                 panel.depth += currentMaxDepth;
  90.             }
  91.         }
  92.         dp_List.Add(depthP);
  93.     }

  94.     /// <summary>
  95.     ///把关闭窗口,UIpanel.depth恢复.在窗口关闭前调用
  96.     /// </summary>
  97.     /// <param name="t">T.</param>
  98.     public void CloseWindow(Transform t)
  99.     {
  100.         /*
  101.          * 找到t的父Panel , 从 dp_List 里找到t
  102.          * 从 dp_List 恢复depth,removeITem
  103.          * 如果窗口全部关闭时,就重置currentMaxDepth
  104.          */
  105.         //找到最顶层的panel
  106.         transformResult = null;
  107.         FindUpPanel (t);
  108.         Transform panrent = transformResult;
  109.         int index = -1;
  110.         for(int i=0;i<dp_List.Count;i++)
  111.         {
  112.             if(dp_List[i].transform ==panrent)
  113.             {
  114.                 index =i;
  115.             }
  116.         }
  117.         //从 dp_List 恢复depth,removeITem
  118.         if(index!=-1)
  119.         {
  120.             foreach(DepthPanel depthPanel in dp_List[index].depthPanel_list)
  121.             {
  122.                 depthPanel.panel.depth = depthPanel.depth;
  123.             }
  124.             dp_List.RemoveAt(index);
  125.         }
  126.         if(dp_List.Count==0)
  127.         {
  128.             currentMaxDepth =1000;
  129.         }
  130.     }


  131.     /// 找到最顶层的panel.存入 transformResult
  132.     Transform transformResult;
  133.     void FindUpPanel(Transform transf)
  134.     {
  135.         if(transf.parent== UIRoot2DReference.Instance.transform)
  136.         {
  137.             transformResult =transf;
  138.             return;
  139.         }
  140.         FindUpPanel (transf.parent);
  141.     }

  142.     //找寻所有子物体,存入childArrayList
  143.     List<Transform> childArrayList = new List<Transform> ();
  144.     void FindAllChilds (Transform treeSource)
  145.     {
  146.         if (treeSource.childCount>0)
  147.         {
  148.             int i;
  149.             for (i=0;i<treeSource.childCount;i++)
  150.             {
  151.                 FindAllChilds (treeSource.GetChild(i));
  152.             }
  153.         }
  154.         childArrayList.Add(treeSource);
  155.     }
  156. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-12 13:29 , Processed in 0.085576 second(s), 30 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部