A lightweight service locator utility for Unity to register, manage, and retrieve services, including MonoBehaviour. https://useffarahmand.com/
Find a file
2024-10-23 22:16:54 +03:30
Runtime implement ServiceLocator and Sample 2024-10-23 22:03:24 +03:30
Sample implement ServiceLocator and Sample 2024-10-23 22:03:24 +03:30
.gitignore Initial commit 2024-10-23 21:05:38 +03:30
LICENSE add License 2024-10-23 22:15:22 +03:30
LICENSE.meta add License 2024-10-23 22:15:22 +03:30
package.json Update package.json 2024-10-23 22:06:48 +03:30
package.json.meta implement ServiceLocator and Sample 2024-10-23 22:03:24 +03:30
README.md Create README.md 2024-10-23 22:16:54 +03:30
Runtime.meta implement ServiceLocator and Sample 2024-10-23 22:03:24 +03:30
Sample.meta implement ServiceLocator and Sample 2024-10-23 22:03:24 +03:30

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:

ServiceLocator.RegisterService<IService>(myServiceInstance);

For lazy instantiation:

ServiceLocator.RegisterService<IService>();

Retrieving a Service 🔍

var myService = ServiceLocator.GetService<IService>();

Unregistering a Service 🧹

ServiceLocator.UnregisterService<IService>();

Example 💡

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>();

License 📄

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing 🤝

Contributions are welcome! Please submit a pull request or open an issue for discussion.