【问题标题】:Saving the score, then using it in highscore保存分数,然后在高分中使用它
【发布时间】:2020-06-17 01:22:07
【问题描述】:

我有一个脚本,每 1 秒给你 1 分。

我想知道如何将分数保存为高分。我知道 PlayerPref,但我无法理解它。我也尝试了其他几种解释。

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class score : MonoBehaviour
{
    private Text scoreText;
    private float PIPS;
    public GameObject gameOverScore;
    public GameObject Player;
    public static float scoreAmount;

    public void Start()
    {
        scoreText = GetComponent<Text>();
        scoreAmount = 0.0f;
        PIPS = 1.0f;
    }

    public void Update()
    {
        if (Player.activeInHierarchy == true)
        {
            scoreText.text = scoreAmount.ToString("F0");
            scoreAmount += PIPS * Time.deltaTime;
        }
        else
        {
            scoreText.enabled = false;
            gameOverScore.GetComponent<TextMeshProUGUI>().text = scoreAmount.ToString("F0");
        }
    }
}

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    这是我的一个脚本中的一个实例 -->

    int HighScore = PlayerPerfs.GetInt("HighScore", 0) //If playing First Time Score = 0
    void Save()
        {
            if(ScoreUpdate.CurrentScore > HighScore) //Check if CurrentScore is more than HighScore
            {
                PlayerPrefs.SetInt("HighScore", ScoreUpdate.CurrentScore);//Save New High Score
            }
        }
    

    您现在只需在游戏完成或玩家死亡(如果在任何时候死亡)时调用此函数Save()

    编辑:将您的变量替换为 ScoreUpdate

    【讨论】:

      【解决方案2】:

      虽然这不是理想的方式,但您可以在 Awake 和 OnDisable() 上使用 playerprefs。

      using UnityEngine;
      using UnityEngine.UI;
      using TMPro;
      
      public class score : MonoBehaviour
      {
      private Text scoreText;
      private float PIPS;
      public GameObject gameOverScore;
      public GameObject Player;
      public static float scoreAmount;
      public float HighScore;
      
      public void Start()
      {
          scoreText = GetComponent<Text>();
          scoreAmount = 0.0f;
          PIPS = 1.0f;
      }
      
      private void OnDisable()
      {
          // this will save the highscore in playerprefs when the game ends[application quit].
      
          PlayerPrefs.SetFloat("HighScore", HighScore);
      }
      
      private void Awake()
      {
          // this will load the highscore from playerprefs
          HighScore = PlayerPrefs.GetFloat("HighScore");
      }
      
      public void Update()
      {
          if (Player.activeInHierarchy == true)
          {
              scoreText.text = scoreAmount.ToString("F0");
              scoreAmount += PIPS * Time.deltaTime;
              if (scoreAmount > HighScore)
              {
                  HighScore = scoreAmount;
              }
          }
          else
          {
              scoreText.enabled = false;
              gameOverScore.GetComponent<TextMeshProUGUI>().text = scoreAmount.ToString("F0");
          }
      }
      }
      

      如果这有帮助,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-07
        • 1970-01-01
        • 2014-10-05
        • 2021-09-13
        • 2014-09-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多