Skip to content

MeeXaSiK/Singleton

Repository files navigation

🚄 Singleton

License Version Unity

Singleton for Unity by Night Train Code

  • 🚀 Performance
  • 🔒 Thread safety
  • 💫 Persistence supported
  • 🛡️ Protection against misuse

▶ Installation

Supports installation as a Unity module via a git link in the PackageManager

https://github.com/MeeXaSiK/Singleton.git

or direct editing of Packages/manifest.json:

"com.nighttraincode.singleton": "https://github.com/MeeXaSiK/Singleton.git",

As source

You can also clone the code into your Unity project.

🔸 How to use

Inherit a class from Singleton<T> where T : Singleton<T>

public class Player : Singleton<Player>
{

}

Get instance using Instance property.

_player = Singleton<Player>.Instance;
_player = Player.Instance;

If you need Awake method, use OnAwake instead.

public class Player : Singleton<Player>
{
    protected override void OnAwake()
    {
        // Your code here.
    }
}

🔸 Persistence

If you want to make the singleton persistent, then enable the Dont Destroy On Load option in the inspector of class, that inherited from Singleton<T>.