UIWrapContent实现Item复用

using System.Collections.Generic;
using UnityEngine;

/// + Scroll View
/// |- UIWrappedContent
/// |-- Item 1
/// |-- Item 2
/// |-- Item 3
/// 
public class LoopContent : UIWrapContent
{
    protected override void Start()
    {
        base.Start();
        onInitializeItem = OnUpdateItem;

        WrapItem[] items = this.GetComponentsInChildren<WrapItem>();
        for (int i = 0; i < items.Length; i++)
        {
            items[i].SetValue(Manager.GetData()[i]);
        }
    }

    private void OnUpdateItem(GameObject go, int index, int realIndex)
    {
        go.GetComponent<WrapItem>().SetValue(realIndex);
    }
}
using UnityEngine;
using System.Collections;

public class WrapItem : MonoBehaviour
{
    public UILabel lb_Name;

    public void SetValue(int index)
    {
        lb_Name.text = Manager.GetData()[index].ToString();
    }
}
public class Manager
{
    public static List<int> GetData()
    {
        List<int> nums = new List<int>();
        for (int i = 0; i < 20; i++)
            nums.Add(i);
        return nums;
    }
}

 

posted @ 2016-05-05 16:30  贴心小冰棍  阅读(1177)  评论(0编辑  收藏  举报