Unity排行榜优化滚动效果
作者:接啊是哦ida 时间:2021-09-08 13:02:07
本文实例为大家分享了Unity排行榜优化滚动效果的具体代码,供大家参考,具体内容如下
自己做的一个优化排行榜的功能,当有大量的数据需要在scroolRect中可以通过只夹在几个item循环利用便可以展示所需的内容;
下面是效果实现图
下面是我的一个中心思想
通过对处在视野第一个Item左上和左下左边点的位置来判断是将最后一个移动到第一个前面,还是将第一个移动到最后一个后面。
用到的我目前来说不太常用的数据结构 LinkedList 方便用于移除第一个和最后一个;
以下是代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class WuXianGunDong : MonoBehaviour
{
List<string> strs = new List<string>();//需要修改
public int DataTips;
public Transform content;
public GameObject loopItemPrefab;
Vector3[] viewCorners = new Vector3[4];
float hight; //生成的loopItem所占的区域
int current = -1;
int itemCount;//生成的Item的数量
public LinkedList<GameObject> loopItems = new LinkedList<GameObject>();
#region 回调
private void Awake()
{
for (int i = 0; i < DataTips; i++)
{
strs.Add(i.ToString());
}
hight = loopItemPrefab.GetComponent<RectTransform>().sizeDelta.y + content.GetComponent<VerticalLayoutGroup>().spacing;
itemCount = (int)(transform.GetComponent<RectTransform>().sizeDelta.y / hight) + 2;
if (itemCount > DataTips)
itemCount = DataTips;
for (int i = 0; i < itemCount; i++)
{
GameObject obj = Instantiate(loopItemPrefab, content);
obj.GetComponentInChildren<Text>().text = strs[i];
loopItems.AddLast(obj);
current++;
}
transform.GetComponent<RectTransform>().GetWorldCorners(viewCorners);
content.GetComponent<VerticalLayoutGroup>().enabled = true;
}
private void Start()
{
Invoke("DisGrid", 0.1f);
}
#endregion
#region 拖拽的时候
public void OnChange()
{
if (DataTips < itemCount)
{
return;
}
Vector3[] rectCorners = new Vector3[4];
//当第一个离开视野的时候
loopItems.First.Value.GetComponent<RectTransform>().GetWorldCorners(rectCorners);
if (rectCorners[0].y > viewCorners[1].y)
{
if (current + 1 < strs.Count)
{
current++;
loopItems.First.Value.transform.GetComponentInChildren<Text>().text = strs[current];
loopItems.First.Value.GetComponent<RectTransform>().localPosition = loopItems.Last.Value.GetComponent<RectTransform>().localPosition - new Vector3(0, hight, 0);
loopItems.AddLast(loopItems.First.Value);
loopItems.RemoveFirst();
}
}
//当最后一个进入视野的时候
loopItems.First.Value.GetComponent<RectTransform>().GetWorldCorners(rectCorners);
if (rectCorners[1].y < viewCorners[1].y)
{
if (current - itemCount >= 0)
{
loopItems.Last.Value.transform.GetChild(0).GetComponent<Text>().text = strs[current - itemCount];
loopItems.Last.Value.GetComponent<RectTransform>().localPosition = loopItems.First.Value.GetComponent<RectTransform>().localPosition + new Vector3(0, hight, 0);
loopItems.AddFirst(loopItems.Last.Value);
loopItems.RemoveLast();
current--;
}
}
}
#endregion
public void DisGrid()
{
//关闭LayoutGroup
content.GetComponent<VerticalLayoutGroup>().enabled = false;
//设置宽度
content.GetComponent<RectTransform>().sizeDelta = new Vector2(content.GetComponent<RectTransform>().sizeDelta.x, strs.Count * hight);
}
}
来源:https://blog.csdn.net/weixin_47634555/article/details/106857090
标签:Unity,排行榜,滚动
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你使用Java获取当前时间戳的详细代码
2021-09-19 04:41:02
Android 中TextView中跑马灯效果的实现方法
2023-08-07 01:39:46
使用java实现http多线程断点下载文件(二)
2021-06-08 04:26:17
C# NAudio 库的各种常见使用方式之播放 录制 转码 音频可视化
2023-06-20 04:14:16
![](https://img.aspxhome.com/file/2023/1/82151_0s.png)
SparkSQL开窗函数分析使用示例
2022-04-16 02:26:32
图解二叉树的三种遍历方式及java实现代码
2022-02-08 12:03:32
![](https://img.aspxhome.com/file/2023/7/83027_0s.png)
MyBatis动态SQL标签用法实例详解
2023-11-26 01:04:27
C#文件分割的方法
2023-09-17 22:20:56
Jetpack Compose常用组件详细介绍
2022-10-16 19:42:58
![](https://img.aspxhome.com/file/2023/4/90134_0s.png)
Android引导页面的简单实现
2023-08-06 04:24:32
![](https://img.aspxhome.com/file/2023/3/86013_0s.gif)
Java线程池实现原理总结
2023-04-15 02:37:11
![](https://img.aspxhome.com/file/2023/5/88575_0s.png)
SpringBoot 使用Mybatis分页插件实现详解
2023-03-06 22:34:19
![](https://img.aspxhome.com/file/2023/3/85523_0s.png)
Java中锁的实现和内存语义浅析
2021-06-04 01:09:49
Android App实现监听软键盘按键的三种方式
2021-09-14 10:39:27
![](https://img.aspxhome.com/file/2023/3/91703_0s.png)
Java及数据库对日期进行格式化方式
2023-08-23 12:27:52
![](https://img.aspxhome.com/file/2023/2/58462_0s.jpg)
java中URLEncoder.encode与URLDecoder.decode处理url特殊参数的方法
2022-05-22 05:32:00
使用IDEA创建servlet JavaWeb 应用及使用Tomcat本地部署的实现
2023-09-11 06:04:09
![](https://img.aspxhome.com/file/2023/4/57814_0s.jpg)
C#实现文件与字符串互转的方法详解
2023-04-26 03:18:12
![](https://img.aspxhome.com/file/2023/2/70402_0s.png)
C#超市收银系统设计
2023-06-21 00:00:09
![](https://img.aspxhome.com/file/2023/9/66579_0s.png)
详解Spring MVC 集成EHCache缓存
2022-05-28 04:42:52
![](https://img.aspxhome.com/file/2023/3/77933_0s.png)