unity游戏开发中的TotalScore系统

TotalScore system in unity game development

我已经编写了这个脚本来获取分数并且这个脚本工作正常但是每当我结束游戏或游戏结束时scorecount重置为0并且当我重新运行我的游戏时它再次从0开始计数而我想要它记住我的最后一点,并从我离开的地方开始计数。我的意思是它应该像一生的总分。

这是我的代码示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class ShackUp : MonoBehaviour 
{

public Text scoreText;


private int scoreCount = 0;

private void Update () 
{
    if (Input.GetKeyDown (KeyCode.Escape))
        OnbackButtonPressed ();

    if (gameOver)
        return;

    if (Input.GetMouseButtonDown (0)) 
    {
        if (PT ())
        {               
            SpawnTile ();
            scoreCount++;
            scoreText.text = scoreCount.ToString ();
        }
        else
        {
            EndGame ();
        }
    }

}

您有多种解决方案可以做到这一点。

最简单的是使用PlayerPrefs。 使用 PlayerPrefs,您允许 Unity 将数据保存到磁盘。

要保存值,请执行以下操作:

PlayerPrefs.SetFloat("Score", scoreCount);
PlayerPrefs.Save();

稍后阅读:

var score = PlayerPrefs.GetFloat("Score");

对于偏好值和小数据(如您的分数),这是一个很好的解决方案。

如果您需要存储更大的数据块,另一种解决方案是将数据保存到文件中。要获取 Unity 知道它可以访问的文件夹的路径,无论平台是什么,请像这样获取 persistantDataPath

Application.persistantDataPath

最后,如果你真的有很多数据或者你需要在线保存,你可以考虑使用服务器或数据库,但我想这不是你现在问的,所以我不会开发它这里

编辑,代码中的示例,使用 属性 以便将 ScoreCount 保存在 playerPrefs

public class ShackUp : MonoBehaviour 
{

    public Text scoreText;


    private int ScoreCount 
    {
        get{ return PlayerPrefs.GetInt("Score", 0); }
        set{ PlayerPrefs.SetInt("Score", value); }
    }

    private void OnDestroy()
    {
        PlayerPrefs.Save();
    }

    private void Update () 
    {
        if (Input.GetKeyDown (KeyCode.Escape))
            OnbackButtonPressed ();

        if (gameOver)
            return;

        if (Input.GetMouseButtonDown (0)) 
        {
            if (PT ())
            {               
                SpawnTile ();
                ScoreCount++;
                scoreText.text = ScoreCount.ToString ();
            }
            else
            {
                EndGame ();
            }
        }

    }
}

编辑: 以下是您评论后的一些精确度

1) 为了您对 c# 属性的理解: 是的,我们可以用 PlayerPrefs.GetFloat("Score") 行替换脚本中的变量 scoreCount。这行得通。但是在这个例子中我做了一些不同的事情:我使用了 属性。它是这样声明的:

    private int ScoreCount 
    {
        get{ return PlayerPrefs.GetInt("Score", 0); }
        set{ PlayerPrefs.SetInt("Score", value); }
    }

看,它就像一个变量:它是私有的,它是一个整数,它的名称是 ScoreCount(惯例是在这些开头使用大写字母,但这不是强制性的) 然后,我没有给这个变量一个值,而是给它 accessors: a get like this get{ return PlayerPrefs.GetInt("Score", 0); } 这意味着任何时候你写类似 int a = ScoreCount 的东西,它实际上会为你调用PlayerPrefs.GetInt("Score", 0);,所以你不必写它。

setter 的工作原理相同。在 属性 的集合部分,您始终可以访问名为 value 的变量。因此,如果您在代码中编写 ScoreCount = 3,它实际上会调用 set{ PlayerPrefs.SetInt("Score", value); },并在 value 变量中使用数字 3。

您可以阅读有关属性的更多信息here

总而言之,您可以调用

scoreText.text = PlayerPrefs.GetInt("Score").ToString ()

但这会起到同样的作用

scoreText.text = ScoreCount.ToString ()

2)

关于向字符串中添加一些文本。这称为字符串连接 文档是 here

总而言之,您可以通过以下两种方式完成此操作。

使用“+”运算符添加字符串。由于“+”是一个字符串,添加一个 int 工作并给出一个字符串,所以你不需要再写 toString 了:

scoreText.text = "+" + ScoreCount

有点复杂,但如果你需要更复杂的字符串格式,请使用 string.Format method

scoreText.text = String.Format("+{0}", ScoreCount)

这会将 {0} 替换为 ScoreCount。您可以使用 {1}、{2} ...

添加更多变量