- Add UnityServiceLocator to project
- Add AppEntry prefab to scene
- Specify any types in the list
Dont care if service is MonoBehaviour-inherited (instance will be created on scene automatically)
- Just use Get() to get your service:
ServiceLocator.Get<ExampleService>();
UnityServiceLocator contains an application initialization system that resolves important architectural issues:
- Transparent service initialization order allows you to control the flow of application startup
- Easier to resolve problems of service codependency
- An exception of one of the services does not interrupt code execution and app launch!
- Auto instancing of monobehaviour services without the need to use prefabs
- Asynchronous initialization of services supported
Service locator is a powerful and simple architectural pattern that has a number of advantages over singleton:
- Any type can be registered
- No need to write "single instance validation" boilerplate code in each class
- Any type can be unregistered, so the static object lifecycle can be temporary