如何解锁关卡并从 Unity 2D 中的获胜页面跳转到下一个场景?

关卡完成后,将显示获胜屏幕。其中有两个按钮继续和菜单。我已经设法停用按钮,只保持第一级按钮解锁,但当我清除 1 级时无法解锁 2 级按钮。我也希望继续按钮在它之后跳转到 2 级在完成关卡 1 时显示在下一个场景中。这个游戏是突围风格,这些是我无法解决的问题。我附上了我认为必要的相应脚本。如果你想要其他人请在评论中询问他们。所有脚本的完整列表在最后。我真的很感激一些帮助。如果你要求他们,我一定会尝试更多地解释我的问题。所以请稍后再检查这个问题以检查更改。


下面的脚本附加到第 1 级:-


{

[SerializeField] int breakableBlocks;  // Serialized for debugging purposes

SceneLoader sceneloader;


private void Start()

{

    sceneloader = FindObjectOfType<SceneLoader>();

}


public void CountBreakableBlocks()

{

    breakableBlocks++;

}


public void BlockDestroyed()

{

    breakableBlocks--;

    if (breakableBlocks <= 0)

    {

        GetComponent<LevelSelector>().levelunlocked = 

        sceneloader.LoadWinScreen();

    }

}

}

下面的脚本附加到级别选择器:-


{

    public Button[] levelButtons;

    public int levelunlocked = 1;

    private void Start()

    {

        int levelReached = PlayerPrefs.GetInt("levelReached", levelunlocked);

        for (int i = 0; i < levelButtons.Length; i++)

        {

            if (i + 1 > levelReached)

            {

                levelButtons[i].interactable = false;

            }

        }

    }

}


慕斯709654
浏览 150回答 1
1回答

莫回无

我认为您的问题源于在离开您的 1 级场景之前没有更新您的“levelReached”玩家偏好值。在您发布的 1 级脚本中:public void BlockDestroyed()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; breakableBlocks--;&nbsp; &nbsp; &nbsp; &nbsp; if (breakableBlocks <= 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetComponent<LevelSelector>().levelunlocked =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sceneloader.LoadWinScreen();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }当您的 LoadWinScreen 函数返回 void 时,以下行应该会引发错误:GetComponent<LevelSelector>().levelunlocked =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sceneloader.LoadWinScreen();尝试将该部分代码更改为以下内容:if (breakableBlocks <= 0){&nbsp; &nbsp; PlayerPrefs.SetInt("levelReached", 2);&nbsp; &nbsp; sceneloader.LoadWinScreen();}请注意,在上面的示例中,我假设您有一个单独的脚本运行每个级别的游戏逻辑,因为我没有使用变量来设置新的 PlayerPrefs“levelReached”值。我建议在每个场景中都有一个 GameManager 脚本并跟踪您当前所处的关卡,这将允许您执行以下操作:if (breakableBlocks <= 0){&nbsp; &nbsp; if(PlayerPrefs.GetInt("levelReached") < GameManager.currentLevel + 1)&nbsp; &nbsp; &nbsp; &nbsp; PlayerPrefs.SetInt("levelReached", GameManager.currentLevel + 1);&nbsp; &nbsp; sceneloader.LoadWinScreen();}这需要一些单独的逻辑来承载跨场景的游戏状态,并且有几种方法可以解决这个问题(参见下面的示例和相关的 stackoverflow 链接):使用 Unity DontDestroyOnLoad 函数的单例设计模式ScriptableObjects 在关卡开始存储和检索数据PlayerPrefs 在关卡开始时存储和检索数据unity - 在场景之间传递数据
打开App,查看更多内容
随时随地看视频慕课网APP