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 initializationA
SingletonBuilderProfile
ScriptableObject for maintaining singleton configurationsA base
Singleton<T>
class for creating persistent singleton objects
How the Singleton Builder 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:
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.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.
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
andkillSingletonsOnSceneExit
flags, allowing developers to toggle cleanup behavior as needed.
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.
Finalization Once all singletons are loaded and initialized, the builder logs a confirmation (
"All Singletons is Loaded"
) to indicate that the process is complete.
This singleton solution assumes all singletons being exists in hierarchy and alive in all scenes. If your system need spawn and destroy other singletons, don't add those in the profile.
Create Singleton Profile
create a profile in your asset. Project window > + icon > Profile > Singleton Profile
Create Singleton Builder
create an Empty GameObject in hierarchy
rename as Singleton Builder
add component > AceLand > Tool > Singleton Builder
drag the Singleton Profile to the profile field.
drag the GameObject to asset to make it as a prefab.
remain the GameObject.
Create Singleton Object
create your singleton components.
create an Empty GameObject in Hierarchy.
add the singleton component.
drag the GameObject to asset to make it as a prefab.
delete the GameObject in hierarchy.
drag the prefab to the list in Singleton Profile.
Usage
add the SingletonBuilder prefab to hierarchy in EVERY scene.
Last updated