Table of Contents
The singleton pattern is a way to ensure a class has only a single globally accessible instance available at all times. Behaving much like a regular static class but with some advantages. This is very useful for making global manager type classes that hold global variables and functions that many other classes need to access.
This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.
- Clone the repo
git clone https://github.com/bmoglu/singleton.git
- Import package to your project
To make any class a singleton, simply inherit from the Singleton base class instead of MonoBehaviour, like so:
public class MySingleton : Singleton<MySingleton>
{
// Then add whatever code to the class you need as you normally would.
public string MyAwesomeString = "Hello world!";
}
Now you can access all public fields, properties and methods from the class anywhere using ClassName.Instance:
public class MyAwesomeClass : MonoBehaviour
{
private void OnEnable()
{
Debug.Log(MySingleton.Instance.MyAwesomeString);
}
}
Distributed under the MIT License. See LICENSE
for more information.