https://github.com/umodules/uservice
A lightweight service locator utility for Unity to register, manage, and retrieve services, including MonoBehaviour.
https://github.com/umodules/uservice
dependency-injection monobehaviour service-locator unity
Last synced: about 2 months ago
JSON representation
A lightweight service locator utility for Unity to register, manage, and retrieve services, including MonoBehaviour.
- Host: GitHub
- URL: https://github.com/umodules/uservice
- Owner: UModules
- License: mit
- Created: 2024-10-23T17:35:38.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-23T18:50:15.000Z (over 1 year ago)
- Last Synced: 2025-02-14T12:36:22.904Z (over 1 year ago)
- Topics: dependency-injection, monobehaviour, service-locator, unity
- Language: C#
- Homepage: https://useffarahmand.com/
- Size: 12.7 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# UService ๐
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`.
## Features โจ
* ๐ง **Service Registration:** Register services either by providing an instance or through lazy instantiation.
* ๐ฎ **MonoBehaviour Management:** Handles `MonoBehaviour` services by attaching them to Unity `GameObjects`.
* ๐ **Service Retrieval:** Retrieve registered services with lazy instantiation.
* ๐งน **Unregister Services:** Clean up services when they are no longer needed.
## Installation ๐ฆ
* Clone or download the repository.
* Add the `ServiceLocator` script to your Unity project.
* Make sure the namespace `UService` is referenced in your code.
## Usage ๐
### Registering a Service ๐ ๏ธ
To register an existing instance of a service:
```C#
ServiceLocator.RegisterService(myServiceInstance);
```
For lazy instantiation:
```C#
ServiceLocator.RegisterService();
```
### Retrieving a Service ๐
```C#
var myService = ServiceLocator.GetService();
```
### Unregistering a Service ๐งน
```C#
ServiceLocator.UnregisterService();
```
## Example ๐ก
Here is a simple usage example:
```C#
// Register a new service
ServiceLocator.RegisterService();
// Retrieve the registered service
MyService service = ServiceLocator.GetService();
// Unregister the service when it's no longer needed
ServiceLocator.UnregisterService();
```
## License ๐
This project is licensed under the MIT License. See the [LICENSE](https://github.com/UModules/UService/blob/main/LICENSE) file for details.
## Contributing ๐ค
Contributions are welcome! Please submit a pull request or open an issue for discussion.