【问题标题】:High Score Unity3D using PlayerPrefs使用 PlayerPrefs 高分 Unity3D
【发布时间】:2015-05-06 12:17:06
【问题描述】:

我一直在尝试使用 PlayerPrefs 制作最佳时间高分系统,我查看了很多资源,但不确定我做错了什么。 Bellow 是我的脚本,其中包含我最好的时间高分尝试,我已经评论了我试图用我的脚本做的所有事情以及需要做的事情或我认为可能不起作用的事情。

我不确定的所有区域都标记了 TODO:不确定我是否正确执行了此操作。

对于方法:

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

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

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

    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);
        }
    }
    

【问题讨论】:

  • 问题是什么?
  • 您似乎没有在PlayerWin() 中设置bestTime,它总是为零。除此之外,它似乎很好。
  • @Laykker 我在问如何使用 PlayerPrefs 来确定我当前是否有最佳时间,存储最佳时间并将我的游戏时间与最佳时间(高分)进行比较,如果我的游戏时间是最好的时间更新最好的时间是我的游戏时间
  • @Catwood 是的,它总是 0。如何设置最佳时间?

标签: c# unity3d


【解决方案1】:
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);
    }

在您检查实际时间是否为高分之后,但在显示它之前设置您最好的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多