AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  • Create Singleton Profile
  • Create Singleton Builder
  • Create Singleton Object
  • Usage
  1. Packages
  2. Library
  3. Mono

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 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:

  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.

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

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

Create Singleton Builder

  1. create an Empty GameObject in hierarchy

  2. rename as Singleton Builder

  3. add component > AceLand > Tool > Singleton Builder

  4. drag the Singleton Profile to the profile field.

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

  6. remain the GameObject.

Create Singleton Object

  1. create your singleton components.

    // create a singleton component
    public class YourComponent : Singleton<YourComponent>
    {
        public override void SceneInit()
        {
            // called each scene loaded
        }
        
        protected override void Awake()
        {
            base.Awake();
            // Your initialization code
        }
        
        // your stuffs
    }
  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.

Usage

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


Last updated 2 months ago