Unity排行榜優(yōu)化滾動效果
更新時間:2021年07月27日 14:54:39 作者:接啊是哦ida
這篇文章主要為大家詳細介紹了Unity排行榜優(yōu)化滾動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity排行榜優(yōu)化滾動效果的具體代碼,供大家參考,具體內容如下
自己做的一個優(yōu)化排行榜的功能,當有大量的數(shù)據(jù)需要在scroolRect中可以通過只夾在幾個item循環(huán)利用便可以展示所需的內容;
下面是效果實現(xiàn)圖
下面是我的一個中心思想
通過對處在視野第一個Item左上和左下左邊點的位置來判斷是將最后一個移動到第一個前面,還是將第一個移動到最后一個后面。
用到的我目前來說不太常用的數(shù)據(jù)結構 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所占的區(qū)域 int current = -1; int itemCount;//生成的Item的數(shù)量 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]; //當?shù)谝粋€離開視野的時候 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); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。