Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何节省在unity中花费的最短时间?_C#_Unity3d_Time_Save - Fatal编程技术网

C# 如何节省在unity中花费的最短时间?

C# 如何节省在unity中花费的最短时间?,c#,unity3d,time,save,C#,Unity3d,Time,Save,我正在unity 3d中制作一个迷宫游戏,完成关卡所需的时间最少,就是高分。有没有办法用Playerprefs保存高分?脚本编写是在C#中完成的。您可以首先将此检查放入唤醒函数中的homescript中: if(!PlayerPrefs.HasKey("HighestScore")) { PlayerPrefs.SetString("HighestScore", "keep some big number"); } 或者或者可以使用

我正在unity 3d中制作一个迷宫游戏,完成关卡所需的时间最少,就是高分。有没有办法用Playerprefs保存高分?脚本编写是在C#中完成的。

您可以首先将此检查放入唤醒函数中的homescript中:

   if(!PlayerPrefs.HasKey("HighestScore"))
        {
            PlayerPrefs.SetString("HighestScore", "keep some big number");
        }
或者或者可以使用(最好)

然后,当特定级别结束时,您需要保存分数 将现有PlayerPref与当前玩家分数进行比较

例如浮点数

if(PlayerPrefs.GetFloat("HighestScore")>=current_score)
{
PlayerPrefs.SetFloat("HighestScore",current_score);
}

希望这有帮助

您可以使用Get/Set评估器使其更容易

public class GameManager: MonoBehavior {
    ...
    public float HighScore {
        get {
            return PlayerPrefs.GetFloat("HIGHSCORE", 0);
        }
        set {
            if(value > HighScore) {
                PlayerPrefs.SetFloat("HIGHSCORE", value);
            }
        }
    }
    ...
    public void GameOver() {
        HighScore = currentScore;
    }
}

谢谢,我知道答案了!只需要检查第一轮是否已经开始

// Start is called before the first frame update
void Start()
{



    HighScore = score;



    WinScore.text = PlayerPrefs.GetFloat("HighScore").ToString();



}

// Update is called once per frame
void Update()
{
    if(PlayerPrefs.GetFloat("HighScore") == 0f)
    {

        PlayerPrefs.SetFloat("HighScore", (float)HighScore);
        WinScore.text = HighScore.ToString();


    }

    if (HighScore < PlayerPrefs.GetFloat("HighScore")){ 

        PlayerPrefs.SetFloat("HighScoreEasy", (float)HighScore);
        WinScore.text = HighScore.ToString();


    }

}
//在第一次帧更新之前调用Start
void Start()
{
高分=分数;
WinScore.text=PlayerPrefs.GetFloat(“HighScore”).ToString();
}
//每帧调用一次更新
无效更新()
{
如果(PlayerPrefs.GetFloat(“高分”)==0f)
{
PlayerPrefs.SetFloat(“HighScore”,(float)HighScore);
WinScore.text=HighScore.ToString();
}
如果(HighScore
因此,将您的问题简化为基本问题:或
// Start is called before the first frame update
void Start()
{



    HighScore = score;



    WinScore.text = PlayerPrefs.GetFloat("HighScore").ToString();



}

// Update is called once per frame
void Update()
{
    if(PlayerPrefs.GetFloat("HighScore") == 0f)
    {

        PlayerPrefs.SetFloat("HighScore", (float)HighScore);
        WinScore.text = HighScore.ToString();


    }

    if (HighScore < PlayerPrefs.GetFloat("HighScore")){ 

        PlayerPrefs.SetFloat("HighScoreEasy", (float)HighScore);
        WinScore.text = HighScore.ToString();


    }

}