UGUI动态滑动列表

目录结构


Image  增加控件  ScrollRect


Content 增加VerticalLayoutGroup


预制体





详细代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class initMessage : MonoBehaviour {
    List message = new List();
    public GameObject item;
    public Button cancel;
    GameObject myMessage;
    GameObject parent;
    Vector3 itemLocalPos;
    Vector2 contentSize;
    float itemHeight;
// Use this for initialization
void Start () {
        parent = GameObject.Find("Content");
        contentSize = parent.GetComponent().sizeDelta;
        item = Resources.Load("Prefabs/List_1") as GameObject;
        itemHeight = item.GetComponent().rect.height;
        itemLocalPos = item.transform.localPosition;


        for (int i = 0; i < 8; i++)
        {
            AddItem(i);
        }
    }

// Update is called once per frame
void Update () {

}


    public void AddItem(int index)
    {
        GameObject a = Instantiate(item) as GameObject;
        a.GetComponent().SetParent(parent.GetComponent(), false);
        a.transform.localPosition = new Vector3(itemLocalPos.x, itemLocalPos.y - message.Count * itemHeight,0);
        message.Add(a);
        parent.transform.GetChild(index).transform.Find("jujue").GetComponent

你可能感兴趣的:(UGUI)