1. obj的身上的脚本继承与下面这个类 (以便排序的时候获取方便,直接获取这个脚本的height 和 width就好了)
using UnityEngine;
using System.Collections;
public class UIItemSize : MonoBehaviour {
/// <summary>
/// 高
/// </summary>
public int _height = 0;
/// <summary>
/// 宽
/// </summary>
public int _width = 0;
public virtual int height
{
get { return _height; }
set { _height = value; }
}
public virtual int width
{
get { return _width; }
set { _width = value; }
}
}
2. obj身上的脚本 (使用上面脚本的方法)
public class YuanFenItem: UIItemSize
{
public void SetData(HeroFateData fateData)
{
height = 10;
width = 10;
}
}
3. 排序脚本
/// <summary>
/// 排序Items
/// </summary>
/// <param name="items">排序的数组items</param>
/// <param name="dic">间距</param>
/// <param name="x">初始posX</param>
/// <param name="y">初始posY</param>
/// <param name="isv">true 竖排 false 横排</param>
public static void OnSortItems(List<GameObject> items,int dic,int x = 0,int y =0, bool isv = true)
{
if (items == null || items.Count == 0)
return ;
int allheight = 0;
for (int i = 0;i < items.Count;i ++)
{
GameObject go = items[i];
if (go.gameObject.activeInHierarchy == false)
continue;
UIItemSize com = go.GetComponent<UIItemSize>();
if (com == null)
continue;
if (isv == true)
{
int h = com._height;
go.gameObject.transform.localPosition = new Vector3 (x,y-allheight,0);
allheight = allheight + h + dic;
}
else
{
int w = com._width;
go.gameObject.transform.localPosition = new Vector3 (x+allheight,y,0);
allheight = allheight + w + dic;
}
}
}