版权声明:转载请注明出处!不注明也无所谓,嘿嘿。 https://blog.csdn.net/qq_15020543/article/details/82944225
排行榜的开发利用PlayerPrefs来实现数据持久化,对PlayerPrefs不了解的同学先去看一下我这个博客
https://blog.csdn.net/qq_15020543/article/details/82228221
先创建好UI
先在GameController里面初始化键值。我们只有五个榜位,为什么要创建6个呢,因为我们要保存新的得分和等级,用来和已经保存的相比较,用冒泡排序来实现排序。
再保存数据,并且进行排序
排行榜代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
public class RankingPanel : BasePanel {
private Text PanelName;
private Button onest;
private Button twost;
private Button threest;
private Button fourest;
private Button fiveest;
private Button backToMenu;
public override void OnEnter()
{
SetValue();
EnterAnim();
}
public override void OnExit()
{
ExitAnim();
}
// Use this for initialization
void Awake () {
PanelName = transform.Find("Ranking").GetComponent<Text>();
onest = transform.Find("1st").GetComponent<Button>();
twost = transform.Find("2st").GetComponent<Button>();
threest = transform.Find("3st").GetComponent<Button>();
fourest = transform.Find("4st").GetComponent<Button>();
fiveest = transform.Find("5st").GetComponent<Button>();
backToMenu = transform.Find("BackToMenu").GetComponent<Button>();
backToMenu.onClick.AddListener(BackToMenu);
PanelName.transform.localPosition = new Vector3(5, 1500, 0);
onest.transform.localPosition = new Vector3(5,-1500,0);
twost.transform.localPosition = new Vector3(5, -1500, 0);
threest.transform.localPosition = new Vector3(5, -1500, 0);
fourest.transform.localPosition = new Vector3(5, -1500, 0);
fiveest.transform.localPosition = new Vector3(5, -1500, 0);
backToMenu.transform.localPosition = new Vector3(5, -1500, 0);
InActiveUI();
}
private void SetValue()
{
onest.transform.Find("Text").GetComponent<Text>().text = " 1ST " + "Lv. " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top1L"))
+ " Score " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top1S"));
twost.transform.Find("Text").GetComponent<Text>().text = " 2ST " + "Lv. " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top2L"))
+ " Score " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top2S"));
threest.transform.Find("Text").GetComponent<Text>().text = " 3ST " + "Lv. " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top3L"))
+ " Score " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top3S"));
fourest.transform.Find("Text").GetComponent<Text>().text = " 4ST " + "Lv. " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top4L"))
+ " Score " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top4S"));
fiveest.transform.Find("Text").GetComponent<Text>().text = " 5ST " + "Lv. " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top5L"))
+ " Score " + PlayerPrefs.GetInt(PlayerPrefs.GetString("Top5S"));
}
private void EnterAnim()
{
ActiveUI();
PanelName.transform.DOLocalMoveY(567,0.07f);
onest.transform.DOLocalMoveY(328,0.1f);
twost.transform.DOLocalMoveY(164, 0.13f);
threest.transform.DOLocalMoveY(0, 0.15f);
fourest.transform.DOLocalMoveY(-166, 0.17f);
fiveest.transform.DOLocalMoveY(-332, 0.19f);
backToMenu.transform.DOLocalMoveY(-633, 0.2f);
}
private void ExitAnim()
{
PanelName.transform.DOLocalMoveY(1500, 0.07f);
onest.transform.DOLocalMoveY(-1500, 0.2f);
twost.transform.DOLocalMoveY(-1500, 0.19f);
threest.transform.DOLocalMoveY(-1500, 0.17f);
fourest.transform.DOLocalMoveY(-1500, 0.15f);
fiveest.transform.DOLocalMoveY(-1500, 0.13f);
backToMenu.transform.DOLocalMoveY(-1500, 0.1f).OnComplete(()=>InActiveUI());
}
private void ActiveUI()
{
PanelName.gameObject.SetActive(true);
onest.gameObject.SetActive(true);
twost.gameObject.SetActive(true);
threest.gameObject.SetActive(true);
fourest.gameObject.SetActive(true);
fiveest.gameObject.SetActive(true);
backToMenu.gameObject.SetActive(true);
}
private void InActiveUI()
{
PanelName.gameObject.SetActive(false);
onest.gameObject.SetActive(false);
twost.gameObject.SetActive(false);
threest.gameObject.SetActive(false);
fourest.gameObject.SetActive(false);
fiveest.gameObject.SetActive(false);
backToMenu.gameObject.SetActive(false);
}
private void BackToMenu()
{
UIManager.Instance.PopPanel();
UIManager.Instance.PushPanel(UIPanelType.GameStart);
}
}
然后我们只要在需要保存游戏的地方调用SaveScore()函数就可以了。下次进入游戏就依旧可以看到之前的排行榜。