Singleton for Unity by Night Train Code
- 🚀 Performance
- 🔒 Thread safety
- 💫 Persistence supported
- 🛡️ Protection against misuse
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",
You can also clone the code into your Unity project.
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.
}
}
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>
.