簡體   English   中英

高分與 PlayerPref c#

[英]Highscore with PlayerPref c#

所以我最近開始編碼,這也是我在 C# 和 Unity 中的第一個項目。 所以我的問題可能很容易解決,並且可能會出現一些錯誤。 所以我願意接受想法或解釋。

所以我嘗試制作一個腳本來將我的高分保存在 PlayerPrefs 中,並在我玩 2D 無盡跑步者時顯示它們。 (類似於 Chrome 中的恐龍游戲)“正常”分數有效,但我似乎無法顯示高分。 有誰知道我哪里出錯了或者我怎么能以一種簡單的方式做到這一點?

我的畫布中已經有一個圖像+文本(正如我在網上看到的正常分數顯示)並添加了它,這(下)是我的高分代碼。 我無法在網上找到任何解釋來解決它,如果您知道任何來源,我很樂意閱讀並了解更多信息。

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

public class HighScore : MonoBehaviour
{
    public Text HighscoreText;

    
    private void Start()
    {
       
    }
  
    void StoreHighscore(int newHighscore)
    {
        int oldHighscore = PlayerPrefs.GetInt("Highscore",0);
        if (newHighscore > oldHighscore)
        {
            PlayerPrefs.SetInt("Highscore", newHighscore);
    
        }

    }

    void Update()
    {
     HighscoreText.text = PlayerPrefs.GetInt("newHighscore") + " Highscore";   
    }
}

我在我的 GameOver 腳本中添加了 PlayerPref.Save()。 在將場景重新加載為 GameOver 之前保存 PlayerPrefs。

謝謝

void Update()
    {
     HighscoreText.text = PlayerPrefs.GetInt("newHighscore") + " Highscore";   
    }

一個簡單的錯誤,您只是嘗試使用鍵“newHighscore”獲取 PlayerPref,但該鍵不存在,因為當您使用 PlayerPrefs.SetInt 時,您傳遞了鍵“Highscore”。

要解決這個問題,只需嘗試獲取正確的密鑰:

void Update()
        {
         HighscoreText.text = PlayerPrefs.GetInt("Highscore") + " Highscore";   
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM