Unity排行榜優(yōu)化滾動(dòng)效果
本文實(shí)例為大家分享了Unity排行榜優(yōu)化滾動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
自己做的一個(gè)優(yōu)化排行榜的功能,當(dāng)有大量的數(shù)據(jù)需要在scroolRect中可以通過(guò)只夾在幾個(gè)item循環(huán)利用便可以展示所需的內(nèi)容;
下面是效果實(shí)現(xiàn)圖
下面是我的一個(gè)中心思想
通過(guò)對(duì)處在視野第一個(gè)Item左上和左下左邊點(diǎn)的位置來(lái)判斷是將最后一個(gè)移動(dòng)到第一個(gè)前面,還是將第一個(gè)移動(dòng)到最后一個(gè)后面。
用到的我目前來(lái)說(shuō)不太常用的數(shù)據(jù)結(jié)構(gòu) LinkedList 方便用于移除第一個(gè)和最后一個(gè);
以下是代碼
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 回調(diào) 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 拖拽的時(shí)候 public void OnChange() { if (DataTips < itemCount) { return; } Vector3[] rectCorners = new Vector3[4]; //當(dāng)?shù)谝粋€(gè)離開(kāi)視野的時(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(); } } //當(dāng)最后一個(gè)進(jìn)入視野的時(shí)候 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() { //關(guān)閉LayoutGroup content.GetComponent<VerticalLayoutGroup>().enabled = false; //設(shè)置寬度 content.GetComponent<RectTransform>().sizeDelta = new Vector2(content.GetComponent<RectTransform>().sizeDelta.x, strs.Count * hight); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。