SingletonBehaviour is a generic Unity Singleton pattern implementation designed for MonoBehaviour-derived classes. This pattern ensures that only one instance of the specified MonoBehaviour type exists within the Unity scene.


Release v1.0.0

https://github.com/swzwij/Code-Crate/releases/tag/Singleton-Behaviour-v1.0.0


Installation

Importing a Unity Package:

  1. Download the Package: Locate the Unity package you want to import. This could be from the Unity Asset Store or a developer's website. Download the .unitypackage file.

  2. Import into Unity: There are two ways to import the package into your Unity project:

  3. Import Options (Optional):

    A window will appear showing the contents of the package. By default, all items are selected for import. You can uncheck any files or folders you don't want to import into your project.

  4. Click Import: Once you're happy with the selection, click Import to begin importing the package files into your project.


Usage

Singleton Instance

The Singleton instance can be accessed using the static Instance property. If no instance exists, it will create one as needed.

// Access the Singleton instance
MyMonoBehaviourSingleton instance = SingletonBehaviour<MyMonoBehaviourSingleton>.Instance;

Example

public class MyMonoBehaviourSingleton : SingletonBehaviour<MyMonoBehaviourSingleton>
{
    // Add your MonoBehaviour-specific code here
}

Notes