码迷,mamicode.com
首页 > 其他好文 > 详细

UIGI 一级二级三级四级啦啦啦等列表层式排列效果

时间:2016-04-05 00:37:21      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

在每个需要排序的物体上放入GridTag 脚本 其中GridTag脚本用于提供此物体的深度

 1 using UnityEngine;
 2 using System.Collections;
 3 using UnityEngine.UI;
 4 
 5 public class GridTag : MonoBehaviour
 6 {
 7     private int depth = 0;
 8 
 9     public int Depth
10     {
11         get
12         {
13             return depth;
14         }
15     }
16 
17     void Awake()
18     {
19         FindDepth(transform);
20 
21         Debug.Log("当前深度"+depth);
22     }
23 
24     void FindDepth(Transform trans)
25     {
26         if (trans.parent != null)
27         {
28             depth--;
29             trans = trans.parent;
30             FindDepth(trans);
31         }
32     }
33 }

Sorting 通过GridTag 和 其中的深度值 排序

  1 using UnityEngine;
  2 using System.Collections;
  3 using System.Collections.Generic;
  4 
  5 /// <summary>
  6 /// 给物体排序
  7 /// </summary>
  8 public class Sorting : MonoBehaviour
  9 {
 10     /// <summary>
 11     /// 第一个物体 偏移调节
 12     /// </summary>
 13     public Vector3 startPostion;
 14 
 15     /// <summary>
 16     /// panel的宽和高
 17     /// </summary>
 18     Vector2 theSize;
 19 
 20     /// <summary>
 21     /// 横条的背部面板 用于滑动条。。。
 22     /// </summary>
 23     public RectTransform panel;
 24 
 25     /// <summary>
 26     /// 上一个同深度序列
 27     /// </summary>
 28     private int lastIndex;
 29 
 30     void Start()
 31     {
 32         Sort();
 33     }
 34 
 35     //void Update()
 36     //{
 37     //    Sort(this.gameObject);
 38 
 39     //}
 40 
 41     /// <summary>
 42     /// 获取物体下的GridTag数组 并且 对gameObj 下的物体进行排列
 43     /// </summary>
 44     /// <param name="gameObj">父物体</param>
 45     public void Sort()
 46     {
 47         float height = 0;
 48 
 49         GridTag[] gridTags;
 50 
 51         RectTransform[] gridRect;
 52 
 53         int[] depthList;
 54 
 55         gridTags = gameObject.GetComponentsInChildren<GridTag>();
 56 
 57         gridRect = new RectTransform[gridTags.Length];
 58 
 59         depthList = new int[gridTags.Length];
 60 
 61         //DepthSort(gridTags, depthIndex, ref depthList);
 62         Depth_RectList(gridTags, gridRect, depthList);
 63 
 64         //PositionSort(height, gridTags, depthList);
 65         SizeControl(gridRect);
 66 
 67         SortPosition(height, gridTags, gridRect, depthList);
 68 
 69     }
 70 
 71     /// <summary>
 72     /// 排列物体下 含GridTag 的位置
 73     /// </summary>
 74     private void SortPosition(float height, GridTag[] gridTags, RectTransform[] gridRect, int[] depthList)
 75     {
 76         //遍历所有要排列的物体
 77         for (int i = 0; i < gridTags.Length; i++)
 78         {
 79             //如果横条的深度值为最大 即为第一级 判断其位置
 80             if (gridTags[i].Depth == depthList[0])
 81             {
 82                 height = 0;
 83                 for (int b = 0; b < i; b++)
 84                 {
 85                     height += gridRect[b].sizeDelta.y;
 86                 }
 87                 //transform.GetChild(i).GetComponent<RectTransform>().anchoredPosition3D = new Vector3(918, transform.GetChild(i).GetComponent<RectTransform>().sizeDelta.y * (-0.5f - i), 0);
 88                 gridRect[i].anchoredPosition3D = startPostion + new Vector3(0, -height - gridRect[i].sizeDelta.y / 2, 0);
 89             }
 90                 //如果横条上一个为同级 向下排列
 91             else if (gridTags[i].Depth == gridTags[i - 1].Depth)
 92             {
 93                 gridRect[i].anchoredPosition3D = gridRect[i - 1].anchoredPosition3D - new Vector3(0, gridRect[i].sizeDelta.y, 0);
 94             }
 95                 //如果横条的深度值低于上一个 即为同一级中第一个 
 96             else if (gridTags[i].Depth < gridTags[i - 1].Depth)
 97             {
 98                 gridRect[i].anchoredPosition3D = new Vector3(0, -(gridRect[i - 1].sizeDelta.y + gridRect[i].sizeDelta.y) / 2, 0);
 99             }
100                 //如果横条深度大于上一个 即2 3 2 中第二个2 则按下面方式排列
101             else if (gridTags[i].Depth > gridTags[i - 1].Depth)
102             {
103                 for (int a = i - 1; a > 0; a--)
104                 {
105                     if (gridTags[a].Depth == gridTags[i].Depth)
106                     {
107                         lastIndex = a;
108                         break;
109                     }
110                 }
111                 Debug.Log("上层序列:" + lastIndex);
112 
113                 height = gridRect[i - 1].sizeDelta.y * (i - lastIndex - 1) + gridRect[i].sizeDelta.y;
114 
115                 gridRect[i].anchoredPosition3D = gridRect[lastIndex].anchoredPosition3D + new Vector3(0, -height, 0);
116             }
117         }
118     }
119 
120     /// <summary>
121     /// 控制横条面板的宽和高
122     /// </summary>
123     /// <param name="gridRect"></param>
124     private void SizeControl(RectTransform[] gridRect)
125     {
126         theSize.y = 0;
127         theSize.x = panel.sizeDelta.x;
128 
129         foreach (var item in gridRect)
130         {
131             theSize.y += item.sizeDelta.y;
132         }
133 
134         if (theSize.y < transform.parent.GetComponent<RectTransform>().sizeDelta.y)
135             theSize.y = transform.parent.GetComponent<RectTransform>().sizeDelta.y;
136 
137         panel.sizeDelta = theSize;
138     }
139 
140     /// <summary>
141     /// 通过GridTag数组 获取深度和Rect数组
142     /// </summary>
143     private void Depth_RectList(GridTag[] gridTags, RectTransform[] gridRect, int[] depthList)
144     {
145         for (int i = 0; i < gridTags.Length; i++)
146         {
147             depthList[i] = gridTags[i].Depth;
148             gridRect[i] = gridTags[i].GetComponent<RectTransform>();
149 
150             Debug.Log("" + i + "个," + "当前深度:::" + depthList[i]);
151         }
152     }
153 }

 

UIGI 一级二级三级四级啦啦啦等列表层式排列效果

标签:

原文地址:http://www.cnblogs.com/redUnity/p/5353324.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!