UService is a lightweight service locator utility for Unity, designed to manage and retrieve service instances efficiently. The tool allows for the easy registration, retrieval, and management of services, including MonoBehaviour services which must be attached to GameObject.
- 🔧 Service Registration: Register services either by providing an instance or through lazy instantiation.
- 🎮 MonoBehaviour Management: Handles
MonoBehaviourservices by attaching them to UnityGameObjects. - 🔍 Service Retrieval: Retrieve registered services with lazy instantiation.
- 🧹 Unregister Services: Clean up services when they are no longer needed.
- Clone or download the repository.
- Add the
ServiceLocatorscript to your Unity project. - Make sure the namespace
UServiceis referenced in your code.
To register an existing instance of a service:
ServiceLocator.RegisterService<IService>(myServiceInstance);For lazy instantiation:
ServiceLocator.RegisterService<IService>();var myService = ServiceLocator.GetService<IService>();ServiceLocator.UnregisterService<IService>();Here is a simple usage example:
// Register a new service
ServiceLocator.RegisterService<MyService>();
// Retrieve the registered service
MyService service = ServiceLocator.GetService<MyService>();
// Unregister the service when it's no longer needed
ServiceLocator.UnregisterService<MyService>();This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please submit a pull request or open an issue for discussion.