Unity排行榜优化滚动效果

这篇文章主要为大家详细介绍了Unity排行榜优化滚动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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 strs = new List();//需要修改 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 loopItems = new LinkedList(); #region 回调 private void Awake() { for (int i = 0; i ().sizeDelta.y + content.GetComponent().spacing; itemCount = (int)(transform.GetComponent().sizeDelta.y / hight) + 2; if (itemCount > DataTips) itemCount = DataTips; for (int i = 0; i ().text = strs[i]; loopItems.AddLast(obj); current++; } transform.GetComponent().GetWorldCorners(viewCorners); content.GetComponent().enabled = true; } private void Start() { Invoke("DisGrid", 0.1f); } #endregion #region 拖拽的时候 public void OnChange() { if (DataTips ().GetWorldCorners(rectCorners); if (rectCorners[0].y > viewCorners[1].y) { if (current + 1 ().text = strs[current]; loopItems.First.Value.GetComponent().localPosition = loopItems.Last.Value.GetComponent().localPosition - new Vector3(0, hight, 0); loopItems.AddLast(loopItems.First.Value); loopItems.RemoveFirst(); } } //当最后一个进入视野的时候 loopItems.First.Value.GetComponent().GetWorldCorners(rectCorners); if (rectCorners[1].y = 0) { loopItems.Last.Value.transform.GetChild(0).GetComponent().text = strs[current - itemCount]; loopItems.Last.Value.GetComponent().localPosition = loopItems.First.Value.GetComponent().localPosition + new Vector3(0, hight, 0); loopItems.AddFirst(loopItems.Last.Value); loopItems.RemoveLast(); current--; } } } #endregion public void DisGrid() { //关闭LayoutGroup content.GetComponent().enabled = false; //设置宽度 content.GetComponent().sizeDelta = new Vector2(content.GetComponent().sizeDelta.x, strs.Count * hight); } }

以上就是Unity排行榜优化滚动效果的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程