您好,登錄后才能下訂單哦!
這篇文章主要講解了unity實(shí)現(xiàn)無限列表功能的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
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; } }
看完上述內(nèi)容,是不是對(duì)unity實(shí)現(xiàn)無限列表功能的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。