简体   繁体   English

单击一定数量的按钮时如何统一更改场景

[英]How to change a scene in unity when a certain amount of buttons are clicked

I am working on a memory matching game in Unity where the user has to memorize 3 letters and then click 3 image buttons with those letters on them. 我正在Unity中开发一款内存匹配游戏,用户必须记住3个字母,然后单击上面带有这些字母的3个图像按钮。 If the correct image buttons are clicked, it changes to the level complete scene, if not it goes to a game over scene. 如果单击正确的图像按钮,它将切换到完成级别场景,否则,将转到游戏结束场景。 How to I go about changing the scene if the 3 correct buttons are clicked? 如果单击3个正确的按钮,如何更改场景?

I'm new to Unity so any links/code snippets would be amazing! 我是Unity的新手,所以任何链接/代码段都很棒!

If the player has to memorize 3 letters and then press 3 buttons with these letters, I assume there are going to be more buttons with wrong letters (otherwise it is impossible to loose). 如果玩家必须记住3个字母,然后按3个带有这些字母的按钮,那么我认为将会有更多带有错误字母的按钮(否则就无法松开)。

I assume you know how to create buttons in the Canvas. 我假设您知道如何在“画布”中创建按钮。 So you pass a different function for each letter which will set to true a flag for that letter and increase a counter. 因此,您为每个字母传递了一个不同的函数,该函数会将该字母的标志设置为true并增加一个计数器。

void OnClickA()
{
    counterLetter++;
    FlagA = true;
    //or an array which is position is one leter index = 0 -> a, index = 1 -> b
    ArrayLetters[0] = true
}

Then you check if(counterLetter == 3) and you check if the flags you were expecting are true. 然后您检查if(counterLetter == 3)并检查您期望的标志是否为真。 You can implement this as you prefer. 您可以根据需要实现此目标。 Checking the flags or checking if the correct positions of the array are set to 1. 检查标志或检查数组的正确位置是否设置为1。

In case the player selected the correct button, (And I think here is where starts the part you didn´t know how to solve) loads the victory level: 如果玩家选择了正确的按钮((我认为这是您不知道如何解决的部分开始的位置)加载胜利级别:

Application.LoadLevel("VictoryScene");

In case he made a mistake: 万一他犯了一个错误:

Application.LoadLevel("LostScene");

Edit based on @ZayedUpal´s comment.For new versions of Unity (later than5.3), you should use intead: 根据@ZayedUpal的注释进行编辑。对于Unity的新版本(低于5.3),您应该使用intead:

UnityEngine.SceneManagement;
SceneManager.LoadScene("nameOfScene");

To be able to load this scenes, first you need to create them in the Editor, then you need to add them to the build. 为了能够加载这些场景,首先需要在编辑器中创建它们,然后需要将它们添加到构建中。

File -> Build Settings File -> Build Settings

There you drag and drop the scenes you want to build in the top window. 您可以在此处将要构建的场景拖放到顶部窗口中。

在此处输入图片说明

So later you will be able to pass their names to the function LoadLevel() 因此,稍后您将能够将其名称传递给函数LoadLevel()

Changing scenes is pretty straight forward in Unity: 在Unity中更改场景非常简单:

using UnityEngine.SceneManagement;
SceneManager.LoadScene("OtherSceneName");

You can also use the scene build index found in the build menu, although I don't recommend it because it might change if your project gets bigger: 您也可以使用在构建菜单中找到的场景构建索引,尽管我不建议这样做,因为如果您的项目变大,它可能会改变:

SceneManager.LoadScene(1);

Unity docs on this method: https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html 有关此方法的Unity文档: https : //docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM