Unity3D infinite scroll to achieve dynamic loading leaderboard

foreword

    In game development, the ranking list is the most common function. Each game has a ranking list with multiple values ​​to stimulate player consumption. In game development, the ranking list of short-link games is generally downloaded from the server first. The Excel file is realized by reading, and the strong networking game also parses the Excel file in the server and displays the data to the client. Generally, a leaderboard will display about 200 pieces of data. How to display 200 pieces of data through a ScrollView, It is definitely not advisable to list 200 pieces of data in a ScrollView. How to dynamically read and display according to the number of rows in Excel.

    As can be seen from the above figure, there are only 10 self-objects under the Grid on the right, but at runtime, they are stacked downward in a loop, or displayed upward in a loop, so that hundreds of items in the leaderboard can be displayed in this way. data

wheel quote

    This function is mainly composed of two parts, namely: infinite scrolling technology and parsing Excel configuration files. I would like to thank Yusong MOMO here for quoting his parsing Excel technology. The article links are here: Parsing Excel , and Jane The infinite scrolling technology of a big man in the book , the two articles are very good in the description and analysis of the technology, thanks to the wheels made by the two big men.

accomplish

With parsing Excel and infinite scroll technology implementation is simple. Only such a script is needed to implement the leaderboard function.


using UnityEngine;
using Excel;
using System.IO;
using UnityEngine.UI;
using System.Data;
public class RankingList : MonoBehaviour {

    InfinityGridLayoutGroup infinityGridLayoutGroup;

    private int amount = 20;

	// Use this for initialization
	void Start () {
        infinityGridLayoutGroup = transform.Find("Panel_Test/Panel_Grid").GetComponent<InfinityGridLayoutGroup>();
        
        infinityGridLayoutGroup.updateChildrenCallback = UpdateChildrenCallback;
        infinityGridLayoutGroup.SetAmount(amount);

    }
	
    void UpdateChildrenCallback(int index,Transform trans)
    {
        Text text = trans.Find("Text").GetComponent<Text>();

        FileStream stream = File.Open(Application.dataPath + "/UserLevel.xlsx", FileMode.Open, FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
        DataSet result = excelReader.AsDataSet();

        int columns = result.Tables[0].Columns.Count;
        int rows = result.Tables[0].Rows.Count;
        amount = rows;
        Debug.Log(amount);
        string tmpwrite = "";

        for (int i = 0; i < columns; i++)
        {
            string level = result.Tables[0].Rows[index][i].ToString();
            tmpwrite += level;
        }
        text.text = tmpwrite;
        text.fontSize = 20;
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324696556&siteId=291194637