本文实例为大家分享了unity实现无限列表功能的具体代码,供大家参考,具体内容如下
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">世界坐标的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; } }
以上拓展方法是判断两个Recttransform类型的物体是否相交。
然后ScrollRec的滑动回调方法中更新UI位置,代码如下
private void OnScrollRectValueChanged(Vector2 arg0) { Dictionary<int, DynamicRect> inOverlaps = new Dictionary<int, DynamicRect>(); mRectMask = 遮罩物体的RectTransform.WorldRect(); //m_DynamicRectDic这个字典保存的是你所有UI需要放置的位置数据, //判断所有UI哪个是可见哪个不可见 ,保存起来 foreach (DynamicRect dR in m_DynamicRectDic.Values) { tmpTra.localPosition = dR.localPos; //获取每个位置UI的世界坐标Rect Rect rect = m_LevelItemPrefabRT.WorldRect2(tmpTra.position); if (rect.Overlaps(mRectMask)) { inOverlaps.Add(dR.Index, dR); } } //m_LevelItemList是保存你实例化后的UI列表,比如你这个遮罩页面最多显示3个UI,你需要实例化4个UI,然后动态修改gameobject的显示与隐藏 int len = m_LevelItemList.Count; for (int i = 0; i < len; ++i) { //LevelItem是UI上挂载的脚本,用于更新UI界面的显示和数据存储的 LevelItem item = m_LevelItemList[i]; if (item.DRect != null && !inOverlaps.ContainsKey(item.DRect.Index)) { //item的DRect为null时,隐藏物体,否则显示物体 item.DRect = null; } } //判断哪些可以重复利用的UI,然后赋予新的数据与位置 foreach (DynamicRect dR in inOverlaps.Values) { if (GetDynmicItem(dR) == null) { LevelItem item = GetNullDynmicItem(); if (item == null) continue; item.DRect = dR; //更新UI的位置和显示(自己计算,每种显示不一样) _UpdateChildTransformPos(item.gameObject, dR.Index); } } } /// <summary> /// 通过动态格子获得动态渲染器 /// </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> /// 本地坐标 /// </summary> public Vector3 localPos; /// <summary> /// 格子索引 /// </summary> public int Index; public DynamicRect(int index, Vector3 localPos) { this.Index = index; this.localPos = localPos; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。
- 本文固定链接: https://zxbcw.cn/post/184888/
- 转载请注明:必须在正文中标注并保留原文链接
- QQ群: PHP高手阵营官方总群(344148542)
- QQ群: Yii2.0开发(304864863)