Unity UGUI —— 无限循环List(转载)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEngine.UI; /// <summary> /// 无限循环List /// 作者:EdisonLee /// public class UILoop1 : UIBase { enum Direction { Horizontal, Vertical } [SerializeField] private RectTransform m_Cell; [SerializeField] private Vector2 m_Page; [SerializeField] Direction direction = Direction.Horizontal; [SerializeField,Range(4,10)] private int m_BufferNo; private List<RectTransform> m_InstantiateItems = new List<RectTransform>(); private IList m_Datas; public Vector2 CellRect { get { return m_Cell != null ? m_Cell.sizeDelta : new Vector2(100, 100); } } public float CellScale { get { return direction == Direction.Horizontal ? CellRect.x : CellRect.y; } } private float m_PrevPos = 0; public float DirectionPos { get { return direction == Direction.Horizontal ? m_Rect.anchoredPosition.x : m_Rect.anchoredPosition.y; } } private int m_CurrentIndex; //页面的第一行(列)在整个conten中的位置 private Vector2 m_InstantiateSize = Vector2.zero; public Vector2 InstantiateSize { get { if (m_InstantiateSize == Vector2.zero) { float rows, cols; if (direction == Direction.Horizontal) { rows = m_Page.x; cols = m_Page.y + ( float )m_BufferNo; } else { rows = m_Page.x + ( float )m_BufferNo; cols = m_Page.y; } m_InstantiateSize = new Vector2(rows, cols); } return m_InstantiateSize; } } public int PageCount { get { return ( int )m_Page.x * ( int )m_Page.y; } } public int PageScale { get { return direction == Direction.Horizontal ? ( int )m_Page.x : ( int )m_Page.y; } } private ScrollRect m_ScrollRect; private RectTransform m_Rect; public int InstantiateCount { get { return ( int )InstantiateSize.x * ( int )InstantiateSize.y; } } protected override void Awake() { m_ScrollRect = GetComponentInParent<ScrollRect>(); m_ScrollRect.horizontal = direction == Direction.Horizontal; m_ScrollRect.vertical = direction == Direction.Vertical; m_Rect = GetComponent<RectTransform>(); m_Cell.gameObject.SetActive( false ); } public override void Data( object data) { m_Datas = data as IList; if (m_Datas.Count > PageCount) { setBound(getRectByNum(m_Datas.Count)); } else { setBound(m_Page); } if (m_Datas.Count > InstantiateCount) { while (m_InstantiateItems.Count < InstantiateCount) { createItem(m_InstantiateItems.Count); } } else { while (m_InstantiateItems.Count > m_Datas.Count) { removeItem(m_InstantiateItems.Count - 1); } while (m_InstantiateItems.Count < m_Datas.Count) { createItem(m_InstantiateItems.Count); } } } private void createItem( int index) { RectTransform item = GameObject.Instantiate(m_Cell); item.SetParent(transform, false ); item.anchorMax = Vector2.up; item.anchorMin = Vector2.up; item.pivot = Vector2.up; item.name = "item" + index; item.anchoredPosition = direction == Direction.Horizontal ? new Vector2(Mathf.Floor(index / InstantiateSize.x) * CellRect.x, -(index % InstantiateSize.x) * CellRect.y) : new Vector2((index % InstantiateSize.y) * CellRect.x, -Mathf.Floor(index / InstantiateSize.y) * CellRect.y); m_InstantiateItems.Add(item); item.gameObject.SetActive( true ); updateItem(index, item.gameObject); } private void removeItem( int index) { RectTransform item = m_InstantiateItems[index]; m_InstantiateItems.Remove(item); RectTransform.Destroy(item.gameObject); } /// <summary> /// 由格子数量获取多少行多少列 /// </summary> /// <param name="num"></param>格子个数 /// <returns></returns> private Vector2 getRectByNum( int num) { return direction == Direction.Horizontal ? new Vector2(m_Page.x, Mathf.CeilToInt(num / m_Page.x)) : new Vector2(Mathf.CeilToInt(num / m_Page.y), m_Page.y); } /// <summary> /// 设置content的大小 /// </summary> /// <param name="rows"></param>行数 /// <param name="cols"></param>列数 private void setBound(Vector2 bound) { m_Rect.sizeDelta = new Vector2(bound.y * CellRect.x, bound.x * CellRect.y); } public float MaxPrevPos { get { float result; Vector2 max = getRectByNum(m_Datas.Count); if (direction == Direction.Horizontal) { result = max.y - m_Page.y; } else { result = max.x - m_Page.x; } return result * CellScale; } } public float scale { get { return direction == Direction.Horizontal ? 1f : -1f; } } void Update() { while (scale * DirectionPos - m_PrevPos < -CellScale * 2) { if (m_PrevPos <= -MaxPrevPos) return ; m_PrevPos -= CellScale; List<RectTransform> range = m_InstantiateItems.GetRange(0, PageScale); m_InstantiateItems.RemoveRange(0, PageScale); m_InstantiateItems.AddRange(range); for ( int i = 0; i < range.Count; i++) { moveItemToIndex(m_CurrentIndex * PageScale + m_InstantiateItems.Count + i, range[i]); } m_CurrentIndex++; } while (scale * DirectionPos - m_PrevPos > -CellScale) { if (Mathf.RoundToInt(m_PrevPos) >= 0) return ; m_PrevPos += CellScale; m_CurrentIndex--; if (m_CurrentIndex < 0) return ; List<RectTransform> range = m_InstantiateItems.GetRange(m_InstantiateItems.Count - PageScale, PageScale); m_InstantiateItems.RemoveRange(m_InstantiateItems.Count - PageScale, PageScale); m_InstantiateItems.InsertRange(0, range); for ( int i = 0; i < range.Count; i++) { moveItemToIndex(m_CurrentIndex * PageScale + i, range[i]); } } } private void moveItemToIndex( int index, RectTransform item) { item.anchoredPosition = getPosByIndex(index); updateItem(index, item.gameObject); } private Vector2 getPosByIndex( int index) { float x, y; if (direction == Direction.Horizontal) { x = index % m_Page.x; y = Mathf.FloorToInt(index / m_Page.x); } else { x = Mathf.FloorToInt(index / m_Page.y); y = index % m_Page.y; } return new Vector2(y * CellRect.x, -x * CellRect.y); } private void updateItem( int index, GameObject item) { item.SetActive(index < m_Datas.Count); if (item.activeSelf) { UILoopItem lit = item.GetComponent<UILoopItem>(); lit.UpdateItem(index, item); lit.Data(m_Datas[index]); } } } |
https://www.zhihu.com/question/23895384
本文固定链接:http://www.cnblogs.com/fly-100/p/4549354.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南