使用 PlayerPrefs 拯救心灵

Save hearts with PlayerPrefs

我正在使用 unity c# 构建一个 2d 小行星射击游戏。我需要的是为下一关拯救玩家的心。所以我只是把分数保存下来,我现在需要的是拯救心脏。分数是为下一个级别保存的,所以它是这样工作的,例如在第一级你有 3000 分,然后你进入 nesxt 级别,然后它将从 3000 开始,但是如果你点击重新启动或者如果oyu退出你从0开始,来回第一级。这就是分数如何运作,同样需要与心相伴。因此,如果您以 1 颗心完成第一级,那么您需要以 1 颗心进入 2 级。如果你用 3 颗心完成 2 tn 3 颗 3 颗心将开始,如果你单击重新启动或者如果你退出,那么它将从默认开始。红心是图片 UI,我想用玩家偏好来做得分,但我不确定怎么做。

if (col.gameObject.tag == "AsteroidBig") {
            if (heart.enabled == true && heart1.enabled == true && heart2.enabled == true) {
                heart.enabled = false;
                Instantiate (explosion, col.transform.position, col.transform.rotation);
                Vector3 pos = new Vector3 (0f, 0f, 0f);
                Instantiate (player, pos, Quaternion.identity);
                Destroy (col.gameObject);
                Destroy (this.gameObject);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
            } else if (heart1.enabled == true && heart2.enabled == true) {
                heart1.enabled = false;
                Instantiate (explosion, col.transform.position, col.transform.rotation);
                Vector3 pos = new Vector3 (0f, 0f, 0f);
                Instantiate (player, pos, Quaternion.identity);
                Destroy (col.gameObject);
                Destroy (this.gameObject);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
            } else if (heart2.enabled == true) {
                heart2.enabled = false;
                Instantiate (explosion, col.transform.position, col.transform.rotation);
                Vector3 pos = new Vector3 (0f, 0f, 0f);
                Instantiate (player, pos, Quaternion.identity);
                Destroy (col.gameObject);
                Destroy (this.gameObject);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
            } else if (heart.enabled == false && heart1.enabled == false && heart2.enabled == false) {
                Instantiate (explosion, col.transform.position, col.transform.rotation);
                Destroy (col.gameObject);
                Destroy (this.gameObject);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
                Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
                GameOverPanel.SetActive (true);
            }
        }

一个带红心的if语句看起来是这样的,有点乱,但就是这样。如果你触摸小行星,它会检查是否有 3 个红心,其中一个设置为 false,如果你有 2 个红心,那么另一个将设置为 false,依此类推,直到你没有红心并且你松了。这就是碰撞的逻辑。请帮助拯救这颗心,问任何问题,因为我知道它已经搞砸了。

这里有两个选项,具体取决于哪个对您来说更容易

1) 创建静态 class 加载新关卡时,静态 classes 不会重置,因此您可以在此处存储有关玩家的任何信息

public class GameData
{
    public static int NumberOfHearts = 3;
}

然后在您的代码中更新 NumberOfHearts 以反映当您被小行星触及时您有多少颗心。例如,

GameData.NumberOfHearts--; //We just got touched by an asteroid

2) 场景管理器(如果您使用 Application.LoadLevel() 则不适用)

您可以在场景的根部为您的玩家创建一个游戏对象。
然后当需要加载关卡时,您可以调用 SceneManager.MoveGameObjectToScene(playerObject, "sceneName")

有关 MoveGameObjectToScene 的更多信息,请参阅 here

在这种情况下,该对象将与任何脚本和子对象一起使用,因此您已经拥有了下一级别的所有红心。