今年刚开始写文章,也没有注意格式什么的。但是我会确保代码是可用的。
本文主要描述,使用UGUI,当ScrollView里的ItemPrefab大小都一致时,如何实现单行单列的循环列表(仅有展示功能)。具体的数据类未赋值,仅在item上显示了他的index。
以下为加在ScrollRect上的脚本,对于Horizontal没有处理,请自行添加。对于space的处理也未完善,通过加入space 的计算可以减少实际item的个数。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[AddComponentMenu("Extensions/ScrollRectRepeat")]
[RequireComponent(typeof(ScrollRect))]
public class ScrollViewRepeat : MonoBehaviour
{
public string PrefabPath = "Prefab/Item";
public ScrollType scrollType = ScrollType.Vertical;
public RectTransform Content;
public int Count = 20;
public float space = 2;
private GameObject prefab;
private List<ScrollViewItem> itemList = new List<ScrollViewItem>();
// Use this for initialization
void Start ()
{
prefab = Resources.Load<GameObject>(PrefabPath);
GetItemWidthOrHeight();
SetContentSize();
InitItems(GetItemsCount());
Register();
}
public float GetItemWidthOrHeight()
{
Vector2 sizePrefab = prefab.GetComponent<RectTransform>().sizeDelta;
if (scrollType == ScrollType.Vertical)
{
return sizePrefab.y;
}
retu