Skip to content

4. Mini Game Scripts

Raphaël Tétreault edited this page Oct 27, 2025 · 19 revisions

Overview

This project provides scripts to create mini game gameplay loop. It provides a replacement to MonoBehaviour which has more detailed event functions that map into a consistent mini game loop. The MiniGameBehaviour is the class to derive in this case. The MiniGameManager is the script that manages all behaviour scripts.

There are a number of supporting scripts, too. These will be cover in brief. To see all files, review the following directory: Assets/Scripts


Mini Game Loop

The left half in orange shows the how the functions on the right are trigger in a mini game. Some are automatic based on familiar Unity MonoBehaviour events, while others are timed, and more still manually triggered (either optionally or required).


Mini Game Manager

File: Assets/Scripts/MiniGameBehaviour.cs

This script needs to be attached to an object in your mini game scene as it drives all mini game events.

Notable Fields

You should familiarize yourself with the fields of the Mini Game Manager. Listed here are notable fields.

  • State: These variables cannot be set and serve to help debug the state of the mini game manager.
  • Debug: There are some debug settings. Debug Mode, when enabled, let's you put in a custom time for the mini game. It also speeds up the countdown when starting the game to get you to testing more quickly.
  • Parameters: These settings configure the mini game. You can assign a custom start message, set the game time (based on allowed presets), and view the current game winner (though that should be set in code unless debugging).
  • Metadata: The fields provided here let you input your team member's preferred names. In a future build, these will appear in credits.

Mini Game Behaviour

File: Assets/Scripts/MiniGameManager.cs

This file acts as a replacement for MonoBehaviour with additional events (listed in the above diagram).

Creating New Mini Game Scripts

To make use of the MiniGameBehaviour script, we must inherit from it as we would a Unity MonoBehaviour. As illustrated above, a MiniGameBehaviour derives from MonoBehaviour so has all its functions plus additional ones.

// Make sure to place your script in the proper namespace as discuss on XXX
namespace MiniGameCollection.Games2025.Team00
{
  public class Example
  {
    // Mini game time initialized to value maximum game time.
    protected override void OnTimerInitialized(int maxGameTime) { }

    // The 3-2-1-<message> countdown events.
    protected override void OnCountDown(string message) { }
  
    // Event signaling mini game start.
    protected override void OnGameStart() { }

    // Event signaling time remaining.
    // Occurs every MonoBehaviour.Update.
    protected override void OnTimerUpdate(float timeRemaining) { }

    // Event signaling time remaining.
    // Occurs once per second on whole number change.
    protected override void OnTimerUpdateInt(int timeRemaining) { }

    // Event signaling mini game end.
    protected override void OnGameEnd() { }

    // Event signaling mini game winner.
    protected override void OnGameWinner(MiniGameWinner winner) { }

    // Event signaling mini game terminated.
    protected override void OnGameClose() { }
  }
}

The MiniGameBehaviour makes use of MonoBehaviour's OnEnable, OnDisable, and Reset functions. If you need to use these functions, you must override them using the override keyword.

namespace MiniGameCollection.Games2025.Team00
{
  public class Example
  {
    protected override void OnEnable { }
    protected override void OnDisable { }
    protected override void Reset { }
  }
}

Starting and Stopping Mini Game Loop

Mini Game Kicker

Assets/Scripts/MiniGameManagerKicker.cs

This is a utility script to automatically start the mini game when your scene loads rather than having to have another script call MiniGameManager.StartGame() manually.

Determining Win/Lose For Players

The game will end either when time runs out or when the MiniGamManager.StopGame() function is called. Your code must assign a winner before the OnGameWinner event triggers shortly after the game ends. To do this, assign the MiniGameManager.Winner property directly at any time, though the MiniGameBehaviour.OnGameEnd() function is a great place to do that as it is called on the game ends and can decide a victor before the OnGameWinner event triggers.

namespace MiniGameCollection.Games2025.Team00
{
  public class Example
  {
    // Assigned in inspector
    [field: SerializeField]
    public MiniGameManager MiniGameManager { get; private set; }

    protected override void OnGameEnd()
    {
      // Rigged. Better code would actually decide the winner.
      MiniGameManager.Winner = MiniGameWinner.Draw;
    }
  }
}

You can consult the MiniGameWinner enumeration for all possible options.

Clone this wiki locally