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

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

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52352
精华
343

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

跳转到指定楼层
楼主
发表于 2016-1-16 03:41:15 |只看该作者 |倒序浏览

调用方法

  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. }

  21. //实现代码
  22. using UnityEngine;
  23. using System.Collections.Generic;

  24. /// <summary>
  25. /// 打开窗口时根据打开的顺序分配Depth,后打开的窗口depth逐渐升高,.
  26. /// 窗口打开后,搜索全部Panel,记录Depth,然后把depth提高,关闭时恢复记录的depth
  27. /// 恢复时,在子物体里根据Panel搜索,恢复depth
  28. /// </summary>
  29. public class UIWindowsDepthManger : MonoBehaviour {

  30.     public int depthIncremental =37;//depth每次增量

  31.     public class DepthPanel
  32.     {
  33.         public int depth;//深度
  34.         public UIPanel panel;
  35.         public DepthPanel(int depth,UIPanel panel)
  36.         {
  37.             this.depth=depth;
  38.             this.panel=panel;
  39.         }
  40.     }
  41.     public class DepthP
  42.     {
  43.         public Transform transform;//父Panel
  44.         public List<DepthPanel> depthPanel_list;//父Panel下的所有panel深度数据列表
  45.         public DepthP(Transform transform)
  46.         {
  47.             this.transform = transform;
  48.             depthPanel_list =new List<DepthPanel>();
  49.         }
  50.         public void Add(DepthPanel depthPanel)
  51.         {
  52.             depthPanel_list.Add (depthPanel);
  53.         }

  54.     }

  55.     List<DepthP> dp_List = new List<DepthP> ();
  56.     static UIWindowsDepthManger _instance;
  57.     public static UIWindowsDepthManger GetInstance
  58.     {
  59.         get
  60.         {
  61.             if(_instance==null)
  62.             {
  63.                 GameObject go = new GameObject("UIWindowsDepthManger");
  64.                 _instance = go.AddComponent<UIWindowsDepthManger>();
  65.             }
  66.             return _instance;
  67.         }
  68.     }

  69.     void Awake()
  70.     {
  71.         if(_instance!=null)
  72.         {
  73.             Debug.LogError("many UIWindowsDepthManger");
  74.         }
  75.         _instance = this;
  76.     }

  77.     static int currentMaxDepth = 1000;//当前最高depth

  78.     /// <summary>
  79.     /// 把打开的窗口,UIpanel.depth提升.在窗口打开时调用
  80.     /// </summary>
  81.     /// <param name="transform">Transform.</param>
  82.     public void AddOpenWindow (Transform transform)
  83.     {
  84.         //检测transform是否是上一个窗口,是就返回
  85.         if(dp_List.Count>0)
  86.         {
  87.             if(dp_List[dp_List.Count-1].transform ==GetUpPanel(transform))
  88.             {
  89.                 return;
  90.             }
  91.             currentMaxDepth += depthIncremental;
  92.         }
  93.         //根据t搜索到最顶层的panel,搜索全部Panel,记录Depth,然后把depth提高,关闭时恢复记录的depth
  94.         //找到最顶层的panel
  95.         Transform panrent =GetUpPanel( transform);
  96.         DepthP depthP = new DepthP (panrent);
  97.         //搜索全部Panel
  98.         childArrayList.Clear ();
  99.         FindAllChilds (panrent);
  100.         foreach(Transform t in childArrayList)
  101.         {
  102.             UIPanel panel = t.GetComponent<UIPanel>();
  103.             if(panel!=null)
  104.             {
  105.                 //记录depth
  106.                 DepthPanel  depthPanel =new DepthPanel(panel.depth,panel);
  107.                 depthP.Add(depthPanel);
  108.                 //修改depth
  109.                 panel.depth += currentMaxDepth;
  110.             }
  111.         }
  112.         dp_List.Add(depthP);
  113.     }

  114.     /// <summary>
  115.     ///把关闭窗口,UIpanel.depth恢复.在窗口关闭前调用
  116.     /// </summary>
  117.     /// <param name="t">T.</param>
  118.     public void CloseWindow(Transform t)
  119.     {
  120.         /*
  121.          * 找到t的父Panel , 从 dp_List 里找到t
  122.          * 从 dp_List 恢复depth,removeITem
  123.          * 如果窗口全部关闭时,就重置currentMaxDepth
  124.          */
  125.         //找到最顶层的panel
  126.         Transform panrent = GetUpPanel( t);
  127.         int index = -1;
  128.         for(int i=0;i<dp_List.Count;i++)
  129.         {
  130.             if(dp_List[i].transform ==panrent)
  131.             {
  132.                 index =i;
  133.             }
  134.         }
  135.         //从 dp_List 恢复depth,removeITem
  136.         if(index!=-1)
  137.         {
  138.             foreach(DepthPanel depthPanel in dp_List[index].depthPanel_list)
  139.             {
  140.                 depthPanel.panel.depth = depthPanel.depth;
  141.             }
  142.             dp_List.RemoveAt(index);
  143.         }
  144.         if(dp_List.Count==0)
  145.         {
  146.             currentMaxDepth =1000;
  147.         }
  148.     }


  149.     //找到最顶层的panel
  150.     Transform GetUpPanel(Transform tf)
  151.     {
  152.         transformResult = null;
  153.         FindUpPanel (tf);
  154.         Transform panrent = transformResult;
  155.         return panrent;
  156.     }


  157.     /// 找到最顶层的panel.存入 transformResult
  158.     Transform transformResult;
  159.     void FindUpPanel(Transform transf)
  160.     {
  161.         if(transf.parent== UIRoot2DReference.Instance.transform)
  162.         {
  163.             transformResult =transf;
  164.             return;
  165.         }
  166.         FindUpPanel (transf.parent);
  167.     }

  168.     //找寻所有子物体,存入childArrayList
  169.     List<Transform> childArrayList = new List<Transform> ();
  170.     void FindAllChilds (Transform treeSource)
  171.     {
  172.         if (treeSource.childCount>0)
  173.         {
  174.             int i;
  175.             for (i=0;i<treeSource.childCount;i++)
  176.             {
  177.                 FindAllChilds (treeSource.GetChild(i));
  178.             }
  179.         }
  180.         childArrayList.Add(treeSource);
  181.     }
  182. }
复制代码
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

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

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-9-13 07:24 , Processed in 0.113221 second(s), 28 queries .

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

© 2008-2019 Narkii Inc.

回顶部