Skip to content

pspkurara/singleton

Repository files navigation

Singleton

Supports Unity's simple Singleton Behavior.

Usage

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
	}
}

Full API references

Installation

Using OpenUPM

Go to Unity's project folder on the command line and call:

openupm add com.pspkurara.singleton

Using Unity Package Manager (For Unity 2018.3 or later)

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",
    ...
  },
}

Requirement

Unity 2018.1 or later
May work in Unity5, but unofficial.

License

Author

See Also

About

Supports Unity's simple Singleton Behavior.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages