开始场景——排行榜数据逻辑
排行榜单条数据
排行榜列表
然后在数据管理类中声明一个对应的字段
初始化数据
然后再在上一节课所编写的UpdatePanelInfo函数中处理数据更新的逻辑
时间换算算法
然后再在数据管理类中编写一个在排行榜中添加数据的方法以提供给外部
直到当前RankInfo、GameDataMgr、RankPanel的代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 单条排行榜信息
/// </summary>
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()
{
}
}
/// <summary>
/// 排行榜列表
/// </summary>
public class RankList
{
public List<RankInfo> 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<RankPanel>
{
public CustomGUIButton btnClose;
//利用列表来储存文本控件对应的脚本信息
private List<CustomGUILabel> labPM = new List<CustomGUILabel>();
private List<CustomGUILabel> labPlayer = new List<CustomGUILabel>();
private List<CustomGUILabel> labScore = new List<CustomGUILabel>();
private List<CustomGUILabel> labTime = new List<CustomGUILabel>();
void Start()
{
HideMe();
btnClose.clickEvent += () =>
{
HideMe();
BeginPanle.Instance.ShowMe();
};
//为列表填充信息
for (int i = 1; i <= 3; i++)
{
labPM.Add(transform.Find("PM/labPM" + i).GetComponent<CustomGUILabel>());
labPlayer.Add(transform.Find("Player/labPlayer" + i).GetComponent<CustomGUILabel>());
labScore.Add(transform.Find("Score/labScore" + i).GetComponent<CustomGUILabel>());
labTime.Add(transform.Find("Time/labTime" + i).GetComponent<CustomGUILabel>());
}
}
//更新面板的信息
public void UpdatePanelInfo()
{
List<RankInfo> 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();
}
}