【Unity3D】【NGUI】grid下面的item的重复利用

38 篇文章 3 订阅

NGUI讨论群:333417608


解决的问题

使用grid放置item的时候,每次数据可能都不一样,但是每次都删除grid下面的节点,之后动态创建新的item是比较浪费的。

写个简单的工具函数,原理很简单。

1、先获得grid下面的可用item

2、根据data的大小进行初始化

3、每次在可用的item列表里面获取新的item,如果不够用了,就创建新的item

4、disable掉没用的item

附:每个grid下面预先要有一个名字包含“Template_”的模板item。这个模板不会被用,之前尝试过把这个模板也当做一个item正常使用,但是有些NGUI的widget会出现BUG。

using UnityEngine;
using System.Collections.Generic;
//qq group :333417608
public class UITools
{
	
	/* usage:
		List<GameObject> canUseList = UITools.GetCanUseItemList(gridRoot);
		for (int i=0; i<totalData.Length; ++i)
		{
			GameObject go = UITools.GetNewItemObj(canUseList, gridRoot, prefab);
			// do init
		}
		UITools.UnActiveUnuseItem(canUseList);

		// prefab is the template
	 */ 

	static public GameObject GetNewItemObj (List<GameObject> canUseList, GameObject root, GameObject prefab)
	{
		GameObject go = null;
		if (canUseList.Count > 0) {
			go = canUseList [0];
			canUseList.RemoveAt (0);
		} else {
			go = NGUITools.AddChild (root, prefab);
		}
		NGUITools.SetActiveSelf (go, true);
		return go;
	}
	
	static public T GetNewItemObj<T> (List<T> canUseList, GameObject root, GameObject prefab) where T : Component
	{
		T item = null;
		if (canUseList.Count > 0) {
			item = canUseList [0];
			canUseList.RemoveAt (0);
		} else {
			item = NGUITools.AddChild (root, prefab).GetComponent<T>();
		}
		item.name = string.Format("{0:D3}", 0);
		NGUITools.SetActiveSelf (item.gameObject, true);
		return item;
	}
	
	static public List<GameObject> GetCanUseItemList (GameObject root)
	{
		List<GameObject> itemList = new List<GameObject> ();
		Transform rootT = root.transform;
		for (int i=0; i<rootT.childCount; ++i) {
			GameObject go = rootT.GetChild (i).gameObject;
			if (IsNotTemplateGameObject(go))
			{
				itemList.Add (go);
			}
		}
		return itemList;
	}
	
	static public List<T> GetCanUseItemList<T> (GameObject root) where T : Component
	{
		List<T> childrenList = new List<T> ();
		Transform rootT = root.transform;
		for (int i=0; i<rootT.childCount; ++i) {
			Transform child = rootT.GetChild (i);
			T t = child.GetComponent<T> ();
			if (t != null && IsNotTemplateGameObject(child.gameObject)) {
				childrenList.Add (t);
			}
		}
		return childrenList;
	}
	
	static public void UnActiveUnuseItem (List<GameObject> canUseList)
	{
		foreach (var item in canUseList) {
			NGUITools.SetActiveSelf (item, false);
		}
	}
	
	static public void UnActiveUnuseItem<T> (List<T> canUseList) where T : Component
	{
		foreach (var item in canUseList) {
			NGUITools.SetActiveSelf (item.gameObject, false);
		}
	}
	
	static private bool IsNotTemplateGameObject(GameObject go)
	{
		bool result = !go.name.ToLower().Contains("template_");
		if (!result && go.activeSelf)
		{
			NGUITools.SetActiveSelf(go, false);
		}
		return result;
	}
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值