本文介绍了Unity-如何保存记录?与playerpref的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以向我解释如何使用PlayerPrefs保存得分"并将其显示在屏幕上吗?

Can anyone explain to me, how to save "score" with PlayerPrefs and display it on the screen?

public class record : MonoBehaviour{

private Text counterText;
public float score;

void Start()
{
    counterText = GetComponent<Text>() as Text;

}

void Update()
{
    score += Time.deltaTime;
    counterText.text = "Score: " + score.ToString("00");
}}

推荐答案

您可以在类中添加2个方法

You can add 2 methods to your class

void SaveScore()
{
    PlayerPrefs.SetFloat("score", score);
}

void LoadScore()
{
    PlayerPrefs.GetFloat("score");
}

有关PlayerPrefs的更多信息,请参见: http://docs.unity3d.com/ScriptReference/PlayerPrefs .html

More informations about PlayerPrefs here: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

PlayerPrefs存储在系统注册表中.使用它们保存分数不是一个好主意,首选一个具有加密功能的简单文本文件:(堆栈溢出团结论坛)

PlayerPrefs are stored in the system registry. It's not a good idea to use them to save the score, prefer a simple text file with encryption: (Stack Overflow, Unity Forum)

这篇关于Unity-如何保存记录?与playerpref的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 12:07