如何通过Unity的PlayerPrefs跳过关卡?

时间:2019-11-02 22:12:53

标签: c# android unity3d

所有人。

我已经有一段时间了。基本上,我正在开发手机游戏,我想设置广告按钮以在有人按下某个按钮时跳过该按钮。现在,它可以跟踪我离开的位置,但是当我按下“重新开始”按钮并跳过尝试重新启动之前刚刚离开的级别时,它不会重置。

我试图使用两个不同的功能来工作,但是首先要关注的是int“ sceneNo”和StartOver函数。我使用PlayerPrefs来设置sceneNo。上。

sceneNo = PlayerPrefs.GetInt("Level", SceneManager.GetActiveScene().buildIndex);

而且,这是StartOver的代码。

public void StartOver()
{
    SceneManager.LoadScene(1);
    PlayerPrefs.DeleteKey("level");
    sceneNo = (SceneManager.GetActiveScene().buildIndex);
}

对于我尝试重置游戏的两种不同功能...

public void SkipMe()
{

    sceneNo = SceneManager.GetActiveScene().buildIndex + 1;
    SceneManager.LoadScene(PlayerPrefs.GetInt("Level", sceneNo));
}

还有一个...

public void SkipAdFunction()
{
    SceneManager.LoadScene(sceneNo);
    Answer.text = "";
    PlayerPrefs.SetInt("Level", sceneNo + 1);
    sceneNo = (SceneManager.GetActiveScene().buildIndex);

}

最后,这是脚本的其余部分。

void HandleShowResult(ShowResult result) { if (result == ShowResult.Finished) { Playable.GetComponent<LoadScene>().SkipMe();

这两个脚本都执行相同的操作。 任何和所有帮助将不胜感激如何解决该问题。预先谢谢你。

1 个答案:

答案 0 :(得分:0)

StartOver()中,您提到了这一点

 PlayerPrefs.DeleteKey("level");

但是在 SkipMe(),SkipAdFunction()中,您编写了

PlayerPrefs.SetInt("Level", sceneNo + 1);

请参见级别,您提到的是不同的键,键区分大小写,因此请更改