我在安卓系统上使用PlayerPrefs in unity (C#)时遇到错误
它看起来并没有保存它
脚本1
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Scores : MonoBehaviour
{
public static int highscore;
public static int points;
public void Start()
{
}
public void Update()
{
highscore = PlayerPrefs.GetInt("highscore");
PlayerPrefs.SetInt("highscore", highscore);
PlayerPrefs.Save();
}
}
脚本2(使用脚本的部分)
void Update()
{
highscore = PlayerPrefs.GetInt("Highscore");
if (points > highscore)
{
highscore = points;
highscoretext.text = "Highscore: " + highscore;
highscoretext2.text = "Highscore: " + highscore;
}
我只是可以弄清楚当我得到一个高分,例如12,我死了,我回到主屏幕,然后回到游戏,当重新启动应用程序时,高分是游戏相同的
发布于 2020-06-17 11:03:22
当您现在设置最高分数时,您将获得当前的最高分数并将其存储在highscore
变量中。然后使用highscore
变量覆盖高分,有效地将高分设置为自身。您实际要做的是检查当前分数是否大于最高分数,如果是,则将最高分数设置为当前分数。
highscore = PlayerPrefs.GetInt("highscore");
if(score > highscore)
{
PlayerPrefs.SetInt("highscore", score);
PlayerPrefs.Save();
}
代码不能工作的另一个原因是,在脚本1中用小h拼写"highscore"
,而在脚本2中用大h拼写。
而且,每次更新都调用PlayerPrefs.GetInt
是非常低效的。您应该将它们转换为仅在需要获取或设置高分数时才调用的函数。
https://stackoverflow.com/questions/62426656
复制相似问题