使用scroll view实现单行滑动列表。
只需要横向滑动,取消Scroll Rect的Vertical,并取消掉Scrollbar。
为Content添加两个组件
Grid Layout Group设置子节点布局
Content Size Fitter自适应Content大小
Padding:子节点与边界的距离。
Cell Size:子节点大小
Spacing:子节点间隔
Start Corner:从哪边开始
Start Axis:行优先还是列优先
Child Alignment:子节点对齐目标
Constraint:是否限制行列数量
Constraint Count:限制的数量(最大数量)
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class Test:MonoBehaviour
{
Transform content;
//Dictionary<int, Dictionary<string,string>> data;
protected override void OnStart()
{
base.OnStart();
content = transform.Find("Scroll View/Viewport/Content");
//data = GameApp.ConfigManager.GetConfigData("Card").GetLines();
InitScrollView();
}
private void InitScrollView()
{
//int size = data.Count;
//Debug.Log("size "+size);
int size = 10;
for(int i=0;i<size;i++)
{
//int id = 1001+i;
//生成cell并将其设置为content的子对象;
GameObject cell = GameObject.Instantiate(Resources.Load($"View/CardDesView"),content) as GameObject;
//为对象添加脚本
CardDesView view = cell.AddComponent(Type.GetType("CardDesView")) as CardDesView;
//view.SetData(data[id]);
}
}
}