繁体   English   中英

使用PlayerPrefs获得高分Unity3D

[英]High Score Unity3D using PlayerPrefs

我一直在尝试使用PlayerPrefs创建最佳时间高分系统,我查看了太多资源,但不确定自己做错了什么。 贝娄(Bellow)是我的脚本,其中包含我最好的时间高分尝试,我评论了我要使用脚本进行的所有操作以及需要完成的工作或我认为可能不起作用的事情。

我不确定的所有领域都已标记为TODO:不确定我是否做对了。

对于方法:

  • public bool hasBestTime() :我需要检查是否有最佳时间存储为高分

  • float GetBestTime() :我需要获取当前的高分(最佳时间)并返回当前的高分

  • public void PlayerWin() :我需要阅读阅读并存储高分(最佳时间)

     using UnityEngine; using System.Collections; public class GameController : MonoBehaviour { public static readonly string OUT_OF_FUEL_MESSAGE = "Out of fuel!"; public static readonly string WIN_MESSAGE = "Goal collect! You win!"; public static GameController Instance { get; private set; } private bool gameOver = false; //A flag to store when the game is over private int goalsRemaining = 0; //the number of goals that are remaining in the level private float gameTime = 0; //the time (in seconds) that the player has been playing the level void Awake() { UIController.LoadUI(); Instance = this; } void OnDestroy() { Instance = null; } void Start() { } void Update() { if (!gameOver) { gameTime += Time.deltaTime; } UIController.SetTime(gameTime); } /// <summary> /// Check if the game is over /// </summary> public bool isGameOver { get { return gameOver; } } /// <summary> /// Check if we have a best time stored as a highscore /// </summary> public bool hasBestTime { get { if (PlayerPrefs.HasKey("Best Time")) { return true; } return false; //TODO: Not sure if I am doing this correct } } /// <summary> /// Get the current highscore /// </summary> /// <returns>The current highscore</returns> float GetBestTime() { //time = PlayerPrefs.GetFloat("Best Time"); return gameTime; //TODO: Not sure if I am doing this correct } /// <summary> /// Notify the game controller that the player has won the level /// </summary> public void PlayerWin() { gameOver = true; //Update the highscore float bestTime = 0; bool isNewHighscore = false; if (gameTime < PlayerPrefs.GetFloat("Best Time")) { PlayerPrefs.SetFloat("Best Time", gameTime); } //TODO: Not sure if I have done this correct trying to complete the code to read and store the highscore //Pop up the win message UIController.GameOver(WIN_MESSAGE, gameTime, bestTime, isNewHighscore); } /// <summary> /// Notify the game controller that the player has run out of fuel and lost the level /// </summary> public void PlayerOutOfFuel() { gameOver = true; if (hasBestTime) { UIController.GameOver(OUT_OF_FUEL_MESSAGE, gameTime, GetBestTime(), false); } else { UIController.GameOver(OUT_OF_FUEL_MESSAGE); } } /// <summary> /// Notify the game controller that the player has collected a goal /// </summary> public static void PlayerCollectGoal() { Instance.goalsRemaining--; UIController.SetRemaining(Instance.goalsRemaining); if (Instance.goalsRemaining == 0) { Instance.PlayerWin(); } } /// <summary> /// Notify the game controller that a goal has been spawned in the level /// </summary> public static void SpawnGoal() { Instance.goalsRemaining++; UIController.SetRemaining(Instance.goalsRemaining); } } 
public void PlayerWin()
    {
        gameOver = true;

        //Update the highscore
        float bestTime = 0;
        bool isNewHighscore = false;

        if (gameTime < PlayerPrefs.GetFloat("Best Time"))
        {
            PlayerPrefs.SetFloat("Best Time", gameTime);
        }
        //TODO: Not sure if I have done this correct trying to complete the code to read and store the highscore

        bestTime = PlayerPrefs.GetFloat("Best Time"); // ADD THIS LINE HERE

        //Pop up the win message
        UIController.GameOver(WIN_MESSAGE, gameTime, bestTime, isNewHighscore);
    }

在检查实际时间是否为高分之后,但在显示前将其设置为bestTime。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM