Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Main Menu and Difficulty Functionality | Fundamentals of UI
Unity for Beginners

bookMain Menu and Difficulty Functionality

Свайпніть щоб показати меню

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?

Виберіть правильну відповідь

question mark

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

Виберіть правильну відповідь

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?

Виберіть правильну відповідь

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 6. Розділ 2

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Секція 6. Розділ 2
some-alt