Singleton - BluePixelDev/utilkit GitHub Wiki

The Singleton<T> class provides a generic base class for creating singleton instances in Unity. This implementation ensures that only one instance of a class exists at any given time, and it allows that instance to persist across scene loads. By default, it is initialized before any other scripts.

This class uses Unity's MonoBehaviour to manage the instance, and the singleton instance is accessible through the Instance property. The DontDestroyOnLoad function ensures that the singleton persists across scene transitions.

Features

  • Ensures single instance: Only one instance of the class will exist at a time.
  • Automatic initialization: The singleton instance is created and initialized automatically in the Awake method.
  • Persistent across scenes: The instance is not destroyed when loading new scenes.
  • Custom initialization: Provides a method OnAwake that can be overridden in derived classes for additional setup.
  • Default execution order: The DefaultExecutionOrder(-1000) attribute ensures that the singleton is initialized before any other scripts.

Usage

To use this class, create a new class that inherits from Singleton<T> where T is the type of the derived class.

Example:

using UnityEngine;

namespace BP.Utilkit
{
    public class GameManager : Singleton<GameManager>
    {
        // Custom initialization logic here
        protected override void OnAwake()
        {
            base.OnAwake();
            Debug.Log("GameManager Initialized");
        }

        // Add other methods or properties for the GameManager
    }
}

Key Points:

  • Instance Property: Access the singleton instance via GameManager.Instance.
  • OnAwake Method: You can override OnAwake to perform any additional initialization required by the derived class.
  • DontDestroyOnLoad: The instance will automatically persist across scene transitions due to DontDestroyOnLoad.

How It Works

  1. Awake Method: When the singleton class's Awake method is called, it checks if an instance of the class already exists. If not, it assigns the current object as the instance and ensures it doesn't get destroyed when loading new scenes with DontDestroyOnLoad(gameObject). If an instance already exists, it destroys the new object to maintain the singleton pattern.

  2. DefaultExecutionOrder: The [DefaultExecutionOrder(-1000)] attribute ensures that the singleton is initialized before any other scripts. This guarantees that the singleton instance is available early in the execution cycle.

  3. Singleton Inheritance: The Singleton<T> class is abstract, meaning it cannot be instantiated directly. It must be inherited by a concrete class that provides the specific functionality for that instance.

Notes

  • This implementation assumes that you want to use a singleton that is persisted across scenes and is initialized before any other scripts in the scene.
  • If you need to handle cleanup or other operations when the singleton is destroyed, you can override the OnDestroy method in your derived class.
  • Make sure that the Instance is accessed in a safe manner to avoid any potential null reference exceptions (i.e., avoid accessing it before it has been initialized).