using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; /// /// 滚动列表优化,元素循环使用 /// public class TestMain : MonoBehaviour { /// /// 测试用数据 /// private List mData = new List(); /// /// 滚动列表优化 /// private UIWarpContent1 warpContent = null; /// /// 初始化 /// private void Start() { // 组织测试数据 for (int i = 0; i < 50; i++) { mData.Add(new Item("测试:" + i.ToString())); } warpContent = gameObject.transform.GetComponentInChildren(); warpContent.onInitializeItem = onInitializeItem; // 注意:目标init方法必须在warpContent.onInitializeItem之后 warpContent.Init(mData.Count); } /// /// 元素初始化 /// /// 元素 /// 数据索引 private void onInitializeItem(GameObject go, int dataIndex) { Text text = go.transform.Find("Text").GetComponent(); text.text = "i:" + dataIndex + "_N:" + mData[dataIndex].Name(); } /// /// 测试用数据结构 /// public class Item { /// /// 名称 /// private string name; /// /// 构造函数 /// /// 名称字符串 public Item(string name) { this.name = name; } /// /// 获取名称 /// /// 名称字符串 public string Name() { return this.name; } /// /// 删除 /// public void destroy() { this.name = null; } } }