LitJson makes a simple leaderboard

LitJson makes a simple leaderboard

……

……

This is a screenshot of the final effect after being packaged and run on an Android phone (I played three games, and the data of these three games are displayed in the leaderboard):

 

First analyze, what is this doing?

In fact, it is to record the time required by the player for each game, and then make a leaderboard for the player to view.

I don't talk about knowledge points, but just record my thoughts and process of making this ranking.

……

……

1; The first step is to copy the source code of LitJson into your own project.

……

……

2; The second step is to understand one thing, the json information of the leaderboard must be stored in the path of Application.persistentDataPath, which is common to both Android phones and computers, and other paths will not work.

……

……

3; The third step is to create three initial json files under Application.persistentDataPath (used to store the game time of the highest record, second place, and third place respectively). Prepare for read and write operations. And there must be one thing to pay attention to, the file can only be created once, and it cannot be created once every time the game is run, otherwise the information of the leaderboard will be lost.

 

This is a class I specially created for the leaderboard. In the Start of this class, I first use an if to judge whether the file I need to create exists under the Application.persistentDataPath path. If not, initialize it in the if Create, or not create if present (judgment must be made). In this way, when the game is running, only one initial creation of the file will be performed, and the if statement will not be entered after creation.

……

……

4; The fourth step is to make a leaderboard UI, and then obtain the Text component to display the leaderboard data.

 

As shown in the figure above, this piece of code is specially used for display. It can convert time into hours, minutes and seconds. When the player opens the leaderboard, the script is called to obtain the stored data of hours, minutes and seconds, and displayed with Text.

……

……

5; The fifth step is to update the leaderboard data. After each game, the player can get a player's playing time. Just compare the playing time with the three data on the leaderboard, and then update the leaderboard. The logic is shown in the figure below:

 

……

……

After completing the above five steps, a simple leaderboard is completed.

Here is all the code for this class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;

public class Charts_message
{
    public int order;
    public float time;
}

public class JsonIO : MonoBehaviour
{
    Charts_message c1;
    Charts_message c2;
    Charts_message c3;
    void Start()
    {
        if (!(File.Exists(Application.persistentDataPath + "order1.json")))
        {
            初始化排行榜的数据
            c1 = new Charts_message();
            c2 = new Charts_message();
            c3 = new Charts_message();
            c1.order = 1;
            c1.time = 0;
            c2.order = 2;
            c2.time = 0;
            c3.order = 3;
            c3.time = 0;
            string s1 = JsonMapper.ToJson(c1);
            string s2 = JsonMapper.ToJson(c2);
            string s3 = JsonMapper.ToJson(c3);

            File.WriteAllText(Application.persistentDataPath + "order1.json", s1);
            File.WriteAllText(Application.persistentDataPath + "order2.json", s2);
            File.WriteAllText(Application.persistentDataPath + "order3.json", s3);
        }
    }

    public static void charts_Upload(float time)
    {
        string json1=File.ReadAllText(Application.persistentDataPath + "order1.json");
        string json2=File.ReadAllText(Application.persistentDataPath + "order2.json");
        string json3=File.ReadAllText(Application.persistentDataPath + "order3.json");
        Charts_message ch1 = JsonMapper.ToObject<Charts_message>(json1);
        Charts_message ch2 = JsonMapper.ToObject<Charts_message>(json2);
        Charts_message ch3 = JsonMapper.ToObject<Charts_message>(json3);

        List<float> chartList = new List<float>();
        chartList.Add(time);
        chartList.Add(ch1.time);
        chartList.Add(ch2.time);
        chartList.Add(ch3.time);
        chartList.Sort();
        ch1.time = chartList[3];
        ch2.time = chartList[2];
        ch3.time = chartList[1];
        charts_Upload1(ch1, ch2, ch3);
    }

    private static void charts_Upload1(Charts_message ch1,Charts_message ch2,Charts_message ch3)
    {
        string s1 = JsonMapper.ToJson(ch1);
        string s2 = JsonMapper.ToJson(ch2);
        string s3 = JsonMapper.ToJson(ch3);
        File.WriteAllText(Application.persistentDataPath + "order1.json", s1);
        File.WriteAllText(Application.persistentDataPath + "order2.json", s2);
        File.WriteAllText(Application.persistentDataPath + "order3.json", s3);
    }

    //将数据展示为时分秒
    public static int[] charts_play()
    {
        string json1 = File.ReadAllText(Application.persistentDataPath + "order1.json");
        string json2 = File.ReadAllText(Application.persistentDataPath + "order2.json");
        string json3 = File.ReadAllText(Application.persistentDataPath + "order3.json");
        Charts_message ch1 = JsonMapper.ToObject<Charts_message>(json1);
        Charts_message ch2 = JsonMapper.ToObject<Charts_message>(json2);
        Charts_message ch3 = JsonMapper.ToObject<Charts_message>(json3);
        int time1 =(int) ch1.time;
        int time2 =(int) ch2.time;
        int time3 =(int) ch3.time;
        int h1, m1, s1,h2,m2,s2,h3,m3,s3;
        h1 = time1 / 3600;
        m1 = (time1-h1*3600)/60;
        s1 = time1 - h1 * 3600 - m1 * 60;
        h2 = time2 / 3600;
        m2 = (time2 - h2 * 3600) / 60;
        s2 = time2 - h2 * 3600 - m2 * 60;
        h3 = time3 / 3600;
        m3 = (time3 - h3 * 3600) / 60;
        s3 = time3 - h3 * 3600 - m3 * 60;
        int[] charts_TimeData = new int[] { h1, m1, s1, h2, m2, s2, h3, m3, s3 };
        return charts_TimeData;
    }
}

Guess you like

Origin blog.csdn.net/oyqho/article/details/129992863