Services
Use a [RegisterService]
attribute to make any class a global singleton that you can locate by either type or interface.
So this can be a singleton:
using BeatThat.Service;
[RegisterService] // triggers Services to instantiate and register this class
public class Foo : MonoBehaviour, Bar
{
// implementation
}
…and you can then find the singleton by its type
using BeatThat.Service;
public class MyComponent : MonoBehaviour
{
void Start()
{
var foo = Services.Require<Foo>();
}
}
…or you can also find the singleton by its interfaces
using BeatThat.Service;
public class MyComponent : MonoBehaviour
{
void Start()
{
var bar = Services.Require<Bar>(); // since Foo implements Bar
}
}
…services also support dependency injection through this add on package: dependency-injection
For code and documentation see services.