Supports Unity's simple Singleton Behavior.
Create a script that extends SingletonMonoBehaviour. Specify your own class name for the generic T.
using UnityEngine;
using Pspkurara.Singleton;
public class SampleSingleton : SingletonMonoBehaviour<Sample>
{
public int InspectorValue = 1;
}
public class SampleAccess : MonoBehaviour
{
private void Start()
{
Debug.Log(SampleSingleton.instance.InspectorValue); // output: 1
}
}
If you inherit anything with "Internal", only the inherited class has access to ".instance". You can implement static wrapper members to make them look like constants.
using UnityEngine;
using Pspkurara.Singleton;
public class SampleSingleton : InternalSingletonMonoBehaviour<Sample>
{
// raw value.
[SerializeField]
private int m_InspectorValue = 1;
// static wrapper.
public static int Value { get { return instance.m_InspectorValue; } }
}
public class SampleAccess : MonoBehaviour
{
private void Start()
{
Debug.Log(SampleSingleton.Value); // output: 1
}
}
Go to Unity's project folder on the command line and call:
openupm add com.pspkurara.singleton
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
{
"dependencies": {
"com.pspkurara.singleton": "https://github.com/pspkurara/singleton.git#upm",
...
},
}
Unity 2018.1 or later
May work in Unity5, but unofficial.
- GitHub page : https://github.com/pspkurara/singleton