unity實(shí)現(xiàn)無限列表功能
本文實(shí)例為大家分享了unity實(shí)現(xiàn)無限列表功能的具體代碼,供大家參考,具體內(nèi)容如下
public static class RectTransformExtensions { public static bool Overlaps(this RectTransform a, RectTransform b) { return a.WorldRect().Overlaps(b.WorldRect()); } public static bool Overlaps(this RectTransform a, RectTransform b, bool allowInverse) { return a.WorldRect().Overlaps(b.WorldRect(), allowInverse); } public static Rect WorldRect(this RectTransform rectTransform) { Vector2 sizeDelta = rectTransform.sizeDelta; float rectTransformWidth = sizeDelta.x * rectTransform.lossyScale.x; float rectTransformHeight = sizeDelta.y * rectTransform.lossyScale.y; Vector3 position = rectTransform.position; return new Rect( position.x - rectTransformWidth * rectTransform.pivot.x, position.y - rectTransformHeight * rectTransform.pivot.y, rectTransformWidth, rectTransformHeight); } /// <summary> /// /// </summary> /// <param name="rectTransform"></param> /// <param name="pos">世界坐標(biāo)的position</param> /// <returns></returns> public static Rect WorldRect2(this RectTransform rectTransform, Vector3 pos) { Rect rect = new Rect(); Vector2 sizeDelta = rectTransform.sizeDelta; float rectTransformWidth = sizeDelta.x * rectTransform.lossyScale.x; float rectTransformHeight = sizeDelta.y * rectTransform.lossyScale.y; Vector3 position = pos; rect.x = position.x - rectTransformWidth * rectTransform.pivot.x; rect.y = position.y - rectTransformHeight * rectTransform.pivot.y; rect.width = rectTransformWidth; rect.height = rectTransformHeight; return rect; } }
以上拓展方法是判斷兩個(gè)Recttransform類型的物體是否相交。
然后ScrollRec的滑動(dòng)回調(diào)方法中更新UI位置,代碼如下
private void OnScrollRectValueChanged(Vector2 arg0) { Dictionary<int, DynamicRect> inOverlaps = new Dictionary<int, DynamicRect>(); mRectMask = 遮罩物體的RectTransform.WorldRect(); //m_DynamicRectDic這個(gè)字典保存的是你所有UI需要放置的位置數(shù)據(jù), //判斷所有UI哪個(gè)是可見哪個(gè)不可見 ,保存起來 foreach (DynamicRect dR in m_DynamicRectDic.Values) { tmpTra.localPosition = dR.localPos; //獲取每個(gè)位置UI的世界坐標(biāo)Rect Rect rect = m_LevelItemPrefabRT.WorldRect2(tmpTra.position); if (rect.Overlaps(mRectMask)) { inOverlaps.Add(dR.Index, dR); } } //m_LevelItemList是保存你實(shí)例化后的UI列表,比如你這個(gè)遮罩頁面最多顯示3個(gè)UI,你需要實(shí)例化4個(gè)UI,然后動(dòng)態(tài)修改gameobject的顯示與隱藏 int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { //LevelItem是UI上掛載的腳本,用于更新UI界面的顯示和數(shù)據(jù)存儲(chǔ)的 LevelItem item = m_LevelItemList[i]; if (item.DRect != null && !inOverlaps.ContainsKey(item.DRect.Index)) { //item的DRect為null時(shí),隱藏物體,否則顯示物體 item.DRect = null; } } //判斷哪些可以重復(fù)利用的UI,然后賦予新的數(shù)據(jù)與位置 foreach (DynamicRect dR in inOverlaps.Values) { if (GetDynmicItem(dR) == null) { LevelItem item = GetNullDynmicItem(); if (item == null) continue; item.DRect = dR; //更新UI的位置和顯示(自己計(jì)算,每種顯示不一樣) _UpdateChildTransformPos(item.gameObject, dR.Index); } } } /// <summary> /// 通過動(dòng)態(tài)格子獲得動(dòng)態(tài)渲染器 /// </summary> /// <param name="rect"></param> /// <returns></returns> private LevelItem GetDynmicItem(DynamicRect rect) { int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { LevelItem item = m_LevelItemList[i]; if (item.DRect == null) continue; if (rect.Index == item.DRect.Index) return item; } return null; } /// <summary> /// 獲得待渲染的渲染器 /// </summary> /// <returns></returns> private LevelItem GetNullDynmicItem() { int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { LevelItem item = m_LevelItemList[i]; if (item.DRect == null) return item; } return null; } public class DynamicRect { /// <summary> /// 本地坐標(biāo) /// </summary> public Vector3 localPos; /// <summary> /// 格子索引 /// </summary> public int Index; public DynamicRect(int index, Vector3 localPos) { this.Index = index; this.localPos = localPos; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#精確到納秒級(jí)別的計(jì)時(shí)器類實(shí)現(xiàn)代碼
這篇文章主要介紹了C#精確到納秒級(jí)別的計(jì)時(shí)器類,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08C#控制臺(tái)帶參數(shù)程序源碼編寫實(shí)例講解
像ipconfig /all 這樣的CMD命令想必大家都知道,但是很多童鞋可能不知道怎么寫這樣的控制臺(tái)帶參數(shù)的程序,需要的朋友可以了解下2012-12-12c# Selenium爬取數(shù)據(jù)時(shí)防止webdriver封爬蟲的方法
這篇文章主要介紹了c# Selenium爬取數(shù)據(jù)時(shí)防止webdriver封爬蟲的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01.NET C#利用ZXing生成、識(shí)別二維碼/條形碼
ZXing是一個(gè)開放源碼的,用Java實(shí)現(xiàn)的多種格式的1D/2D條碼圖像處理庫,它包含了聯(lián)系到其他語言的端口。這篇文章主要給大家介紹了.NET C#利用ZXing生成、識(shí)別二維碼/條形碼的方法,文中給出了詳細(xì)的示例代碼,有需要的朋友們可以參考借鑒。2016-12-12Unity實(shí)戰(zhàn)之制作動(dòng)畫編輯器
為了更方便地為UI視圖添加動(dòng)畫,將動(dòng)畫的編輯功能封裝在了UI View類中,可以通過編輯器快速的為視圖編輯動(dòng)畫。本文將通過Unity制作一個(gè)動(dòng)畫編輯器,需要的可以參考一下2022-02-02wpf將表中數(shù)據(jù)顯示到datagrid示例
這篇文章主要介紹了wpf將表中數(shù)據(jù)顯示到datagrid示例,需要的朋友可以參考下2014-02-02