We're almost done with the tutorial! The last part we need to add is the main menu.
The main menu for our platformer is a level selector: it allows you to revisit any level you've unlocked, without having to start from level 1. It also allows you to start a new game, meaning it will re-lock every level except the first one, and to quit the application.
1. Level select buttons
Open the Menu scene. You'll notice that every button under Canvas / Panel / Buttons is actually a prefab. This prefab is located under Prefabs / MenuLevelButton.
- Select the MenuLevelButton prefab in the project window
- Add a new string object variable called Scene
- Add a new flow machine component
- Convert its source to a macro called MenuLevelButton
Then, for each level button in the Menu scene, edit the Scene variable to match the name of the level:
- On Level1Button, set it to Level1
- On Level2Button, set it to Level2
- On Level3Button, set it to Level3
- On Level4Button, set it to Level4
We will use this variable to determine which scene to load.
1.1 Load level on click
In the new MenuLevelButton macro, add the following group to load the scene on click:
1.2 Enable if unlocked
Remember how, back when we created the level change script, we saved a LevelX_Unlocked variable upon level completion?
Now is the time to use it: we will check if a saved variable with the matching scene name exists and is true. If it does, the button will be interactable; otherwise, it won't:
1.3 Initial saved variable for new games
Up until now when we were testing, we started by playing the Level1 scene. But if the player started at the Menu scene with no level unlocked, they couldn't even choose the first level from the menu!
To fix this, we will create an initial saved variable. These automatically get created for new games, but will be overridden by any data in the saved game. All we need to do for this is declare the variable in the Initial subtab of the Saved variables tab:
This way, when opening the game for the very first time, Level1_Unlocked will be true.
2. New game button
The new game button will unlock level 1 and lock every level above 1. For this, we will use a For loop, which allows us to increment a number and do an operation on it at each step:
One important thing to note here is that the For loop is inclusive for the first number, but exclusive for the last number. This is why we wrote 5 instead of 4 in the Last field. The Step determines by how much the index changes at every iteration: in our case, it gets incremented by 1.
What this graph does, in other words, is:
- Set Level1_Unlocked to true
- Enter the for loop:
- With Index = 2, set Level2_Unlocked to false
- With Index = 3, set Level3_Unlocked to false
- With Index = 4, set Level4_Unlocked to false
- Exit the for loop
4. Quit button
Just like we did in the pause menu, add an embed flow machine to the QuitButton object with the following graph:
If you test your game now, you should have a pause menu that remembers which levels you unlocked:
Customer support service by UserEcho