Main Menu and Difficulty Functionality
Swipe um das Menü anzuzeigen
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
UIControlsscript (attached to theCanvasor anotherGameObject).
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?
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen