Defining a Local Service
In addition to global services – which are guaranteed to be available to all clients for the entire lifetime of the application – Init(args) also supports defining local services using the Service Tag feature.
Local services live as part of scene and prefab assets, and can have a more limited lifespan and availability than global services.
You can turn any component into a local service by selecting Make Service Of Type… from its context menu.
This will cause a dropdown menu to open, asking you to specify the defining types of the service.
After you’ve selected the defining types, a blue Service Tag will appear in the header of the component in the Inspector. This acts as a marker to let you easily know which components are services, and also gives you access to some useful functionality when you left or right-click it.
If you left-click the Service Tag the Defining Type menu will open again.
If you click the previously selected defining type of the service again, the service tag will be removed and the component will no longer be a service.
You can add more than one defining types for one component by selecting multiple options in the dropdown menu.
If you right-click the Service Tag a context menu will open, giving you access to additional commands.
Set Availability…
If you pick the Set Availability… menu item then a dropdown menu will open, letting you choose one of the following options to define which clients will have access to the service:
- In GameObject – Only clients that are attached to the same GameObject as this component can receive services from it.
- In Children – Only clients that are attached to the same GameObject as this component, or any of its children (including nested children), can receive services from it.
- In Parents – Only clients that are attached to the same GameObject as this component, or any of its parents (including nested parents), can receive services from it.
- In Hierarchy Root Children – Only clients that are attached to the GameObject which is at the root of this component’s hierarchy, or any of the children of the root (including nested children), can receive services from it.
- In Scene – Only clients belonging to the same scene as this component can receive services from it.
- In All Scenes – All scene objects can receive services from this component, regardless of which active scene they belong to.
- Everywhere – All clients can receive services from this component, regardless of of which active scene they belong to, or if they belong a scene at all.
Find Defining Object
Pick the Find Defining Object menu item to highlight the Object that defines the service in the Hierarchy or Project view.
If the service is defined by the ServiceAttribute, then the script asset that contains the attribute will be highlighted.
If the service is defined by a Services component, then the GameObject that contains the Services component will be highlighted.
If the service is defined by a Service Tag added via the Make Service Of Type… menu item then Find Defining Object will not appear in the menu.
Find Clients In Scenes
Pick the Find Clients In Scenes menu item to select all objects in the scene hierarchies that depend on the service.