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 ,请你统计并返回 该数组中和为 k 的子数组的个数 。 子数组是数组中元素的连续非空序列。 示例 1: 输入:nums [1,1,1], k 2 输出:2示例 2&#…...

Three.js CSS2D/CSS3D渲染器
在Three.js开发过程中,有时需要将 HTML 元素与 Three.js 渲染的 3D 场景相结合,这就需要用到 CSS2DRenderer 和 CSS3DRenderer。本文将详细介绍这两种渲染器的原理及其应用 一、CSS2DRenderer 渲染器 概述 CSS2DRenderer 渲染器用于在 3D 场景中渲染纯…...
mongodb文档字符串批量替换
【mongodb文档字符串批量替换脚本语句】 前言: 1、本方式对于数据量大的情况不适用,执行可能比较慢; 2、数据量大的情况,个人推荐代码层面解决,多线程替换更快: (1)写实体类的方式…...
前端安全和解决方案
提到这个我可能想到的就是不要暴露太多的账号密码信息。一些页面的请求和操作要加上权限。 然后下面就详细的介绍前端可能遇到的安全问题以及解决方法。 首先比较常见的前端的安全性问题就是跨站脚本攻击(XSS)。跨站请求伪造(csrfÿ…...
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 实现多层级传值 概述: 在我们想要每个层级都需要某一属性,或者祖孙之间需要传值时,我们可以使用 props 一层一层的向下传递,或者我们使用更便捷的方案,用 creatC…...
flink中barrier不对齐的原因和影响
Barrier 不对齐(Barrier Misalignment)可能导致一些性能和一致性相关的问题,但 Flink 提供了机制来确保即使在不对齐的情况下,也可以保证数据的一致性。 1. 什么是 Barrier 不对齐? Barrier 不对齐是指在分布式数据流…...

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

麒麟系统x86安装达梦数据库
一、安装准备前工作 操作系统:银河麒麟V10,CPU: x86_64 架构 下载地址,麒麟官网:https://www.kylinos.cn/ 数据库:dm8_20220915_x86_kylin10_64 下载地址,达梦数据库官网:https://…...
Java中的“多态“详解
多态(Polymorphism)是面向对象编程(OOP)中的一个核心概念,它允许同一个接口或方法在不同对象上具有不同的实现方式。多态性使得程序在运行时可以根据对象的实际类型来决定调用哪个方法,从而提高代码的灵活性…...

buuctf-[SUCTF 2019]EasySQL 1解题记录
把你的旗帜给我,我会告诉你这面旗帜是对的。 堆叠注入查询数据库 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应用项目 打开命令行界面,然后输入以下命令: dotnet new webapp --output aspne…...

php反序列化1_常见php序列化的CTF考题
声明: 以下多内容来自暗月师傅我是通过他的教程来学习记录的,如有侵权联系删除。 一道反序列化的CTF题分享_ctf反序列化题目_Mr.95的博客-CSDN博客 一些其他大佬的wp参考:php_反序列化_1 | dayu’s blog (killdayu.com) 序列化一个对象将…...
题目 1013: [编程入门]Sn的公式求和
题目 1013: [编程入门]Sn的公式求和 [编程入门]Sn的公式求和 求Snaaaaaa…aa…aaa(有n个a)之值,其中a是一个数字,为2。 例如,n5时222222222222222,n由键盘输入。 #include<stdio.h> int A(int n)…...

算法——赎金信(leetcode383)
题目: 给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。 如果可以,返回 true ;否则返回 false 。 magazine 中的每个字符只能在 ransomNote 中使用一次。 示例 1&#…...
transformers训练(NLP)阅读理解(多项选择)
简介 在阅读理解任务中,有一种通过多项选择其中一个答案来训练机器的阅读理解。比如:给定一个或多个文档h,以及一个问题S和对应的多个答案候选,输出问题S的答案E,E是答案候选中的某一个选项。 这样的目的就是通过文档,…...

微软企业邮箱:安全可靠的企业级邮件服务!
微软企业邮箱的设置步骤?如何注册使用烽火域名邮箱? 微软企业邮箱作为一款专为企业设计的邮件服务,不仅提供了高效便捷的通信工具,更在安全性、可靠性和功能性方面树立了行业标杆。烽火将深入探讨微软企业邮箱的多重优势。 微软…...
什么是分布式锁
定义 分布式锁是控制分布式系统或集群中不同节点对共享资源访问的一种机制。在分布式环境下,多个节点(如多个服务器或多个进程)可能会同时访问诸如数据库中的某条记录、一个共享文件或者一个全局计数器等共享资源。分布式锁的目的是确保在同一…...

Mybatis逆向工程,动态创建实体类、条件扩展类、Mapper接口、Mapper.xml映射文件
今天呢,博主的学习进度也是步入了Java Mybatis 框架,目前正在逐步杨帆旗航。 那么接下来就给大家出一期有关 Mybatis 逆向工程的教学,希望能对大家有所帮助,也特别欢迎大家指点不足之处,小生很乐意接受正确的建议&…...

【快手拥抱开源】通过快手团队开源的 KwaiCoder-AutoThink-preview 解锁大语言模型的潜力
引言: 在人工智能快速发展的浪潮中,快手Kwaipilot团队推出的 KwaiCoder-AutoThink-preview 具有里程碑意义——这是首个公开的AutoThink大语言模型(LLM)。该模型代表着该领域的重大突破,通过独特方式融合思考与非思考…...

MODBUS TCP转CANopen 技术赋能高效协同作业
在现代工业自动化领域,MODBUS TCP和CANopen两种通讯协议因其稳定性和高效性被广泛应用于各种设备和系统中。而随着科技的不断进步,这两种通讯协议也正在被逐步融合,形成了一种新型的通讯方式——开疆智能MODBUS TCP转CANopen网关KJ-TCPC-CANP…...
css的定位(position)详解:相对定位 绝对定位 固定定位
在 CSS 中,元素的定位通过 position 属性控制,共有 5 种定位模式:static(静态定位)、relative(相对定位)、absolute(绝对定位)、fixed(固定定位)和…...

以光量子为例,详解量子获取方式
光量子技术获取量子比特可在室温下进行。该方式有望通过与名为硅光子学(silicon photonics)的光波导(optical waveguide)芯片制造技术和光纤等光通信技术相结合来实现量子计算机。量子力学中,光既是波又是粒子。光子本…...

网站指纹识别
网站指纹识别 网站的最基本组成:服务器(操作系统)、中间件(web容器)、脚本语言、数据厍 为什么要了解这些?举个例子:发现了一个文件读取漏洞,我们需要读/etc/passwd,如…...

android13 app的触摸问题定位分析流程
一、知识点 一般来说,触摸问题都是app层面出问题,我们可以在ViewRootImpl.java添加log的方式定位;如果是touchableRegion的计算问题,就会相对比较麻烦了,需要通过adb shell dumpsys input > input.log指令,且通过打印堆栈的方式,逐步定位问题,并找到修改方案。 问题…...

解析奥地利 XARION激光超声检测系统:无膜光学麦克风 + 无耦合剂的技术协同优势及多元应用
在工业制造领域,无损检测(NDT)的精度与效率直接影响产品质量与生产安全。奥地利 XARION开发的激光超声精密检测系统,以非接触式光学麦克风技术为核心,打破传统检测瓶颈,为半导体、航空航天、汽车制造等行业提供了高灵敏…...
CppCon 2015 学习:Time Programming Fundamentals
Civil Time 公历时间 特点: 共 6 个字段: Year(年)Month(月)Day(日)Hour(小时)Minute(分钟)Second(秒) 表示…...

leetcode_69.x的平方根
题目如下 : 看到题 ,我们最原始的想法就是暴力解决: for(long long i 0;i<INT_MAX;i){if(i*ix){return i;}else if((i*i>x)&&((i-1)*(i-1)<x)){return i-1;}}我们直接开始遍历,我们是整数的平方根,所以我们分两…...