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

 using UnityEngine;
using System.Collections;
using UnityEngine.UI; public class GridTag : MonoBehaviour
{
private int depth = ; public int Depth
{
get
{
return depth;
}
} void Awake()
{
FindDepth(transform); Debug.Log("当前深度"+depth);
} void FindDepth(Transform trans)
{
if (trans.parent != null)
{
depth--;
trans = trans.parent;
FindDepth(trans);
}
}
}

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

 using UnityEngine;
using System.Collections;
using System.Collections.Generic; /// <summary>
/// 给物体排序
/// </summary>
public class Sorting : MonoBehaviour
{
/// <summary>
/// 第一个物体 偏移调节
/// </summary>
public Vector3 startPostion; /// <summary>
/// panel的宽和高
/// </summary>
Vector2 theSize; /// <summary>
/// 横条的背部面板 用于滑动条。。。
/// </summary>
public RectTransform panel; /// <summary>
/// 上一个同深度序列
/// </summary>
private int lastIndex; void Start()
{
Sort();
} //void Update()
//{
// Sort(this.gameObject); //} /// <summary>
/// 获取物体下的GridTag数组 并且 对gameObj 下的物体进行排列
/// </summary>
/// <param name="gameObj">父物体</param>
public void Sort()
{
float height = ; GridTag[] gridTags; RectTransform[] gridRect; int[] depthList; gridTags = gameObject.GetComponentsInChildren<GridTag>(); gridRect = new RectTransform[gridTags.Length]; depthList = new int[gridTags.Length]; //DepthSort(gridTags, depthIndex, ref depthList);
Depth_RectList(gridTags, gridRect, depthList); //PositionSort(height, gridTags, depthList);
SizeControl(gridRect); SortPosition(height, gridTags, gridRect, depthList); } /// <summary>
/// 排列物体下 含GridTag 的位置
/// </summary>
private void SortPosition(float height, GridTag[] gridTags, RectTransform[] gridRect, int[] depthList)
{
//遍历所有要排列的物体
for (int i = ; i < gridTags.Length; i++)
{
//如果横条的深度值为最大 即为第一级 判断其位置
if (gridTags[i].Depth == depthList[])
{
height = ;
for (int b = ; b < i; b++)
{
height += gridRect[b].sizeDelta.y;
}
//transform.GetChild(i).GetComponent<RectTransform>().anchoredPosition3D = new Vector3(918, transform.GetChild(i).GetComponent<RectTransform>().sizeDelta.y * (-0.5f - i), 0);
gridRect[i].anchoredPosition3D = startPostion + new Vector3(, -height - gridRect[i].sizeDelta.y / , );
}
//如果横条上一个为同级 向下排列
else if (gridTags[i].Depth == gridTags[i - ].Depth)
{
gridRect[i].anchoredPosition3D = gridRect[i - ].anchoredPosition3D - new Vector3(, gridRect[i].sizeDelta.y, );
}
//如果横条的深度值低于上一个 即为同一级中第一个
else if (gridTags[i].Depth < gridTags[i - ].Depth)
{
gridRect[i].anchoredPosition3D = new Vector3(, -(gridRect[i - ].sizeDelta.y + gridRect[i].sizeDelta.y) / , );
}
//如果横条深度大于上一个 即2 3 2 中第二个2 则按下面方式排列
else if (gridTags[i].Depth > gridTags[i - ].Depth)
{
for (int a = i - ; a > ; a--)
{
if (gridTags[a].Depth == gridTags[i].Depth)
{
lastIndex = a;
break;
}
}
Debug.Log("上层序列:" + lastIndex); height = gridRect[i - ].sizeDelta.y * (i - lastIndex - ) + gridRect[i].sizeDelta.y; gridRect[i].anchoredPosition3D = gridRect[lastIndex].anchoredPosition3D + new Vector3(, -height, );
}
}
} /// <summary>
/// 控制横条面板的宽和高
/// </summary>
/// <param name="gridRect"></param>
private void SizeControl(RectTransform[] gridRect)
{
theSize.y = ;
theSize.x = panel.sizeDelta.x; foreach (var item in gridRect)
{
theSize.y += item.sizeDelta.y;
} if (theSize.y < transform.parent.GetComponent<RectTransform>().sizeDelta.y)
theSize.y = transform.parent.GetComponent<RectTransform>().sizeDelta.y; panel.sizeDelta = theSize;
} /// <summary>
/// 通过GridTag数组 获取深度和Rect数组
/// </summary>
private void Depth_RectList(GridTag[] gridTags, RectTransform[] gridRect, int[] depthList)
{
for (int i = ; i < gridTags.Length; i++)
{
depthList[i] = gridTags[i].Depth;
gridRect[i] = gridTags[i].GetComponent<RectTransform>(); Debug.Log("第" + i + "个," + "当前深度:::" + depthList[i]);
}
}
}
05-11 13:29