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<ScrollRect>().onValueChanged.AddListener((Vector2 v2)=> { UpdateRank(); });
        count = transform.childCount;
        x = transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition.x;
        firstY = transform.GetChild(0).position.y;
        posSpan = transform.GetChild(0).position.y - transform.GetChild(1).position.y;
        anchorSpan= transform.GetChild(0).GetComponent<RectTransform>().anchoredPosition3D.y - transform.GetChild(1).GetComponent<RectTransform>().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<RectTransform>().sizeDelta = new Vector2(transform.GetComponent<RectTransform>().sizeDelta.x, itemcount);


        rectTransforms = new RectTransform[count];
        for (int i = 0; i < count; i++)
        {
            rectTransforms[i] = transform.GetChild(i).GetComponent<RectTransform>();
            Debug.Log(transform.GetChild(i).GetComponent<RectTransform>().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<Text>()[0].text = (int.Parse(rectTransforms[i].GetComponentsInChildren<Text>()[0].text) + count*2).ToString();
                //rectTransforms[i].GetComponentsInChildren<Text>()[1].text = (int.Parse(rectTransforms[i].GetComponentsInChildren<Text>()[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<Text>()[0].text = (int.Parse(rectTransforms[i].GetComponentsInChildren<Text>()[0].text) - count * 2).ToString();
               // rectTransforms[i].GetComponentsInChildren<Text>()[1].text = (int.Parse(rectTransforms[i].GetComponentsInChildren<Text>()[1].text) - count * 2).ToString();
            }
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值