不规则items排序

这篇博客介绍如何利用对象脚本中的高度和宽度属性,进行不规则items的排序操作,简化排序过程。
摘要由CSDN通过智能技术生成

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值