Unity實戰篇:移植遊戲到安卓平臺的注意事項及其例項(完)(存檔,讀檔,排行榜的開發(PlayerPrefs))
阿新 • • 發佈:2018-12-13
排行榜的開發利用PlayerPrefs來實現資料持久化,對PlayerPrefs不瞭解的同學先去看一下我這個部落格
先建立好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()函式就可以了。下次進入遊戲就依舊可以看到之前的排行榜。