Unity3D制作横向滚动动态自适应列表
我新建了一个测试场景,结构就是添加一个Scroll Rect的组件,然后在Content上添加了一个Horizontal Layout Group组件,item是需要在Content下动态生成的物体。
![](https://img-blog.csdnimg.cn/20200529152115301.png)
![](https://img-blog.csdnimg.cn/20200529153457704.png)
![](https://img-blog.csdnimg.cn/20200529152403721.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTAyOTQ0,size_16,color_FFFFFF,t_70)
为了方便查看,我添加了背景和文字,整体如下。
![](https://img-blog.csdnimg.cn/20200529152825376.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MTAyOTQ0,size_16,color_FFFFFF,t_70)
然后下面是代码的部分。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScrollHorizontal : MonoBehaviour
{
private GameObject item;
private Transform content;
private Vector2 originSize;
void Start()
{
content = GameObject.Find("Content").GetComponent<