C# PlayerPrefs正在unity editor上工作,但不在构建中
我一直在寻找信息,试图解决我的问题,我只是不知道是什么原因造成的。在unity编辑器中,Player Pref似乎工作得很好,但一旦我为Android或PC创建了一个版本,它们就都消失了。我的项目中没有PlayerPrefs.deleteAll 我有3个场景:菜单、游戏和游戏结束。启动菜单场景时,我运行连接到主摄影机的脚本:C# PlayerPrefs正在unity editor上工作,但不在构建中,c#,unity3d,C#,Unity3d,我一直在寻找信息,试图解决我的问题,我只是不知道是什么原因造成的。在unity编辑器中,Player Pref似乎工作得很好,但一旦我为Android或PC创建了一个版本,它们就都消失了。我的项目中没有PlayerPrefs.deleteAll 我有3个场景:菜单、游戏和游戏结束。启动菜单场景时,我运行连接到主摄影机的脚本: using System.Collections; using System.Collections.Generic; using UnityEngine; public
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class prefs : MonoBehaviour
{
public static int bestRecord;
// Start is called before the first frame update
void Start()
{
bestRecord = PlayerPrefs.GetInt("K", 1);
Debug.Log(PlayerPrefs.GetInt("K").ToString());
}
}
当我进入游戏场景时,我的分数是一个TextMeshProUGUI,它会通过调用重复不断更新:
public TextMeshProUGUI points;
void Start()
{
stop = false;
InvokeRepeating("subirMetros", 0f, 0.01f);
}
private void subirMetros()
{
if (stop == false)
{
points.SetText(Math.Round(character.transform.position.x, 0) + "");
}
}
//GameOver is called when the player dies
public void GameOver()
{
stop = true;
CancelInvoke("subirMetros");
int finalPoints = Int32.Parse(points.text);
int recordActual = PlayerPrefs.GetInt("K");
if (recordActual < finalPoints)
{
PlayerPrefs.SetInt("K", finalPoints);
}
SceneManager.LoadScene("GameOver");
}
}
奇怪的是,它在unity编辑器中工作,但在我构建它时却不工作。如果有人能帮助我,我将不胜感激,谢谢。出于调试目的,为了反映应用程序的新用户将看到的内容,从编辑器运行时偶尔使用
PlayerPrefs.DeleteAll()
。(用#if UNITY_EDITOR包装,以防忘记取出。)完成后,在EDITOR中再次点击play——一切正常吗?为什么不调用PlayerPrefs.Save()
?@PhilippLenssen我不知道这个功能,我一直在测试,在构建时仍然得到0分。。。它没有意义,因为我使用1作为默认值。当我删除所有的播放器首选项时,它仍然在编辑器中工作。你需要按照已经建议的进行调试。但是,您应该使用最合适的数据类型来存储数据,即int
,因此在内部使用该数据类型,而不是string
。可能是字符串
的格式与您期望的格式不符,因此无法转换为int
@特洛伊木马程序,原因是!这是转换过程中的一个错误。。。谢谢!
public TextMeshProUGUI record;
void Start()
{
record.text = PlayerPrefs.GetInt("K").ToString();
}