Unity3d Unity PlayerPrefs在WebGL中构建游戏后加载不同的数据

Unity3d Unity PlayerPrefs在WebGL中构建游戏后加载不同的数据,unity3d,unity-webgl,Unity3d,Unity Webgl,我在用Webgl构建的WordSearch游戏中使用PlayerRefs。当我在游戏过程中多次关闭游戏时,问题就会出现。它会自动从随机倒计时值开始,比如20、140等,以及之前的分数(有时不是应该保存的准确分数)。 这是我使用的playerpreds的代码 [SerializeField] public float currentLevelScore = 0.0f; [SerializeField] private float timeLeft; [SerializeField] privat

我在用Webgl构建的WordSearch游戏中使用PlayerRefs。当我在游戏过程中多次关闭游戏时,问题就会出现。它会自动从随机倒计时值开始,比如20、140等,以及之前的分数(有时不是应该保存的准确分数)。 这是我使用的playerpreds的代码

[SerializeField] public  float currentLevelScore = 0.0f;
[SerializeField] private float timeLeft;
[SerializeField] private string accessToken = "";

void Awake()
{
    GetPlayerPrefData();
    }

void Start() {
        if(timeLeft == fixedTime)
        {
            PauseGame();
        }

        //Retrive Saved Player prefs Data
        if (PlayerPrefs.GetFloat("timeLeft")<150 && PlayerPrefs.GetFloat("timeLeft") > 0)
        {
            StartGameCanvas.SetActive(false);
        }
        else
        {
            if(PlayerPrefs.GetFloat("timeLeft") == 150)
            {
                StartGameCanvas.SetActive(true);
            }
        }

        if (PlayerPrefs.GetFloat("timeLeft").Equals(0) || PlayerPrefs.GetFloat("timeLeft").Equals(null))
        {
            PlayerPrefs.SetFloat("timeLeft", fixedTime);
        }
}

void GetPlayerPrefData()
    {
        accessToken = PlayerPrefs.GetString("accessToken");
        currentLevelScore = PlayerPrefs.GetFloat("currentLevelScore");
        float countdownTime = PlayerPrefs.GetFloat("timeLeft");
        if (countdownTime <= 0)
        {
            timeLeft = fixedTime;
        }
        else
        {
            timeLeft = countdownTime;
        }
    }

//This function is calls when the Time is up or user hit the quit button
 public void ResetAllData()
    {
        float defaultScore = 0;
        string defaultAccessToken = null;

        PlayerPrefs.SetFloat("currentLevelScore", defaultScore);
        PlayerPrefs.SetFloat("timeLeft", fixedTime);
        PlayerPrefs.SetString("timeLeft", defaultAccessToken);
        PlayerPrefs.Save();
    }
[SerializeField]公共浮点currentLevelScore=0.0f;
[SerializeField]专用浮点timeLeft;
[SerializeField]私有字符串accessToken=“”;
无效唤醒()
{
GetPlayerPrefData();
}
void Start(){
if(timeLeft==fixedTime)
{
PauseGame();
}
//检索已保存的播放器prefs数据
如果(PlayerPrefs.GetFloat(“timeLeft”)0)
{
StartGameCanvas.SetActive(假);
}
其他的
{
if(PlayerPrefs.GetFloat(“timeLeft”)==150)
{
StartGameCanvas.SetActive(真);
}
}
if(PlayerPrefs.GetFloat(“timeLeft”).Equals(0)| | PlayerPrefs.GetFloat(“timeLeft”).Equals(null))
{
PlayerPrefs.SetFloat(“timeLeft”,fixedTime);
}
}
void GetPlayerPrefData()
{
accessToken=PlayerPrefs.GetString(“accessToken”);
currentLevelScore=PlayerPrefs.GetFloat(“currentLevelScore”);
float countdown=PlayerPrefs.GetFloat(“timeLeft”);

如果(countdownTime
PlayerPrefs.GetFloat(“timeLeft”).Equals(null)
->
!PlayerPrefs.HasKey(“timeLeft”)
。不会解决您的问题,但会使您的代码更简单。@gman感谢您的建议。非常感谢您的建议
PlayerPrefs.GetFloat(“timeLeft”).Equals(null)
->
!PlayerPrefs.HasKey(“timeLeft”)
。不会解决您的问题,但会使您的代码更简单。@gman感谢您的建议。非常感谢您的建议