Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samethope/the-locator
A service locator implementation for Unity with consideration for gameobjects and their hierarchies, scenes and the global scope.
https://github.com/samethope/the-locator
servicelocator unity
Last synced: about 1 month ago
JSON representation
A service locator implementation for Unity with consideration for gameobjects and their hierarchies, scenes and the global scope.
- Host: GitHub
- URL: https://github.com/samethope/the-locator
- Owner: SametHope
- License: mit
- Created: 2024-06-17T14:26:47.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-09-08T17:21:09.000Z (4 months ago)
- Last Synced: 2024-09-09T03:52:34.347Z (4 months ago)
- Topics: servicelocator, unity
- Language: C#
- Homepage:
- Size: 465 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# TheLocator
A service locator implementation for Unity with that supports multiple services of a single type and has consideration for gameobjects, scenes and the global scope.## About
This implementation of the pattern defines containers/scopes for gameobjects, scenes and the global scope. It allows the user to get, register or unregister services within these scopes. An optional name can also be provided to later identify a service beside its type, allowing multiple instances of a single type to be registered. Getting of a service is delegated to higher level scopes if a lower one fails.This package does not create any extra overhead and uses regular C# objects. It integrates into Unity and initializes itself with the usage of the `RuntimeInitializeOnLoadMethod` attribute. EnterPlaymodeOptions are also supported as the static fields are overridden and initialized automatically when required.
## Installation
Having the necessary scripts on the project is all that is needed. This package is best installed with the Unity Package Manager via the git URL.1. Open the Unity Package Manager.
2. Select "Add package from git URL..."
3. Paste ` https://github.com/SametHope/the-locator.git#main ` as the URL.
4. Done.### Optional
I also highly recommend installation of the [Unity3D-SerializableInterface](https://github.com/Thundernerd/Unity3D-SerializableInterface) package together with this package. It's installation requires more work than this one. If you have NPM and openupm-cli, follow instructions on the repo above, otherwise follow the guide below.1. Make sure there are no compilation errors on the project.
2. Download the [Installer .unitypackage](https://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.serializableinterface?registry=https://package.openupm.com).
3. Import the whole package. This will trigger the installation of the actual package.
4. Done.## Usage
Services must be manually added and removed as one would expect.### Register Services
```cs
// Register a service for this gameobject via its component, itself, its scene and the global scope in order
Locator.Register(serviceObject, this);
Locator.Register(serviceObject, gameObject);
Locator.Register(serviceObject, gameObject.scene);
Locator.Register(serviceObject);// Do the same but with a named service
Locator.Register(serviceObject, this, "namedService");
Locator.Register(serviceObject, gameObject, "namedService");
Locator.Register(serviceObject, gameObject.scene, "namedService");
Locator.Register(serviceObject, "namedService");// Use functions with Try prefix to not get exceptions if the operation fails
Locator.TryRegister(serviceObject, this);
Locator.TryRegister(serviceObject, this, "namedService");
...
```### Unregister Services
```cs
// Unregister a service for this gameobject via its component, itself, its scene and the global scope in order
Locator.Unregister(this);
Locator.Unregister(gameObject);
Locator.Unregister(gameObject.scene);
Locator.Unregister();// Do the same but with a named service
Locator.Unregister(this, "namedService");
Locator.Unregister(gameObject, "namedService");
Locator.Unregister(gameObject.scene, "namedService");
Locator.Unregister("namedService");// Use functions with Try prefix to not get exceptions if the operation fails
Locator.Unregister(this);
Locator.TryUnregister(serviceObject, this, "namedService");
...
```### Get Services
```cs
// Get a service for this gameobject via its component, itself, its scene and the global scope in order
Locator.Get(this);
Locator.Get(gameObject); // Will check the scene if service is not found
Locator.Get(gameObject.scene);// Will check the global scope if service is not found
Locator.Get();// Do the same but with a named service
Locator.Get(this, "namedService");
Locator.Get(gameObject, "namedService");
Locator.Get(gameObject.scene, "namedService");
Locator.Get("namedService");// Use functions with Try prefix to not get exceptions if the operation fails
Locator.TryGet(this, out var service);
Locator.TryGet(this, out var namedService, "namedService");
...
```### Misc
```cs
// If type is not provided it is assumed, these are also valid signatures
Locator.Register(serviceObject, gameObject.scene);
Locator.Unregister(serviceObject);
Locator.TryGet(this, out IExampleService namedService, "namedService");
...
```### Documentation
All functions have XML documentation without exception. The `Locator` class even has a short summary of usage.
![alt text](Images/1.png)
![alt text](Images/3.png)### Tests
There are about 300 lines of tests that cover all functionality.
![alt text](Images/2.png)