Unity2D教程:生成滚动列表

关注专栏,持续更新哦

教程总目录


结构如下:
Unity2D教程:生成滚动列表_第1张图片
ListPanel挂载ListUI脚本,其中Item对象为需要生成的预置物。
Unity2D教程:生成滚动列表_第2张图片

除了Item之外还需要找到Content(设置为生成项的父亲)和Scrollbar(由于生成后会滚到中间去,设置scrollbar.value = 1滚回顶部)
Unity2D教程:生成滚动列表_第3张图片
ScrollPanel组件及其设置:
Unity2D教程:生成滚动列表_第4张图片

ContentPanel组件及其设置:
Unity2D教程:生成滚动列表_第5张图片
ScrollBar组件设置:
Unity2D教程:生成滚动列表_第6张图片

代码:

/* 
 *  Author : Jk_Chen
 */

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class ListUI : MonoBehaviour
{
    public GameObject item;
    GameObject scroll;
    Transform content;
    Scrollbar scrollbar;

    // 第一次先加载,然后再SetActive=false
    void Start()
    {
        scroll = transform.Find("ScrollPanel").gameObject;
        content = transform.Find("ScrollPanel/ContentPanel");
        scrollbar = transform.Find("ScrollPanel/Scrollbar").gameObject.GetComponent<Scrollbar>();
        ShowItems();
        transform.Find("PhoneBG/BackButton").gameObject.GetComponent<Button>().onClick.AddListener(Back);
    }

    bool loadItemDone = false;
    float loadItemTimer = 0;
    private void Update()
    {
        if (!loadItemDone)
        {
            loadItemTimer += Time.deltaTime;
            if (loadItemTimer > 0.1)
            {
                scrollbar.value = 1;
                loadItemDone = true;
            }
        }

    }

    void ShowItems()
    {
        Sprite[] L = {
            Resources.Load<Sprite>("Image/Face/Smile"),
            Resources.Load<Sprite>("Image/Face/PokerFace"),
            Resources.Load<Sprite>("Image/Face/Stunned"),
            Resources.Load<Sprite>("Image/Face/Angry"),
            Resources.Load<Sprite>("Image/Face/Cry"),
        };

        SQL S = SQL.instance;
        S.OpenDataBase("Music");
        using(var dr = S.ExecuteReader("Select * from Music order by difficulty"))
        {
            while (dr.Read())
            {
                GameObject obj = Instantiate(item, transform.position, transform.rotation);
                string music = dr["Name"].ToString();
                string musician = dr["Musician"].ToString();
                string type = dr["Type"].ToString();
                float best = float.Parse(dr["Best"].ToString());

                obj.transform.Find("PlayButton").gameObject.GetComponent<Image>().sprite =
                    L[int.Parse(dr["Difficulty"].ToString())];
                obj.transform.Find("Music").gameObject.GetComponent<Text>().text = music;
                obj.transform.Find("Musician").gameObject.GetComponent<Text>().text = musician;
                obj.transform.Find("Best").gameObject.GetComponent<Text>().text = (best * 100).ToString("F1") + "%";
                obj.transform.Find("PlayButton").gameObject.GetComponent<Button>().onClick.
                    AddListener(delegate
                    {
                        string musicFile = music + " - " + musician + "." + type;
                        string textFile = music + " - " + musician + ".txt";
                        ToGameWithFile(musicFile, textFile);
                    });
                obj.transform.SetParent(content);
            }
        }
        S.CloseDataBase();
    }

    void Back()
    {
        gameObject.SetActive(false);
    }

    void ToGameWithFile(string musicFile, string textFile)
    {
        Config.instance.sceneToLoad = "Main";
        PlayerPrefs.SetString("MusicFileName", musicFile);
        PlayerPrefs.SetString("TextFileName", textFile);
        SceneManager.LoadScene("Loading");
    }
}


你可能感兴趣的:(Unity2D教程)