Singleton Instance - BNS-MarkUlrich/MarksMagicToolbox GitHub Wiki
SingletonInstance Script Documentation
Overview
The SingletonInstance script is designed to create a singleton pattern for MonoBehaviour components in Unity. It ensures that only one instance of a specified component exists within the scene.
Class: SingletonInstance<T>
This is an abstract class that inherits from MonoBehaviour and implements the singleton pattern.
Type Parameters
T: The type of component that this singleton pattern is applied to.
Public Members
Instance |
|
| Type |
T |
| Description |
Gets the singleton instance of the specified component type. |
| Usage |
Access this property to retrieve the singleton instance of the component. |
Methods
InitSingletonInstance() |
|
| Type |
protected virtual T |
| Description |
Initializes the singleton instance. |
| Returns |
The singleton instance of the component. |
| Usage |
Override this method to perform additional initialization tasks for the singleton instance. |
OnEnable() |
|
| Type |
protected virtual void |
| Description |
Called when the MonoBehaviour becomes enabled. |
| Usage |
Override this method to trigger the initialization of the singleton instance upon enabling. |
Usage
- Inheritance: Inherit from this class and specify the component type
T.
- Accessing Instance: Use the
Instance property to access the singleton instance of the specified component.
Example
using UnityEngine;
public class MySingletonComponent : SingletonInstance<MySingletonComponent>
{
// Additional members and functionality specific to MySingletonComponent
// ...
}