Singleton

A singleton management system

A robust and flexible singleton management system that simplifies the creation and lifecycle management of persistent objects across scenes.

The system consists of three main components:

  • A SingletonBuilder component for managed instantiation and scene initialization

  • A SingletonBuilderProfile ScriptableObject for maintaining singleton configurations

  • A base SingletonBehaviour<T> class for creating persistent singleton objects

Package Info

display name

AceLand Singleton

package name

latest version

1.0.0

namespace

dependencies


How It Works

The Singleton Builder is a powerful tool designed to streamline the management and lifecycle of singleton objects in your Unity project. It provides automated instantiation, initialization, and cleanup of singletons across scenes, ensuring robust and consistent behavior for persistent objects. Here's a breakdown of how it works:

  1. Profile-Based Configuration The Singleton Builder relies on a SingletonBuilderProfile, a ScriptableObject that defines and manages a list of singleton prefabs. This profile allows you to centralize the configuration of all singletons in your project, making it easy to control their behavior and lifecycle.

  2. Runtime Initialization During the Awake phase of the Singleton Builder, the following processes occur:

    • Existing Singleton Detection: The builder scans the scene for any active singleton objects to avoid duplicate instantiations.

    • Singleton Instantiation: If a singleton defined in the profile is missing from the scene, the builder instantiates it from the provided prefab, initializes it with its SceneInit() method, and ensures its persistence across scenes.

    • Logging & Debugging: Each step of the process is logged, providing detailed feedback for debugging and verification.

  3. Scene Transition Management

    • The Singleton Builder optionally supports cleanup of singletons on scene transitions. When configured, singletons listed in the profile can be destroyed during the scene unload process to ensure a clean state for the next scene.

    • This behavior is controlled via onSceneExitActions and killSingletonsOnSceneExit flags, allowing developers to toggle cleanup behavior as needed.

  4. Centralized Lifecycle Management By using the Singleton Builder, developers no longer need to manually manage the instantiation and destruction of singleton objects. The tool ensures that singletons are:

    • Loaded only when necessary.

    • Properly initialized for each scene.

    • Removed if configured to do so on scene transitions.

  5. Finalization Once all singletons are loaded and initialized, the builder logs a confirmation ("All Singletons is Loaded") to indicate that the process is complete.


How to Build

Create Singleton Profile

  1. create a profile in your asset. Project window > + icon > AceLand > Singleton Profile

Create Singleton Builder

  1. create a Singleton Builder Object in Hirarchy

    1. from menu - GameObject > AceLand > Create Singleton Builder

    2. in hierarchy - right click > AceLand > Create Singleton Builder

  2. drag the Singleton Profile to the profile field.

  3. drag the Singleton Builder Object to asset to make it as a prefab.

  4. remain the GameObject.

Create Singleton Object

  1. create your singleton Behaviour components.

  2. create an Empty GameObject in Hierarchy.

  3. add the singleton component.

  4. drag the GameObject to asset to make it as a prefab.

  5. delete the GameObject in hierarchy.

  6. drag the prefab to the list in Singleton Profile.


Setup the System

  1. add the SingletonBuilder prefab to hierarchy in EVERY scene.


Other Settings

In Singleton Builder, there is a setting Exit Behaviour under Scene section.

On case of some singleton objects being exists in current scene only:

  1. create a new Singleton Profile contains those objects

    1. confirm those singleton not exists in other profile in existing builder

  2. create an other Singleton Builder for the scene

  3. set Exit Behaviour to Kill All Singnleton Objects

  4. set the profile created in step 1 to the builder


Last updated