Unity排行榜優(yōu)化滾動效果
本文實例為大家分享了Unity排行榜優(yōu)化滾動效果的具體代碼,供大家參考,具體內(nèi)容如下
自己做的一個優(yōu)化排行榜的功能,當有大量的數(shù)據(jù)需要在scroolRect中可以通過只夾在幾個item循環(huán)利用便可以展示所需的內(nèi)容;
下面是效果實現(xiàn)圖

下面是我的一個中心思想

通過對處在視野第一個Item左上和左下左邊點的位置來判斷是將最后一個移動到第一個前面,還是將第一個移動到最后一個后面。
用到的我目前來說不太常用的數(shù)據(jù)結(jié)構(gòu) 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 回調(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 拖拽的時候
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()
{
//關(guān)閉LayoutGroup
content.GetComponent<VerticalLayoutGroup>().enabled = false;
//設(shè)置寬度
content.GetComponent<RectTransform>().sizeDelta = new Vector2(content.GetComponent<RectTransform>().sizeDelta.x, strs.Count * hight);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#異常處理中try和catch語句及finally語句的用法示例
這篇文章主要介紹了C#異常處理中try和catch語句及finally語句的用法示例,finally語句的使用涉及到了C#的垃圾回收特性,需要的朋友可以參考下2016-02-02
C#實現(xiàn)快速將數(shù)據(jù)寫入Excel單元格
這篇文章主要為大家詳細介紹了如何使用C#實現(xiàn)快速將數(shù)據(jù)寫入Excel單元格,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
C#中通過反射將枚舉元素加載到ComboBo的實現(xiàn)方法
本文主要介紹了C#中通過反射將枚舉元素加載到ComboBo的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09

