Code a button to load a different scene or close the application

This lesson build on from Creating a UI, make sure that you have completed that one first.

Open your Scene with a UI Menu

Create an empty object on the scene called MenuManager

Create a new Script called Menu Manager

Attach the script to the Menu Manager object.

Open the MenuManager script in the editor

We are going to add public methods to this to handle processing input from the buttons.

Delete the Start and Update methodss.

Add the code below.

Lines 4 and 5 import the UI and SceneManagement packages.

The public method DisplayCanvas on lines 10-13 takes one Canvas parameter, the canvas to switch to.

Ideally we would be able to have the current one as a parameter but due to limitations with the Unity Editor we can only have one parameter.

We first need to turn off all of the canvases on the scene by setting their activate status to false.

On Line 11 we find all of the loaded Canvas objects. Note that this will turn off all Canvas objects so it might not be the best solution for you. It actually won’t be in 90% of the cases, but it works.

Now we need to turn them off. This uses a for each loop that loops through each Canvas in the list as shown on line 12. The list is closed on line 14.

Line 13 sets each Canvas object in the list to have it’s active status set to false.

Line 15 sets the one canvas that we want to change to to have an active status.

Lines 19-21 has a public method that will quit the application. This uses the Application.Quit() method. Note that this will not work inside the Unity editor, you need to have a Unity build for this to work.

Finally Lines 23-25 are similar to the load scene triggers previously covered here. The difference here is that we don’t use a trigger we run this method when the button has been pressed.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class MenuManager : MonoBehaviour
{

    public void DisplayCanvas(Canvas canvas) {
        Canvas[] canvases = FindObjectsOfType<Canvas>();
        foreach(Canvas c in canvases) {
            c.gameObject.SetActive(false);
        }
        canvas.gameObject.SetActive(true);

    }

    public void Quit() {
        Application.Quit(); // This will only work in a build not in the editor
    }

    public void LoadScene(string sceneName) {
        SceneManager.LoadScene(sceneName);
    }
}

Now we need to link these methods up to buttons.

First select the Play button on the Main Canvas.

Look in the inspector for OnClick()

Click the +

Drag the MenuManager Object onto where is shows None (Object)

This links the Menu Manager to the button

Now we need to link the method.

Select No Function and then the name of your script and the name of the method to call.

In this case LoadScene

Type the name of the Scene you want to load.

Setup the other buttons in a similar manner.

For the About button, add the DisplayCanvas method

Then add the about Canvas

For the Exit button add the Quit method.

Finally on the Back button on the About Canvas link the DisplayCanvas method to display the Main Canvas

Now you need to make sure that all of the Scenes have been added to the build.

Go to File > Build Settings

Make sure that all of the Scenes you are going to use are added.

The buttons should now all work.

Press the play button to test them. Note the Exit button will not work in the editor. You will need to create a build for that.

You might also like