Now that we have the text displaying correctly, let's add in the ability to move from the main menu into the game and quit the game.
At this point, you will see a new child object to the Canvas called Button and if you were to extend that object, you'd see that it has a Text child as well.
Button
object and open up the Anchors Preset menu and from there select the bottom-stretch option.Notice now that Pos X and Width have been replaced with a Left and Right value. When we use the stretch option, it will scale the object either horizontally or vertically depending on your choice. The Left and Right values are how far to the left or right in pixels that the center of the object will be based.
300
to center the object with 300 pixels on each side. Then, change Pivot Y to 0
to base our positions to offset via the top and then change the Pos Y value to 100
to move the object 100 pixels above the bottom of the screen.Button
object and change the button's text child's text value to Start Game
. OSP-DIN
and change Font Size to 25
.Button
object and from Inspector scroll down to the Button component. From there, you'll see a number of colors and a target graphic that will be tinted when you mouseover or click on the button (try playing the game and interacting with the button to see the default behaviour).Example Code
folder and bring the contents of the Art Assets
folder for Chapter 2 into the Sprites
folder from our Project tab.Sprite Swap
and then for Highlighted Sprite put in buttonYellow
, Pressed Sprite to buttonGreen
, and Disabled Sprite to buttonDisabled
. buttonBlue
. Afterward, change Image Type to Sliced. You'll notice that there is an error saying it doesn't have a border, and that's true.buttonBlue
sprite. From Inspector, you'll see the properties for Sprite and a button called Sprite Editor. Click on it and you'll be able to see properties for how the sprite will be drawn. It's also used for animations, but in this case we are going to establish a border. For L (left), R (right), T (top), and B (bottom) put in 10 and then click on the Apply button.What this will do is anytime we create a sprite using the Sliced type and change the width and height, the four non-cardinal corners will not scale but the others will, allowing us to create boxes of whatever size we want. Pretty nifty, huh?
50
. Back in Hierarchy, open up the Exit Game object and change the Text's text to Exit Game.We now have two customized buttons that look great, but aren't functional. Let's fix that now.
MainMenuBehaviour
and drag it up to the top of your Hierarchy for easy access.MainMenuBehaviour
, make sure the language is set to C#, and then select Create and Add. This will create a new script and attach it for us automatically. This is created in the Assets
folder, so go ahead into the Project tab and move the script into the Scripts
folder and then double-click on it to enter your IDE of course.using UnityEngine; using UnityEngine.SceneManagement; public class MainMenuBehaviour : MonoBehaviour { public void LoadLevel(string levelName) { SceneManager.LoadScene(levelName); } public void QuitGame() { #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying=false; #else Application.Quit(); #endif } }
The LoadScene
function will load a level based on a name that we provide to it and the QuitGame
function will either set the editor to no longer play if we are in the Unity Editor, and will quit the game if we are playing an exported version.
Chapter_1
, but that's not terribly descriptive. From the Project tab, open the Scenes folder and rename the level to Gameplay
by selecting it and then clicking on its name.Button
component. From there, in the On Click ()
section, click on + to add something for our button to do.Main Menu Behaviour
object from the Hierarchy tab to the area that currently says None (Object) that's just been added to the list.Gameplay
.OnClick
action just like we did before, except this time call MainMenuBehaviour | Quit Game.Main_Menu
to your list at index 0 by selecting Add Open Scenes and then dragging the Main_Menu
level to the top so it will be the level that starts off when we start the game.