Singleton <T>
Description
The singleton pattern is essentially a class which only allows a single instance of itself to be created and usually gives simple access to that instance. 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.
Static Properties
Property | Function |
Instance | Access singleton instance through this propriety. |
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | using UnityEngine;
// Definition
public class Manager : Singleton<Manager>
{
public void Foo()
{
Debug.Log("Foo");
}
}
// Usage
public class ExampleClass : MonoBehaviour
{
private void Start()
{
Manager.Instance.Foo();
}
}
|