Unity UGUI无限循环列表ScrollView(横向)

Unity做游戏避免不了使用列表,比如关卡游戏 有可能会有上千个关卡,这个使用UGUI原生列表会非常卡,下面提供了一种方法,可以无限循环列表,并且很流畅
列表循环展示:
在这里插入图片描述

1:功能实现代码


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

public class MainStart : MonoBehaviour
{
    public FitScrollViewH curfit;

    public int AllItemNum = 1000;

    // Start is called before the first frame update
    void Start()
    {
        ReloadItems();
    }

    public void ReloadItems()
    {
        curfit.SetElement = delegate(ScrollViewTemplate temp)
        {
            //todo 做自己想做的事,赋值列表信息等
            temp.width = 150;//列表宽
            Transform tf = temp.ts;//列表节点
            int index = temp.index + 1;
            Transform title = tf.Find("Text");
            title.GetComponent<Text>().text = String.Format("第{0}个数据",index);
        };
        curfit.Total = AllItemNum;//列表总个数
        curfit.Initialize(1);//列表定位
    }
}

2:列表功能

1刷新界面数据 调用方法

 curfit.UpdateData();

当界面数据有更新时调用该方法 列表位置不变,数据变成最新的

2标题列表定位

当进入游戏中需要定位到某一个数据在第一列时调用方法

 curfit.Initialize(sel);//列表定位sel:要定位数据的索引位置

3:控件脚本

主要控件脚本有两个

1:FitScrollViewBase.CS
2: FitScrollViewH.CS

4:unity设置
4.1:脚本导入
在这里插入图片描述
4.2:拖拽代码
在这里插入图片描述
4.3:运行代码
在这里插入图片描述
关注微信公众号:回复UGUI无限列表资源
请添加图片描述
下一篇:Unity UGUI无限列表循环 (竖向)

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现无限循环ScrollView,可以使用 Unity 的 UI 布局组件和代码结合的方式来实现。 首先,在 ScrollView 中添加一个 Content 布局组件,用于控制 ScrollView 中的子控件排列。 然后,在代码中动态生成需要显示的子控件,并将其添加到 Content 中。为了实现无限循环,需要在首尾各添加一个相同的子控件,这样在滑动到末尾时,可以无缝地切换到开头,实现循环。 以下是一个简单的示例代码,可以放在 ScrollView 的父物体上: ```csharp using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class InfiniteScrollView : MonoBehaviour { public GameObject itemPrefab; public int itemCount = 10; private List<GameObject> itemList = new List<GameObject>(); private RectTransform contentRectTransform; private float itemHeight; private int currentItemIndex = 0; void Start() { // 获取 Content 的 RectTransform 组件 contentRectTransform = GetComponentInChildren<ScrollRect>().content; // 获取子控件的高度 itemHeight = itemPrefab.GetComponent<RectTransform>().rect.height; // 生成子控件 for (int i = 0; i < itemCount + 2; i++) { GameObject item = Instantiate(itemPrefab, contentRectTransform); item.transform.localPosition = new Vector3(0, -itemHeight * i, 0); itemList.Add(item); } // 更新 Content 的高度 contentRectTransform.sizeDelta = new Vector2(contentRectTransform.sizeDelta.x, itemHeight * (itemCount + 2)); // 更新子控件的内容 UpdateItemContent(); } void Update() { // 判断是否需要切换子控件 if (Input.GetKeyDown(KeyCode.UpArrow)) { currentItemIndex++; if (currentItemIndex > itemCount + 1) { currentItemIndex = 1; } UpdateItemContent(); } else if (Input.GetKeyDown(KeyCode.DownArrow)) { currentItemIndex--; if (currentItemIndex < 1) { currentItemIndex = itemCount + 1; } UpdateItemContent(); } } void UpdateItemContent() { // 更新子控件的内容 for (int i = 0; i < itemList.Count; i++) { int index = currentItemIndex - (itemList.Count - i - 1); if (index <= 0) { index += itemCount; } else if (index > itemCount) { index -= itemCount; } itemList[i].GetComponentInChildren<Text>().text = "Item " + index; } // 更新 Content 的位置 contentRectTransform.anchoredPosition = new Vector2(contentRectTransform.anchoredPosition.x, itemHeight * (currentItemIndex - 1)); } } ``` 这个示例代码实现了一个简单的无限循环 ScrollView,可以根据实际需求进行修改。其中,itemPrefab 是用于生成子控件的预制体,itemCount 是需要显示的子控件个数。在 UpdateItemContent 函数中,根据当前的 currentItemIndex 来更新子控件的内容,并将 Content 移动到对应的位置。在 Update 函数中,通过监听上下箭头键来模拟手指滑动,实现切换子控件的效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yffgamestart

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值