unity无限循环列表的代码
时间: 2023-04-04 17:01:44 浏览: 156
以下是 Unity 无限循环列表的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InfiniteScroll : MonoBehaviour
{
public RectTransform prefab;
public RectTransform content;
public int itemCount = 100;
public float itemSize = 100f;
private List<RectTransform> itemInstances = new List<RectTransform>();
void Start()
{
for (int i = ; i < itemCount; i++)
{
RectTransform item = Instantiate(prefab) as RectTransform;
item.SetParent(content, false);
item.anchoredPosition = new Vector2(, -i * itemSize);
itemInstances.Add(item);
}
}
void Update()
{
float contentY = content.anchoredPosition.y;
float minY = itemInstances[].anchoredPosition.y - itemSize;
float maxY = itemInstances[itemInstances.Count - 1].anchoredPosition.y + itemSize;
if (contentY < minY)
{
RectTransform item = itemInstances[];
itemInstances.RemoveAt();
itemInstances.Add(item);
item.anchoredPosition = new Vector2(, maxY);
content.anchoredPosition += new Vector2(, itemSize);
}
else if (contentY > maxY)
{
RectTransform item = itemInstances[itemInstances.Count - 1];
itemInstances.RemoveAt(itemInstances.Count - 1);
itemInstances.Insert(, item);
item.anchoredPosition = new Vector2(, minY);
content.anchoredPosition -= new Vector2(, itemSize);
}
}
}
阅读全文