【问题标题】:PlayerPrefs not saving high score.PlayerPrefs 不保存高分。
【发布时间】:2017-05-24 01:34:07
【问题描述】:

我有一个没完没了的游戏,分数根据 Time.fixedDeltaTime 增加。如果它高于获得的分数,我会尝试在玩家偏好中存储高分,但它似乎并没有坚持下去。有人可以指出我在这里做错了什么吗?我使用 HUDScript 将分数存储在 PlayerPrefs 中,并使用 GameOverScript 在游戏结束场景中呈现分数和高分。

        using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HUDScript : MonoBehaviour {

    float playerScore = 0;
    public Text scoreText;

    float highScore;
    public Text highScoreText;

    void Start () {
        highScore = PlayerPrefs.GetFloat ("Highscore", 0);
    }


    void Update () {
        playerScore += Time.fixedDeltaTime;
        scoreText.text = "Score: " + Mathf.Round((playerScore * 100)).ToString();
        if (playerScore > highScore) {
            highScore = playerScore;
            PlayerPrefs.SetFloat ("Highscore", Mathf.Round (highScore * 100));
            PlayerPrefs.Save ();
        }

    }

    public void IncreaseScore(int amount){
        playerScore += amount;
    }

    void OnDisable() {
        PlayerPrefs.SetFloat ("Score", Mathf.Round((playerScore * 100)));
    }
}

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameOverScript : MonoBehaviour {

    HUDScript hud;
    float score = 0;
    float highScore;
    public Text scoreText;
    public Text highScoreText;


    // Use this for initialization
    void Start () {
        score = PlayerPrefs.GetFloat ("Score");
        scoreText.text = "Score: " + score.ToString ();
        highScore = PlayerPrefs.GetFloat ("Highscore");
        highScoreText.text = "High Score: " + highScore.ToString ();
        PlayerPrefs.Save ();
    }

}

【问题讨论】:

  • 只是一个建议,应该删除HUDScript.Update中的保存代码。 HUDScript 什么时候会被禁用?
  • 到底是什么问题?是不是因为 GameOver 的高分是上一场比赛的高分,而不是最近的?我想帮忙,但您的问题不清楚。
  • 对不起,我的问题并不完全清楚。我的确切问题是当游戏重新启动时存储在 PlayerPrefs 中的高分不粘。我正在尝试显示本地持续的高分。现在如果你重新开始游戏,每次玩的分数和高分都是一样的。

标签: c# unity3d


【解决方案1】:

我能够通过将高分检查逻辑移至 GameOver 脚本来解决我的问题。我是 Unity 和 C# 的新手,所以直到我喝了第三杯咖啡之后,这对我来说并不明显。感谢和抱歉模棱两可。

   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HUDScript : MonoBehaviour {

    float playerScore = 0;
    public Text scoreText;


    void Start () {
    }

    void Update () {
        playerScore += Time.fixedDeltaTime;
        scoreText.text = "Score: " + Mathf.Round((playerScore * 100)).ToString();

    }


    public void IncreaseScore(int amount){
        playerScore += amount;
    }

    void OnDisable() {
        PlayerPrefs.SetFloat ("Score", Mathf.Round((playerScore * 100)));

    }

}

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameOverScript : MonoBehaviour {

    HUDScript hud;
    float score = 0;
    float highScore;
    public Text scoreText;
    public Text highScoreText;


    // Use this for initialization
    void Start () {
        score = PlayerPrefs.GetFloat ("Score");
        highScore = PlayerPrefs.GetFloat ("HighScore", 0);

        scoreText.text = "Score: " + score.ToString ();
        if (score > highScore) {
        highScoreText.text = "High Score: " + score.ToString ();
            PlayerPrefs.SetFloat("HighScore", score);
        } else {
            highScoreText.text = "High Score: " + highScore.ToString();
        }

    }

}

【讨论】:

    【解决方案2】:

    我不知道,但是在保存值时将高分乘以 100 有点奇怪,但在加载时却没有。这可能会导致错误,如下所示:

    Current high score: 0
    Game 1: Load current high score: 0. Final player score: 5. Save current high score: 500.
    Current high score: 500
    Game 2: Load current high score: 500. Final player score: 7. 7 < 500. No high score saved.
    

    也许你不应该在保存时做 Mathf.Round(playerScore * 100) 的事情?

    【讨论】:

      【解决方案3】:

      在更新中不断保存分数是低效的并且可能容易出错(就像你的情况一样)(这也没有意义)。我建议您存储分数(根据您的条件_作为用户完成游戏。

      请同时公开 playerScorehighScore 变量,以便您可以观看并确认其价值测试目的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多