Unity排行榜实现

小菜鸟 2021-11-15 PM 1653℃ 0条

实现代码如下:

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



public class Ranklist : MonoBehaviour
{  
  
    /// <summary>
    /// 用户信息模板预制体挂载到的对象
    /// </summary>
    public GameObject ranklist;
    /// <summary>
    /// 控制排行榜显示的对象
    /// </summary>
    public GameObject rank_show;
    /// <summary>
    /// 用户信息模板预制体
    /// </summary>
    public GameObject Line;
    /// <summary>
    /// 保存的名称
    /// </summary>
    public Text Name;
    /// <summary>
    /// 保存的分数
    /// </summary>
    public Text Socre;
    /// <summary>
    /// 玩家排行信息
    /// </summary>
    /// <typeparam name="PlayerNode">信息模板</typeparam>
    /// <returns></returns>
    public static List<PlayerNode> PlayerRankData = new List<PlayerNode>();
    /// <summary>
    /// 是否为第一次录入信息
    /// </summary>
    static bool isFirst = true;

    private void Start()
    {
        //读取json文件中的内容
        string str = File.ReadAllText(Application.dataPath + "/Json/json.json");
        if (!String.IsNullOrEmpty(str))
        {
            PlayerRankData.Clear();//清空列表中的原本数据
            List<PlayerNode> PlayerRankDataNew = JsonMapper.ToObject<List<PlayerNode>>(str);//获取json中的数据
            foreach (PlayerNode playerdata in PlayerRankDataNew)//循环读入排行榜
            {
                UpdateRankData_json(playerdata);
            }
        }
    }
    /// <summary>
    /// 显示排名
    /// </summary>
    public void RankLoad(){      
        rank_show.SetActive(true);
        this.gameObject.SetActive(false);
    }
    // public void UpdateNullRank(){
    //     PlayerNode playerdata = new PlayerNode(null, int.Parse(Socre.text), UnityEngine.Random.Range(20, 100));
        
    //     CreateNewLine(playerdata);
    //     Update_Sort(playerdata);//更新排序
    // }
    /// <summary>
    /// 读入json中的数据到排行榜
    /// </summary>
    /// <param name="playerdata"></param>
    public void UpdateRankData_json(PlayerNode playerdata)
    {
        CreateNewLine(playerdata);      
        Update_Sort(playerdata);//更新排序
    }
    /// <summary>
    /// 插入排名数据
    /// </summary>
    /// <param name="playerdata">插入的数据</param>
    public  void  UpdateRankData()
    {
        if (!String.IsNullOrEmpty(Name.text))//名字为空不存储排名
        {          
            PlayerNode playerdata = new PlayerNode(Name.text, int.Parse(Socre.text), UnityEngine.Random.Range(20, 100));
            CreateNewLine(playerdata);
            Update_Sort(playerdata);//更新排序
            File.WriteAllText(Application.dataPath + "/Json/json.json", JsonMapper.ToJson(PlayerRankData));
        }
    }
    /// <summary>
    /// 排名排序
    /// </summary>
    /// <param name="playerdata">插入数据</param>
    public void Update_Sort(PlayerNode playerdata)
    {
       if (isFirst)//判断是否存在玩家信息 
        {
            PlayerRankData.Add(playerdata);
            isFirst = false;
        }
        else
        {
            bool minimum_rank = true;//是否为最低分数

            for (int i = 0; i < PlayerRankData.Count; i++)//排序
            {
                if (playerdata.Score > PlayerRankData[i].Score)
                {
                    PlayerRankData.Insert(i, playerdata);//指定位置插入数据
                    minimum_rank = false;//避免i为0时结束比较
                    break;
                }
            }
            if (minimum_rank)//如果最低分数,则插入数据到最后
            {
                PlayerRankData.Insert(PlayerRankData.Count, playerdata);
            }

        }
        for (int j = 0; j < PlayerRankData.Count; j++)//更新排名
        {
            PlayerRankData[j].Rank = j + 1;
        }
        updateRank(PlayerRankData);
    }
    /// <summary>
    /// 创建新信息
    /// </summary>
    /// <param name="tmp"></param>
    public void CreateNewLine(PlayerNode tmp){
        GameObject line=Instantiate(Line);
        
        line.transform.SetParent(ranklist.transform,false);
        //line.transform.localScale = new Vector3(1, 1, 1);
        line.transform.GetChild(0).GetComponent<Text>().text=tmp.Rank.ToString();
        line.transform.GetChild(1).GetComponent<Text>().text=tmp.Name;
        line.transform.GetChild(2).GetComponent<Text>().text=tmp.Score.ToString();
    }
    /// <summary>
    /// 更新列表数据
    /// </summary>
    /// <param name="players"></param>
    public void updateRank(List<PlayerNode> players){
        for (int i = 0; i < ranklist.transform.childCount; i++)
        {
            Transform l = ranklist.transform.GetChild(i);
            l.GetChild(0).GetComponent<Text>().text = players[i].Rank.ToString();
            l.GetChild(1).GetComponent<Text>().text = players[i].Name;
            l.GetChild(2).GetComponent<Text>().text = players[i].Score.ToString();
        }
    }
    
}

参考借鉴于:https://blog.csdn.net/derbi123123/article/details/107932519

标签: none

非特殊说明,本博所有文章均为博主原创。

评论啦~