Introduction: (4) Starting to Make a Game - Making a Menu - Part 2
Today we will see how to add a credits scene to our main menu, it will be similar to the last Instructable!
Step 1: Adding the Scene and Script
First, make a new scene named "Credits" in the "scenes" folder, double click on the scene and add a C# script named "Credits" in the "scripts" folder. Now double click on the script to open up MonoDevelop or Visual Studio.
Step 2: Editing the Credits Script
Then delete all the default code and copy+paste this into the script:
If you have any questions about how the code works comment what you want to know I will gladly help!
Step 3: Setting Up the Canvas
Now add a Canvas and Text by right clicking -> UI -> Text and renamed the text to "Credits".
Then when you click on the credits you can edit the text, in the above picture you can see all my preferred settings.
I made the Text:
- Size 25
- Aligned in the middle
Note: To easily move the "Credits" text click on the button in the red circle and drag the text around.
Step 4: Setting Up the Credits Manager
Now we need to add an empty gameobject to put the script on, click -> Create Empty and name it "Credits Manager".
Then add the "Credits" script to it by dragging and dropping the script to the credits manager.
Finally, make sure that you type "Menu" in the "Exit Scene" space.
Step 5: Exiting the Scene (By Clicking a Button)
To do this add a button by right clicking -> UI -> Button.
Then click on the arrow beside the button to show the buttons "Text".
You can change the button text to whatever you want but I will make it simply say "Back".
Step 6: Exiting the Scene (By Clicking a Button)
Now select the button and under the "On Click" tab at the bottom, there should be a + symbol, click it.
Drag and drop the Credits Controller to the box that says "None (Object)"
click on: No Function -> Credits -> ExitScene
Step 7: Exiting the Scene (By Pressing a Key)
To do this one, add another text by right clicking -> UI -> Text.
Now click on the Credits Manager and toggle the "Has Escape Key" option and change the "Escape Key" to whatever you want, in this case, I set it to "Escape".
Then make the text say whatever you want, I will make it say "Press Escape to Return to the Menu"
Step 8: Setting Up the Anchor
Whether you added the button or text you still need to add an anchor.
So first we will select the text or button and click on the box that has a red circle around it, that will make another box called Anchor Presets.
Now hold shift and select the anchor preset that the red arrow is pointing to. This will make the text or button stay in the bottom right corner.
Step 9: Setting Up the Background (Solid Color)
Then click on the Main Camera and set Clear Flags to "Solid Color", then change the Background color to anything you want.
Step 10: Setting Up the Background (Image)
For this purpose, I'm gonna use this Image by Drew Selby.
You have to open the folder where your background is located then drag and drop the picture into the "Assets" folder in Unity. Then click on your background and at the top, change the Texture Type to "Sprite (2D and UI)". After that at the bottom click "Apply".
Step 11: Setting Up the Background (Image)
Then create an image named "Background" and make sure it is above everything else like in the image above.
Now click on the image named "Background" and drag your background to the Source Image space.
Step 12: Setting Up the Background (Image)
So now we need to set Anchors, select the image named "Background" and click on the box that has a red circle around it, that will make another box called Anchor Presets.
Now hold shift and alt then select the anchor preset that the red arrow is pointing to. This will make the Background stretch across the screen no matter what resolution the game is.
Note: if you use this background you might want to change the text color to white.
Step 13: Finished!
Remeber to save by typing: ctrl + s
Next Instructable will teach you how to make an Options Menu.
If you had any problems or if you have any questions or comments, comment them below!