Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PlayerPrefs正在unity editor上工作,但不在构建中_C#_Unity3d - Fatal编程技术网

C# PlayerPrefs正在unity editor上工作,但不在构建中

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

我一直在寻找信息,试图解决我的问题,我只是不知道是什么原因造成的。在unity编辑器中,Player Pref似乎工作得很好,但一旦我为Android或PC创建了一个版本,它们就都消失了。我的项目中没有PlayerPrefs.deleteAll

我有3个场景:菜单、游戏和游戏结束。启动菜单场景时,我运行连接到主摄影机的脚本:

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();
}