C# 使用PlayerPrefs获得高分Unity3D
我一直在尝试使用PlayerPrefs制作一个最佳时间高分系统,我查看了很多资源,但不确定我做错了什么。下面是我的脚本,包含我的最佳时间高分尝试,我已经评论了我试图用我的脚本做的所有事情,以及需要做的事情或我认为可能不起作用的事情 我不确定的所有区域都标记了TODO:不确定我是否做得正确 对于方法:C# 使用PlayerPrefs获得高分Unity3D,c#,unity3d,C#,Unity3d,我一直在尝试使用PlayerPrefs制作一个最佳时间高分系统,我查看了很多资源,但不确定我做错了什么。下面是我的脚本,包含我的最佳时间高分尝试,我已经评论了我试图用我的脚本做的所有事情,以及需要做的事情或我认为可能不起作用的事情 我不确定的所有区域都标记了TODO:不确定我是否做得正确 对于方法: public bool hasBestTime():我需要检查是否将最佳时间存储为高分 float GetBestTime():我需要获取当前高分(最佳时间)并返回当前高分 public void
:我需要检查是否将最佳时间存储为高分public bool hasBestTime()
:我需要获取当前高分(最佳时间)并返回当前高分float GetBestTime()
:我需要读取并存储高分(最佳时间)public void PlayerWin()
使用UnityEngine; 使用系统集合; 公共类游戏控制器:单行为 { public static readonly string OUT_OF_FUEL_MESSAGE=“OUT OF FUEL!”; 公共静态只读字符串WIN_MESSAGE=“目标收集!您赢了!”; 公共静态GameController实例{get;private set;} private bool gameOver=false;//游戏结束时要存储的标志 private int goalsRemaining=0;//该级别中剩余的目标数 private float gameTime=0;//玩家玩该关卡的时间(秒) 无效唤醒() { UIController.LoadUI(); 实例=此; } void OnDestroy() { 实例=null; } void Start() { } 无效更新() { 如果(!gameOver) { gameTime+=Time.deltaTime; } UIController.SetTime(游戏时间); } /// ///检查比赛是否结束 /// 公共场所 { 得到 { 返回游戏结束; } } /// ///检查我们是否将最佳时间存储为高分 /// 公共图书馆有最好的时间 { 得到 { 如果(PlayerPrefs.HasKey(“最佳时间”)) { 返回true; } return false;//TODO:不确定我的操作是否正确 } } /// ///获得当前最高分 /// ///当前的最高分 浮点GetBestTime() { //时间=PlayerPrefs.GetFloat(“最佳时间”); return gameTime;//TODO:不确定我做的是否正确 } /// ///通知游戏管理员玩家已赢得关卡 /// 公共空间播放机温() { gameOver=true; //更新高分 浮动时间=0; bool isNewHighscore=false; 如果(游戏时间
公共无效播放器温()
{
gameOver=true;
//更新高分
浮动时间=0;
bool isNewHighscore=false;
如果(游戏时间
在检查实际时间是否为高分后,但在显示之前,请设置最佳时间。问题是什么?您似乎没有在
PlayerWin()
中设置bestTime
,它将始终为零。除此之外,这似乎还不错。@Laykker我在问如何使用PlayerPrefs来确定当前是否存在最佳时间,存储最佳时间并将我的游戏时间与最佳时间(高分)进行比较,如果我的游戏时间更好,则最佳时间更新最佳时间成为我的游戏时间看看:@Catwood是的,总是0。我如何设定最佳时间?
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);
}