Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Main Menu and Difficulty Functionality | Fundamentals of UI
Unity for Beginners

bookMain Menu and Difficulty Functionality

Sveip for å vise menyen

Creating a Main Menu with Play, Quit, and Difficulty Options

Before players can interact with your game, a main menu gives them control over starting, quitting, and choosing difficulty.

Buttons: Play and Quit

  • create a MainMenu scene and add a Canvas;
  • add Button - TextMeshPro for each option (e.g. Play, Quit);
  • create a UIControls script (attached to the Canvas or another GameObject).

In the script:

using UnityEngine.SceneManagement;

public void PlayButton()
{
   SceneManager.LoadScene("Level"); // load main game scene
}

public void QuitButton()
{
   Application.Quit(); // quit application
}

Assign these public functions to the buttons in the Inspector:

  • Play button -> PlayButton();
  • Quit button -> QuitButton().

Result: clicking buttons performs the expected actions.

Adding Difficulty Options

  • add a Dropdown - TextMeshPro to the menu;
  • rename options (e.g. Easy, Hard, Improbable).

In the UIControls script, include:

using TMPro;

public TMP_Dropdown difficultyDropdown;

public void DifficultyDropdownChanged()
{
   PlayerPrefs.SetInt("Difficulty", difficultyDropdown.value);
}
  • assign the dropdown in the Inspector;
  • configure the dropdown to call DifficultyDropdownChanged() when its value changes.

Persisting Difficulty

To remember the difficulty between scene loads:

void Start()
{
   difficultyDropdown.value = PlayerPrefs.GetInt("Difficulty");
}

This ensures the dropdown reflects the currently saved difficulty.

Applying Difficulty in the Game

In the Level scene, adjust gameplay based on the saved difficulty:

int difficulty = PlayerPrefs.GetInt("Difficulty");

if(difficulty == 0) Time.timeScale = 1f; // Easy
else if(difficulty == 1) Time.timeScale = 1.5f; // Hard
else if(difficulty == 2) Time.timeScale = 2f; // Improbable

Time.timeScale affects the speed of all game physics and actions.

Optionally, you could also multiply score values or other mechanics to further differentiate difficulty.

1. How do you make a Play button start the game when clicked in Unity's main menu?

2. How do you make a dropdown selection persist between scene loads in Unity?

3. After assigning the UIControls script to the Canvas, what must you do in the Inspector to make the Play and Quit buttons call the correct functions?

question mark

How do you make a Play button start the game when clicked in Unity's main menu?

Velg det helt riktige svaret

question mark

How do you make a dropdown selection persist between scene loads in Unity?

Velg det helt riktige svaret

question mark

After assigning the UIControls script to the Canvas, what must you do in the Inspector to make the Play and Quit buttons call the correct functions?

Velg det helt riktige svaret

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Seksjon 6. Kapittel 2
some-alt