当前位置: 首页 > news >正文

Unity 列表滚动到指定位置

使用场景

策划提出需求:当玩家打开领奖界面时,奖励列表需要自动滑动到可以领奖的奖励栏处或者正在进行的任务栏处。

思路

1、将Content设置好对齐方式和锚点

子物体的预制体和Content:pivot轴心点设置为(0,1),并且设置为左上角对齐。

2、主要根据索引计算Content需要设置的高度即(RectTransform的PosY)

 var sumHeight = targetIndex * (itemHeight + spacing);
 scrollRect.content.anchoredPosition = new Vector2(0, sumHeight);

特殊情况

第一种  子物体较少的情况(Content高度小于可视区域高度)

不用移动,不必处理

第二种 滑动到末尾几个索引的时候

1、需要提前计算viewport可以最多完整囊括多少个子物体(visualCount)

2、如果需要定位的目标索引大于或等于(总数量 - visualCount+1)时,使用

scrollRect.normalizedPosition = new Vector2(0, 0);

 直接滑到底端就行。

代码

using UnityEngine;
using UnityEngine.UI;

public class Test : MonoBehaviour
{
    public int sumCount;
    public Transform root;
    public GameObject item;
    public ScrollRect scrollRect;
    public float itemHeight;
    public float spacing;//子物体之间的间隔
    public float contentHeight;
    public float viewportHeight;

    public int visualCount;
    public int compareCount;
    
    public InputField inputField;
    
    void Start()
    {
        sumCount = 100;
        spacing = 10;
        
        var itemRect = item.GetComponent<RectTransform>();
        itemHeight = itemRect.rect.height;
        
        for (var i = 1; i <= sumCount; i++)
        {
            var obj = Instantiate(item, root.transform);
            var txtObj = obj.transform.Find("txt");
            var txt = txtObj.GetComponent<Text>();
            txt.text = i.ToString();
        }
        LayoutRebuilder.ForceRebuildLayoutImmediate(scrollRect.content);
        contentHeight = scrollRect.content.rect.height;
        viewportHeight = scrollRect.viewport.rect.height;

        visualCount = (int)(viewportHeight / itemHeight);
        compareCount = sumCount - visualCount + 1;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var index = int.Parse(inputField.text);
            ScrollToTarget(index);
        }
    }
    
    public void ScrollToTarget(int index)
    {
        if (index <= 0 || index > sumCount)
        {
            return;
        }
        
        //第一种 子物体较少的情况(Content高度小于可视区域高度)
        if (contentHeight < viewportHeight)
        {
            return;
        }   
        
        //先停止列表滑动,以防造成误差
        scrollRect.StopMovement();
        //第二种 滑动到末尾几个索引的时候
        if (index >= compareCount)
        {
            scrollRect.normalizedPosition = new Vector2(0, 0);
            return;
        }
        var targetIndex = index - 1;
        targetIndex = targetIndex < 0 ? 0 : targetIndex;
        var sumHeight = targetIndex * (itemHeight + spacing);
        scrollRect.content.anchoredPosition = new Vector2(0, sumHeight);
    }
}

补充

1、如果需要添加滚动动画,可以使用Dotween来实现

 var sumHeight = targetIndex * (itemHeight + spacing);
 scrollRect.content.DOAnchorPosY(sumHeight,0.5f);

相关文章:

  • c++ std::array使用笔记
  • Python之re模块
  • 【HarmonyOS Next】鸿蒙状态管理装饰器V1和V2混用方案
  • 【Linux】进程间通信——命名管道
  • ONNX转RKNN的环境搭建
  • 计算机毕设-基于springboot的融合多源高校画像数据与协同过滤算法的高考择校推荐系统的设计与实现(附源码+lw+ppt+开题报告)
  • STM32 物联网智能家居 (七) 设备子系统--风扇控制
  • 以太坊测试网
  • 学术ppt模板_院士增选_自然科学奖_技术发明奖_科技进步奖_杰青_长江学者特聘教授_校企联聘长江学者_重点研发_优青_青长_青拔ppt制作案例
  • Spring Boot中Bean Validation的实战应用
  • flutter项目构建常见问题
  • 【前端基础】Day 3 CSS-2
  • Cherno 游戏引擎笔记(91~111)
  • Ubuntu2204下使用NVIDIA GeForce RTX 4090进行DeepSeek-R1-Distill-Llama-8B模型微调
  • SpringDataJPA使用deleteAllInBatch方法逻辑删除失效
  • 智能体编排与AI工作流的区别
  • SpringBoot整合SpringSecurity、MyBatis-Plus综合实例:认证、授权
  • Java面试要点120 - Java虚拟机栈帧结构
  • JavaScript 指南:从入门到实战开发
  • 如何使用useContext进行全局状态管理?
  • 网络注册公司怎么注册/重庆seo技术分享
  • 宿迁房产网签查询/沈阳seo搜索引擎
  • 做英语陪同翻译兼职的网站/什么是整合营销并举例说明
  • 网页设计与制作教程 刘瑞新/广东网络seo推广公司
  • 小学英语教师做应用相关网站/提高网站权重的方法
  • 网站建设和网络推广是干嘛/企业网站优化服务