我一直在尝试使用PlayerPrefs创造一个最好的时间高分系统,我已经看了很多资源,但我不确定我做错了什么。贝娄是我的剧本,包含了我最好的时间高分尝试,我评论了我试图用我的剧本做什么以及需要做什么或者我认为可能不起作用。
所有我不确定的地方都标记了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);
}
}
答案 0 :(得分:2)
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);
}
在检查实际时间是否为高分之后,但在显示之前设置最佳时间。