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

Unity UGUI 垂直循环复用滚动

一 基础类 在unity里面新建这几个类

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 垂直方向滚动
/// </summary>
public class CustomScroll:MonoBehaviour
{public ScrollRect scrollRect;protected List<object> list = new();private RectTransform contentRect;private GridLayoutGroup layoutGroup;public GameObject item;private List<CustomScrollItemMono> scrollTestItems = new();private CustomVertial customVertial;private int startIndex;private int endIndex;private int showItemCount = 14;private void Awake(){contentRect = scrollRect.content.GetComponent<RectTransform>();layoutGroup = contentRect.GetComponent<GridLayoutGroup>();customVertial = contentRect.GetComponent<CustomVertial>();scrollRect.horizontal = false;scrollRect.vertical = true;scrollRect.onValueChanged.AddListener((value) =>{layoutGroup.enabled = false;customVertial.enabled = false;for (int i = startIndex; i < itemPosList.Count; i++){var y = contentRect.anchoredPosition3D.y;if (i + layoutGroup.constraintCount < itemPosList.Count) {if (scrollRect.velocity.y > 0)//手指上滑 {var targetY = -itemPosList[i + layoutGroup.constraintCount].y;if (y >= targetY){startIndex = i + layoutGroup.constraintCount;endIndex = startIndex + showItemCount - layoutGroup.constraintCount;break;}}else if (scrollRect.velocity.y < 0)//手指下滑{if (startIndex > 0 && startIndex  < itemPosDic.Count) {var targetY = -itemPosDic[startIndex].y;if (y <= targetY){startIndex = i - layoutGroup.constraintCount;endIndex = startIndex + showItemCount - layoutGroup.constraintCount;break;}}}}}Debug.Log($"bbb startIndex {startIndex} endIndex {endIndex}");if (endIndex >= itemPosDic.Count) { return; }int index = 0;for (int i = startIndex; i < endIndex + layoutGroup.constraintCount; i++){if (index < scrollTestItems.Count && i < itemPosDic.Count) {var item = scrollTestItems[index];item.Init(i, list[i]);var rect = item.gameObject.GetComponent<RectTransform>();rect.anchoredPosition3D = itemPosDic[i];index += 1;}}});}private Dictionary<int, Vector3> itemPosDic = new();private List<Vector3> itemPosList = new();public void Init() {InitListData();InitItemsPos();InitContentSize();InitShowItems();}public virtual void InitListData() {//必须要新建类 继承此类  重写此方法设置数据}private void InitItemsPos() {int prevRow = 0;int initIndex = 0;for (int i = 0; i < list.Count; i++){int row = Mathf.CeilToInt((i + 1) * 1f / layoutGroup.constraintCount);var pos = Vector3.zero;if (row != prevRow){pos = new Vector3(0, -((row - 1) * (layoutGroup.cellSize.y + layoutGroup.spacing.y) + layoutGroup.padding.top));initIndex = 0;}else{initIndex += 1;pos = new Vector3(initIndex * (layoutGroup.cellSize.x + layoutGroup.spacing.x) + layoutGroup.padding.left, -((row - 1) * (layoutGroup.cellSize.y + layoutGroup.spacing.y) + layoutGroup.padding.top));}itemPosList.Add(pos);itemPosDic.Add(i, pos);prevRow = row;}}private void InitContentSize() {float width = layoutGroup.padding.left + layoutGroup.cellSize.x * layoutGroup.constraintCount +(layoutGroup.constraintCount - 1) * layoutGroup.spacing.x;float height = layoutGroup.padding.top + layoutGroup.cellSize.y * Mathf.Ceil(itemPosList.Count / layoutGroup.constraintCount) +itemPosList.Count / layoutGroup.constraintCount * layoutGroup.spacing.y;contentRect.sizeDelta = new Vector2(width, height);customVertial.SetSize(new Vector2(width, height));}private void InitShowItems() {for (int i = 0; i < showItemCount; i++){GameObject obj = Instantiate(item, contentRect);RectTransform rect = obj.GetComponent<RectTransform>();rect.anchorMin = new Vector2(0, 1);rect.anchorMax = new Vector2(0, 1);rect.pivot = new Vector2(0, 1);obj.transform.name = i.ToString();obj.SetActive(true);CustomScrollItemMono testItem = obj.GetComponent<CustomScrollItemMono>();testItem.Init(i, list[i]);scrollTestItems.Add(testItem);}item.SetActive(false);}
}public class ScrollTestData 
{public int ID;
}
using System.Collections.Generic;
using UnityEngine;public interface ICustomScrollItem
{public void Init(int index,object data);
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;//这个类一定要挂在GridLayout下面 控制Content尺寸
public class CustomVertial : ContentSizeFitter
{private Vector2 size = new();private RectTransform rectTransform;protected override void Awake(){base.Awake();rectTransform = GetComponent<RectTransform>();}public void SetSize(Vector2 s) {size = s;}public override void SetLayoutVertical(){base.SetLayoutVertical();rectTransform.sizeDelta = size;}
}
using System.Collections.Generic;
using UnityEngine;public class CustomScrollItemMono : MonoBehaviour,ICustomScrollItem
{public void Init(int index, object data){InitView(index, data);}public virtual void InitView(int index, object data) {}
}

二 具体使用实例类

1.挂在ScrollRect组件下面 在面板上设置好Scroll和Content和子元素Item

   重写了InitListData 方法 设置需要的列表数据  这里测试放在Start里面启动列表

   实际根据具体逻辑启动

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CustomScrollTest:CustomScroll
{public override void InitListData(){base.InitListData();for (int i = 0; i < 100; i++){list.Add(new ScrollTestData() { ID = i });}}private void Start(){Init();}
}

2.具体的Item元素类 挂在Item上

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;public class ScrollTestItem : CustomScrollItemMono
{public Text text;private ScrollTestData Data= new();private int Index;public override void InitView(int index, object data){ScrollTestData testData = (ScrollTestData)data;Index = index;Data = testData;text.text = Index.ToString();transform.name = Index.ToString();}
}

相关文章:

Unity UGUI 垂直循环复用滚动

一 基础类 在unity里面新建这几个类 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// <summary> /// 垂直方向滚动 /// </summary> public class CustomScroll:MonoBehaviour {public …...

Spring MVC 深度剖析:优势与劣势全面解读

文章目录 Spring MVC 优势1. **松耦合**2. **易于测试**3. **灵活性**4. **强大的配置机制**5. **异常处理**6. **国际化支持**7. **数据验证**8. **安全性**9. **性能优化** Spring MVC 劣势1. **学习曲线**2. **配置复杂性**3. **性能开销**4. **视图技术限制**5. **社区和支…...

力扣hot100-->前缀和/前缀书/LRU缓存

前缀和 1. 560. 和为 K 的子数组 中等 给你一个整数数组 nums 和一个整数 k &#xff0c;请你统计并返回 该数组中和为 k 的子数组的个数 。 子数组是数组中元素的连续非空序列。 示例 1&#xff1a; 输入&#xff1a;nums [1,1,1], k 2 输出&#xff1a;2示例 2&#…...

Three.js CSS2D/CSS3D渲染器

在Three.js开发过程中&#xff0c;有时需要将 HTML 元素与 Three.js 渲染的 3D 场景相结合&#xff0c;这就需要用到 CSS2DRenderer 和 CSS3DRenderer。本文将详细介绍这两种渲染器的原理及其应用 一、CSS2DRenderer 渲染器 概述 CSS2DRenderer 渲染器用于在 3D 场景中渲染纯…...

mongodb文档字符串批量替换

【mongodb文档字符串批量替换脚本语句】 前言&#xff1a; 1、本方式对于数据量大的情况不适用&#xff0c;执行可能比较慢&#xff1b; 2、数据量大的情况&#xff0c;个人推荐代码层面解决&#xff0c;多线程替换更快&#xff1a; &#xff08;1&#xff09;写实体类的方式…...

前端安全和解决方案

提到这个我可能想到的就是不要暴露太多的账号密码信息。一些页面的请求和操作要加上权限。 然后下面就详细的介绍前端可能遇到的安全问题以及解决方法。 首先比较常见的前端的安全性问题就是跨站脚本攻击&#xff08;XSS&#xff09;。跨站请求伪造&#xff08;csrf&#xff…...

Tlias智能辅助学习系统-部门管理

包括查询、新增、删除、修改功能 控制层 package com.itheima.controller;import com.itheima.pojo.Dept; import com.itheima.pojo.Result; import com.itheima.service.DeptService; import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.XSlf4j; import org.spr…...

React第十节组件之间传值之context

1、Context 使用creatContext() 和 useContext() Hook 实现多层级传值 概述&#xff1a; 在我们想要每个层级都需要某一属性&#xff0c;或者祖孙之间需要传值时&#xff0c;我们可以使用 props 一层一层的向下传递&#xff0c;或者我们使用更便捷的方案&#xff0c;用 creatC…...

flink中barrier不对齐的原因和影响

Barrier 不对齐&#xff08;Barrier Misalignment&#xff09;可能导致一些性能和一致性相关的问题&#xff0c;但 Flink 提供了机制来确保即使在不对齐的情况下&#xff0c;也可以保证数据的一致性。 1. 什么是 Barrier 不对齐&#xff1f; Barrier 不对齐是指在分布式数据流…...

软银集团孙正义再度加码OpenAI,近屿智能专注AI人才培养

11月28日凌晨&#xff0c;全球最大财经CNBC报道&#xff0c;软银集团创始人兼CEO孙正义再次向人工智能领域的领军企业OpenAI投资了15亿美元。软银对OpenAI的投资已不是首次。就在上个月&#xff0c;软银已在OpenAI的上一轮融资中注入了5亿美元的资金。但他一直寻求获得OpenAI更…...

麒麟系统x86安装达梦数据库

一、安装准备前工作 操作系统&#xff1a;银河麒麟V10&#xff0c;CPU&#xff1a; x86_64 架构 下载地址&#xff0c;麒麟官网&#xff1a;https://www.kylinos.cn/ 数据库&#xff1a;dm8_20220915_x86_kylin10_64 下载地址&#xff0c;达梦数据库官网&#xff1a;https://…...

Java中的“多态“详解

多态&#xff08;Polymorphism&#xff09;是面向对象编程&#xff08;OOP&#xff09;中的一个核心概念&#xff0c;它允许同一个接口或方法在不同对象上具有不同的实现方式。多态性使得程序在运行时可以根据对象的实际类型来决定调用哪个方法&#xff0c;从而提高代码的灵活性…...

buuctf-[SUCTF 2019]EasySQL 1解题记录

把你的旗帜给我&#xff0c;我会告诉你这面旗帜是对的。 堆叠注入查询数据库 1; show databases; ​ 查询表名 1; show tables; 获取flag 1;set sql_modepipes_as_concat;select 1...

ASP.NET Core 入门

使用 .NET CLI 创建并运行 ASP.NET Core Web 应用。 文章目录 一、先决条件二、创建Web应用项目三、运行应用四、编辑Razor页面 一、先决条件 .NET 8.0 SDK 二、创建Web应用项目 打开命令行界面&#xff0c;然后输入以下命令&#xff1a; dotnet new webapp --output aspne…...

php反序列化1_常见php序列化的CTF考题

声明&#xff1a; 以下多内容来自暗月师傅我是通过他的教程来学习记录的&#xff0c;如有侵权联系删除。 一道反序列化的CTF题分享_ctf反序列化题目_Mr.95的博客-CSDN博客 一些其他大佬的wp参考&#xff1a;php_反序列化_1 | dayu’s blog (killdayu.com) 序列化一个对象将…...

题目 1013: [编程入门]Sn的公式求和

题目 1013: [编程入门]Sn的公式求和 [编程入门]Sn的公式求和 求Snaaaaaa…aa…aaa&#xff08;有n个a&#xff09;之值&#xff0c;其中a是一个数字&#xff0c;为2。 例如&#xff0c;n5时222222222222222&#xff0c;n由键盘输入。 #include<stdio.h> int A(int n)…...

算法——赎金信(leetcode383)

题目&#xff1a; 给你两个字符串&#xff1a;ransomNote 和 magazine &#xff0c;判断 ransomNote 能不能由 magazine 里面的字符构成。 如果可以&#xff0c;返回 true &#xff1b;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。 示例 1&#…...

transformers训练(NLP)阅读理解(多项选择)

简介 在阅读理解任务中&#xff0c;有一种通过多项选择其中一个答案来训练机器的阅读理解。比如&#xff1a;给定一个或多个文档h,以及一个问题S和对应的多个答案候选&#xff0c;输出问题S的答案E&#xff0c;E是答案候选中的某一个选项。 这样的目的就是通过文档&#xff0c…...

微软企业邮箱:安全可靠的企业级邮件服务!

微软企业邮箱的设置步骤&#xff1f;如何注册使用烽火域名邮箱&#xff1f; 微软企业邮箱作为一款专为企业设计的邮件服务&#xff0c;不仅提供了高效便捷的通信工具&#xff0c;更在安全性、可靠性和功能性方面树立了行业标杆。烽火将深入探讨微软企业邮箱的多重优势。 微软…...

什么是分布式锁

定义 分布式锁是控制分布式系统或集群中不同节点对共享资源访问的一种机制。在分布式环境下&#xff0c;多个节点&#xff08;如多个服务器或多个进程&#xff09;可能会同时访问诸如数据库中的某条记录、一个共享文件或者一个全局计数器等共享资源。分布式锁的目的是确保在同一…...

Ubuntu系统下交叉编译openssl

一、参考资料 OpenSSL&&libcurl库的交叉编译 - hesetone - 博客园 二、准备工作 1. 编译环境 宿主机&#xff1a;Ubuntu 20.04.6 LTSHost&#xff1a;ARM32位交叉编译器&#xff1a;arm-linux-gnueabihf-gcc-11.1.0 2. 设置交叉编译工具链 在交叉编译之前&#x…...

Java 语言特性(面试系列1)

一、面向对象编程 1. 封装&#xff08;Encapsulation&#xff09; 定义&#xff1a;将数据&#xff08;属性&#xff09;和操作数据的方法绑定在一起&#xff0c;通过访问控制符&#xff08;private、protected、public&#xff09;隐藏内部实现细节。示例&#xff1a; public …...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

解锁数据库简洁之道:FastAPI与SQLModel实战指南

在构建现代Web应用程序时&#xff0c;与数据库的交互无疑是核心环节。虽然传统的数据库操作方式&#xff08;如直接编写SQL语句与psycopg2交互&#xff09;赋予了我们精细的控制权&#xff0c;但在面对日益复杂的业务逻辑和快速迭代的需求时&#xff0c;这种方式的开发效率和可…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

Reasoning over Uncertain Text by Generative Large Language Models

https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829https://ojs.aaai.org/index.php/AAAI/article/view/34674/36829 1. 概述 文本中的不确定性在许多语境中传达,从日常对话到特定领域的文档(例如医学文档)(Heritage 2013;Landmark、Gulbrandsen 和 Svenevei…...

iOS性能调优实战:借助克魔(KeyMob)与常用工具深度洞察App瓶颈

在日常iOS开发过程中&#xff0c;性能问题往往是最令人头疼的一类Bug。尤其是在App上线前的压测阶段或是处理用户反馈的高发期&#xff0c;开发者往往需要面对卡顿、崩溃、能耗异常、日志混乱等一系列问题。这些问题表面上看似偶发&#xff0c;但背后往往隐藏着系统资源调度不当…...

A2A JS SDK 完整教程:快速入门指南

目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库&#xff…...

基于Springboot+Vue的办公管理系统

角色&#xff1a; 管理员、员工 技术&#xff1a; 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能&#xff1a; 该办公管理系统是一个综合性的企业内部管理平台&#xff0c;旨在提升企业运营效率和员工管理水…...

宇树科技,改名了!

提到国内具身智能和机器人领域的代表企业&#xff0c;那宇树科技&#xff08;Unitree&#xff09;必须名列其榜。 最近&#xff0c;宇树科技的一项新变动消息在业界引发了不少关注和讨论&#xff0c;即&#xff1a; 宇树向其合作伙伴发布了一封公司名称变更函称&#xff0c;因…...