zoukankan      html  css  js  c++  java
  • 【NGUI】grid下面的item的重复利用

    http://blog.csdn.net/u012091672/article/details/21159075
    解决的问题 使用grid放置item的时候,每次数据可能都不一样,但是每次都删除grid下面的节点,之后动态创建新的item是比较浪费的。 写个简单的工具函数,原理很简单。
    1、先获得grid下面的可用item 2、根据data的大小进行初始化 3、每次在可用的item列表里面获取新的item,如果不够用了,就创建新的item 4、disable掉没用的item 附:每个grid下面预先要有一个名字包含“Template_”的模板item。这个模板不会被用,之前尝试过把这个模板也当做一个item正常使用,但是有些NGUI的widget会出现BUG。 [csharp] view plaincopy 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; } }
  • 相关阅读:
    JavaScript 相关记录
    首页大图淡入淡出效果工具flexslider
    取消chrome浏览器下input和textarea的默认样式;html5默认input内容清除“×”按钮去除办法
    Hibernate入门笔记
    Servlet入门笔记
    父容器利用opacity设置透明后,子元素跟着变透明的解决方案
    overflow:hidden与margin:0 auto之间的冲突
    初识Android Studio
    首页图片滚动效果
    DIV宽度设置成100%,浏览器窗口缩小后,右边出现留白
  • 原文地址:https://www.cnblogs.com/123ing/p/4062681.html
Copyright © 2011-2022 走看看