Unity实战篇:移植游戏到安卓平台的注意事项及其实例(完)(存档,读档,排行榜的开发(PlayerPrefs))

版权声明:转载请注明出处!不注明也无所谓,嘿嘿。 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()函数就可以了。下次进入游戏就依旧可以看到之前的排行榜。

总结:其实看到这,聪明的小伙伴已经发觉到可以用同样的方法做存档读档,固然可以,但是,同时我们发现了局限性,这个Demo很简单,用PlayerPrefs没问题,一旦游戏复杂起来..........想想需要保存的键值对数目就令人发指,所以我推荐大家平时开发游戏,用JSON来实现存档读档。(其加密手段也很多,很安全)

猜你喜欢

转载自blog.csdn.net/qq_15020543/article/details/82944225