Main Menu

Preface

In this lesson we will focus on creating the main menu for our roguelike game. While I will make every effort to keep this unit is up to date you can always view the newest release of this project on its GitHub page. Remember this lesson assumes that you know some of the basics of Unity, things like creating projects will not be explained in this lesson.

The roguelike main menu.

User Interface

  • Create a new Unity Project.
  • In the Hierarchy create a UI > Panel.See Image
  • Rename the GameObject “Canvas” >> “MenuUI” and “Panel” >> “MainMenu”.See Image
  • In the Hierarchy create a UI > Button under the “MainMenu”.See Image
  • Rename the newly created Button to “StartButton”. Resize the Button‘s Width = 160 and Height = 50.See Image
  • In the Hierarchy expand the Button and select the Text GameObject. While having the Button selected in the Inspector change the Text (Script) textbox to “START”.See Image
  • In the Hierarchy create a UI > Text under the “MainMenu”.See Image
  • Rename the newly created Text to “Title”. While having the Text selected in the Inspector change the Text (Script) textbox to “Your Game Title”.See Image
  • Rearrange the “Title” Text and “StartButton” Button on the Scene to your preferred positions.See Image

Coding

  • Create a new Script called “MainMenu” and place it under the “Scripts” folder.
  • Attach the newly created Script to “MenuUI”.
  • Remove the Start() and Update() methods from the script.
  • The code for the MainMenu.cs can be seen below, pay close attention to the hightlighted areas:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainMenu : MonoBehaviour {
	public void StartGame() {
		SceneManager.LoadScene(1);
	}
}
  • Line 4: This gives access to Scene Management library.
  • Line 7-10: A custom method that will allow the “StartButton” Button to load the next level.

Attaching the Method

  • In the Hierarchy select the “StartButton” and scroll down in the Inspector until the Button (Script) is visible.See Image
  • Drag the “MenuUI” GameObject in the Hierarchy to the None (Object) in the On Click() element of the Button (Script).See Image
  • Selection the dropdown, currently labelled No Function, and select MainMenu >> StartGame().See Image
Mark as Complete
chevron_right