Unity坦克大战开发全流程——开始场景——排行榜数据逻辑

开始场景——排行榜数据逻辑

Unity坦克大战开发全流程——开始场景——排行榜数据逻辑_第1张图片

排行榜单条数据

Unity坦克大战开发全流程——开始场景——排行榜数据逻辑_第2张图片

排行榜列表

Unity坦克大战开发全流程——开始场景——排行榜数据逻辑_第3张图片

然后在数据管理类中声明一个对应的字段

初始化数据

然后再在上一节课所编写的UpdatePanelInfo函数中处理数据更新的逻辑

Unity坦克大战开发全流程——开始场景——排行榜数据逻辑_第4张图片

时间换算算法

Unity坦克大战开发全流程——开始场景——排行榜数据逻辑_第5张图片

然后再在数据管理类中编写一个在排行榜中添加数据的方法以提供给外部

Unity坦克大战开发全流程——开始场景——排行榜数据逻辑_第6张图片

 直到当前RankInfo、GameDataMgr、RankPanel的代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 单条排行榜信息
/// 
public class RankInfo 
{
    public string name;
    public int score;
    public int time;

    public RankInfo(string name, int score, int time)
    {
        this.name = name;
        this.score = score;
        this.time = time;
    }

    public RankInfo()
    {

    }
}

/// 
/// 排行榜列表
/// 
public class RankList
{
    public List list;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameDataMgr 
{
    private static GameDataMgr instance = new GameDataMgr();
    public static GameDataMgr Instance
    {
        get { return instance; }
    }

    public MusicData musicData;
    public RankList rankData;

    private GameDataMgr()
    {
        //初始化数据
        musicData = PlayerPrefsDataMgr.Instance.LoadData(typeof(MusicData), "Music") as MusicData;
        rankData = PlayerPrefsDataMgr.Instance.LoadData(typeof(RankList), "Music") as RankList;

        //判断是否是第一次进入游戏,如果是第一次,那么就通过代码来手动初始化数据
        if (!musicData.notFirst)
        {
            musicData.notFirst = true;
            musicData.isOpenBK = true;
            musicData.isOpenSound = true;
            musicData.bkValue = 1;
            musicData.soundValue = 1;

            //储存数据
            PlayerPrefsDataMgr.Instance.SaveData(musicData, "Music");
        }
    }

    //以下是提供给外部用于注册事件的函数

    //开关背景音乐
    public void OpenOrCloseBKMisic(bool isOpen)
    {
        musicData.isOpenBK = isOpen;
        BKMusic.Instance.ChangeOpen(isOpen);
        PlayerPrefsDataMgr.Instance.SaveData(musicData, "Music");        
    }

    //开关音效
    public void OpenOrCloseSound(bool isOpen)
    {
        musicData.isOpenSound = isOpen;
        PlayerPrefsDataMgr.Instance.SaveData(musicData, "Music");
    }

    //改变背景音乐的大小
    public void ChangeBKValue(float value)
    {
        musicData.bkValue = value;
        BKMusic.Instance.ChangeValue(value);
        PlayerPrefsDataMgr.Instance.SaveData(musicData, "Music");
    }

    //改变背景音效的大小
    public void ChangeSoundValue(float value)
    {
        musicData.soundValue = value;
        PlayerPrefsDataMgr.Instance.SaveData(musicData, "Music");
    }

    //添加排行榜信息
    public void AddRankInfo(string name, int score, int time)
    {
        rankData.list.Add(new RankInfo(name, score, time));
        rankData.list.Sort((a, b) => a.time < b.time ? -1 : 1);

        for (int i = rankData.list.Count - 1; i >= 3; i--)
        {
            rankData.list.RemoveAt(i);
        }

        PlayerPrefsDataMgr.Instance.SaveData(rankData, "Rank");
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SocialPlatforms.Impl;

public class RankPanel : BasePanel
{
    public CustomGUIButton btnClose;

    //利用列表来储存文本控件对应的脚本信息
    private List labPM = new List();
    private List labPlayer = new List();
    private List labScore = new List();
    private List labTime = new List();

    
    void Start()
    {
        HideMe();

        btnClose.clickEvent += () =>
        {
            HideMe();
            BeginPanle.Instance.ShowMe();
        };


        //为列表填充信息
        for (int i = 1; i <= 3; i++)
        {
            labPM.Add(transform.Find("PM/labPM" + i).GetComponent());
            labPlayer.Add(transform.Find("Player/labPlayer" + i).GetComponent());
            labScore.Add(transform.Find("Score/labScore" + i).GetComponent());
            labTime.Add(transform.Find("Time/labTime" + i).GetComponent());
        }



    }

    //更新面板的信息
    public void UpdatePanelInfo()
    {
        List list = GameDataMgr.Instance.rankData.list;

        for (int i = 0; i < list.Count; i++)
        {
            labPlayer[i].content.text = list[i].name;
            labScore[i].content.text = list[i].score.ToString();
            labTime[i].content.text = list[i].time.ToString();
        }
       
    }

    //重写虚函数
    public override void ShowMe()
    {
        base.ShowMe();
        UpdatePanelInfo();
    }

}

 

 

你可能感兴趣的:(unity,游戏引擎)