Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Create Main Menu | Improve the Game
Fighting Game in Unity

book
Create Main Menu

Main Menu

Script for Main Menu

csharp
using UnityEngine;
using UnityEngine.SceneManagement;

public class MainUI : MonoBehaviour
{
public void StatTheGame()
{
SceneManager.LoadScene(1);
}

public void Quit()
{
Application.Quit();
}
}

The MainUI class is a simple script that provides functionality to start the game and quit the application. This script is intended to be used with UI buttons in Unity.

Class Definition and Methods

The StatTheGame method is designed to initiate the game by loading a new scene. Specifically, SceneManager.LoadScene(1) directs Unity to load the scene with the build index 1, which corresponds to its position in the Build Settings.

The Quit method allows for the application to be closed by calling Application.Quit(), although this functionality is only effective in a built application and not within the Unity Editor.

Usage in Unity

Attaching the Script

Create a GameObject (e.g., an empty GameObject or a UI element). Attach the MainUI script to the GameObject by dragging the script onto it or using the Add Component button in the Inspector.

Setting Up UI Buttons

Create a UI button (e.g., GameObject > UI > Button). Select the button and go to the Inspector. In the On Click () section, click the + button to add a new event. Drag the GameObject with the MainUI script attached into the object field. From the dropdown, select MainUI > functionName.

Game Manager

Script for Game Manager

csharp
public class GameManager : MonoBehaviour
{
public GameStates GameState { private set; get; }
public static event System.Action<GameStates> onGameStateChanges;
public static GameManager instance;

void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
}

void Start()
{
GameState = GameStates.Playing;
onGameStateChanges?.Invoke(GameState);
}

public void FinishGame()
{
GameState = GameStates.Finished;
onGameStateChanges?.Invoke(GameState);
}
}

public enum GameStates
{
Playing,
Finished,
}

The GameManager class is responsible for managing the state of the game. It uses a singleton pattern to ensure there is only one instance of GameManager in the game, manages the game state, and broadcasts state changes using events.

Class Definition and Member Variables

Methods

Awake Method

csharp
void Awake()
{
if (instance != null)
{
Destroy(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
}

Purpose: Ensures there is only one instance of GameManager and prevents it from being destroyed when loading new scenes.

Singleton Pattern: Checks if an instance already exists. If so, it destroys the duplicate. Otherwise, it assigns the instance and marks the object to not be destroyed on loading a new scene.

Start Method

csharp
void Start()
{
GameState = GameStates.Playing;
onGameStateChanges?.Invoke(GameState);
}

Purpose: Initializes the game state to Playing when the game starts and triggers the onGameStateChanges event.

FinishGame Method

csharp
public void FinishGame()
{
GameState = GameStates.Finished;
onGameStateChanges?.Invoke(GameState);
}

Purpose: Sets the game state to Finished and triggers the onGameStateChanges event.

GameStates Enum

csharp
public enum GameStates
{
Playing,
Finished,
}

Purpose: Defines the possible states of the game.

Enemy React to Game State Changes

csharp
private void GameManager_onGameStateChanges(GameStates state)
{
if (state == GameStates.Finished)
{
stateManager.ChangeState(idle);
}
}

This method handles the game state changes. When the game state changes to Finished, the enemy's state is changed to idle.

Finish Panel

Script for Finish Panel

csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.SceneManagement;

public class FinishPanel : MonoBehaviour
{
[SerializeField] Player player;
[SerializeField] GameObject panel;
[SerializeField] TMP_Text infoText;

private void Start()
{
GameManager.onGameStateChanges += GameManager_onGameStateChanges;
}

private void GameManager_onGameStateChanges(GameStates obj)
{
if (obj == GameStates.Finished)
{
panel.SetActive(true);
OnGameFinished();
}
}

void OnGameFinished()
{
if (player.isDead)
{
infoText.text = "You lost";
infoText.color = Color.red;
}
else
{
infoText.text = "You Won";
infoText.color = Color.green;
}
}

public void BackToMainMenu()
{
SceneManager.LoadScene(0);
}

public void Quit()
{
Application.Quit();
}
}

The FinishPanel class handles the display and functionality of the finish panel that appears when the game ends. This panel provides feedback to the player on whether they have won or lost and offers options to return to the main menu or quit the game.

Explanation

Methods

Start Method

csharp
private void Start()
{
GameManager.onGameStateChanges += GameManager_onGameStateChanges;
}

Purpose: Subscribes to the GameManager.onGameStateChanges event to listen for changes in the game state.

GameManager_onGameStateChanges Method

csharp
private void GameManager_onGameStateChanges(GameStates obj)
{
if (obj == GameStates.Finished)
{
panel.SetActive(true);
OnGameFinished();
}
}

Purpose: Handles the game state changes. When the game state is Finished, it activates the finish panel and calls OnGameFinished.

OnGameFinished Method

csharp
void OnGameFinished()
{
if (player.isDead)
{
infoText.text = "You lost";
infoText.color = Color.red;
}
else
{
infoText.text = "You Won";
infoText.color = Color.green;
}
}

Purpose: Updates the finish panel's text based on whether the player is dead or alive.

Logic: If the player is dead, the method sets the text to "You lost" in red. If the player is alive, it sets the text to "You Won" in green.

BackToMainMenu Method

csharp
public void BackToMainMenu()
{
SceneManager.LoadScene(0);
}

Purpose: Loads the main menu scene (build index 0) when called. This method is intended to be linked to a button in the UI.

Quit Method

csharp
public void Quit()
{
Application.Quit();
}

Purpose: Quits the application when called. This method is intended to be linked to a button in the UI.

Summary

The FinishPanel class displays end-of-game results and offers options to return to the main menu or quit. It subscribes to GameManager.onGameStateChanges to update appropriately when the game finishes, enhancing the user experience with clear feedback and intuitive options.

1. What happens when the FinishGame method is called in the GameManager class?

2. What happens when the GameManager changes the game state to GameStates.Finished?

question mark

What happens when the FinishGame method is called in the GameManager class?

Select the correct answer

question mark

What happens when the GameManager changes the game state to GameStates.Finished?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 3

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

We use cookies to make your experience better!
some-alt