unityUGUI无限滑动列表

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


//UGUI无限滑动列表基本版
//第一步删除ScrollRect身上的Viewport  HorizontalScrollBar  VerticalScrollBar
//第二步设置Content对象左上角锚点对齐  挂载此脚本,同时设置C
//第三步Content的子对象都左上角锚点对齐
public class Test2 : MonoBehaviour {
    RectTransform[] rectTransforms;
    public float x;//anchoredPosition.x坐标
    public float firstY;//第一个初始Y值坐标
    public float posSpan;//positin.y坐标间隔
    public float anchorSpan;//anchoredPosition.y间隔也就是RectTransform的PosY
    int count;//初始子对象个数
    // Use this for initialization
    void Start()
    {     
 transform.parent.parent.GetComponent().onValueChanged.AddListener((Vector2 v2)=> { UpdateRank(); });
        count = transform.childCount;
        x = transform.GetChild(0).GetComponent().anchoredPosition.x;
        firstY = transform.GetChild(0).position.y;
        posSpan = transform.GetChild(0).position.y - transform.GetChild(1).position.y;
        anchorSpan= transform.GetChild(0).GetComponent().anchoredPosition3D.y - transform.GetChild(1).GetComponent().anchoredPosition3D.y;



        //根据实际项目个数来设置Content的高度  每个项目两条数据 100*220 22000
        //存在问题  如果需要显示偶数个,需要偶参数
        int itemcount = 100;//获取实际个数
        if (itemcount / 2 * anchorSpan>50000)
        {
             itemcount =int.Parse((itemcount/2 * anchorSpan).ToString());
        }
        else
        {
            itemcount = 50000;
        }
        transform.GetComponent().sizeDelta = new Vector2(transform.GetComponent().sizeDelta.x, itemcount);


        rectTransforms = new RectTransform[count];
        for (int i = 0; i < count; i++)
        {
            rectTransforms[i] = transform.GetChild(i).GetComponent();
            Debug.Log(transform.GetChild(i).GetComponent().anchoredPosition3D.y);//对应rect坐标
        }
    }

    // Update is called once per frame
    void UpdateRank()
    {
        for (int i = 0; i < rectTransforms.Length; i++)
        {
            if (rectTransforms[i].position.y > firstY + posSpan)
            {
                rectTransforms[i].anchoredPosition = new Vector2(x, rectTransforms[i].anchoredPosition.y - anchorSpan * count);
                //赋值
               // rectTransforms[i].GetComponentsInChildren()[0].text = (int.Parse(rectTransforms[i].GetComponentsInChildren()[0].text) + count*2).ToString();
                //rectTransforms[i].GetComponentsInChildren()[1].text = (int.Parse(rectTransforms[i].GetComponentsInChildren()[1].text) + count * 2).ToString();
            }

            if (rectTransforms[i].position.y < firstY - posSpan * (count-1))
            {
                rectTransforms[i].anchoredPosition = new Vector2(x, rectTransforms[i].anchoredPosition.y + anchorSpan * count);
                //赋值
               // rectTransforms[i].GetComponentsInChildren()[0].text = (int.Parse(rectTransforms[i].GetComponentsInChildren()[0].text) - count * 2).ToString();
               // rectTransforms[i].GetComponentsInChildren()[1].text = (int.Parse(rectTransforms[i].GetComponentsInChildren()[1].text) - count * 2).ToString();
            }
        }
    }
}

 

你可能感兴趣的:(unityUGUI无限滑动列表)