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
SingletonBuildercomponent for managed instantiation and scene initializationA
SingletonBuilderProfileScriptableObject for maintaining singleton configurationsA base
SingletonBehaviour<T>class for creating persistent singleton objects
Package Info
display name
AceLand Singleton
package name
latest version
1.0.0
namespace
git repository
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:
Profile-Based Configuration The Singleton Builder relies on a SingletonBuilderProfile, a
ScriptableObjectthat 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
Awakephase 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
onSceneExitActionsandkillSingletonsOnSceneExitflags, 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.
How to Build
Create Singleton Profile
create a profile in your asset. Project window > + icon > AceLand > Singleton Profile
Create Singleton Builder
create a Singleton Builder Object in Hirarchy
from menu -
GameObject>AceLand>Create Singleton Builderin hierarchy - right click >
AceLand>Create Singleton Builder
drag the Singleton Profile to the profile field.
drag the Singleton Builder Object to asset to make it as a prefab.
remain the GameObject.
Create Singleton Object
create your singleton Behaviour 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.
Setup the System
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:
create a new Singleton Profile contains those objects
confirm those singleton not exists in other profile in existing builder
create an other Singleton Builder for the scene
set
Exit BehaviourtoKill All Singnleton Objectsset the profile created in step 1 to the builder
Last updated