這篇文章主要講解了unity實(shí)現(xiàn)無(wú)限列表功能的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
成都創(chuàng)新互聯(lián)專(zhuān)注于天河企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站建設(shè)。天河網(wǎng)站建設(shè)公司,為天河等地區(qū)提供建站服務(wù)。全流程按需定制開(kāi)發(fā),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
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類(lèi)型的物體是否相交。
然后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è)是可見(jiàn)哪個(gè)不可見(jiàn) ,保存起來(lái) 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è)遮罩頁(yè)面最多顯示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> /// 通過(guò)動(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)無(wú)限列表功能的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:unity實(shí)現(xiàn)無(wú)限列表功能的方法
標(biāo)題路徑:http://jinyejixie.com/article8/jjshop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、電子商務(wù)、網(wǎng)站營(yíng)銷(xiāo)、Google、網(wǎng)站排名、動(dòng)態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)