Service API

  08. Reference No Comments

The Service class contains methods that can be used to manually register service objects and retrieve them for clients.

This is lower level API. You typically don’t need to use it directly, but should instead use the [Service] attribute, Service Tag and Services component to register services, and create a class that derives from MonoBehaviour<T…> to retrieve them automatically.

Service.Set

The Service.Set method can be used to register an object as a global service.

The generic type argument of the method determines the defining type for the service, which clients can use to retrieve the instance.

Service.Set<IPlayer>(player);

Service.Unset

The Service.Unset method can be used to register a global service.

The generic type argument of the method specifies the defining type of the service to unset.

Service.Unset<IPlayer>();

Service.AddFor

The Service.Addfor method can be used to register an object as a local service.

The generic type argument of the method determines the defining type for the service, which clients can use to retrieve the instance.

The clients argument specifies which clients have access to the service, based on their location in the scene hierarchies.

The last argument determines the origin point of the service, which is used when determining whether or not a particular client has access to the service in question.

Service.AddFor<IPlayer>(Clients.InChildren, player, player.transform);

Service.RemoveFrom

The Service.RemoveFrom method can be used to unregister a local service.

The generic type argument of the method specifies the defining type of the service to unset.

Service.RemoveFrom<IPlayer>(Clients.InChildren, player, player.transform);

Service.TryGet

The Service.TryGet method can be used by a client object to try and retrieve a global service previously registered using Service.Set or Service.AddFor with a clients value of Everywhere.

if(Service.TryGet(out Player player))
{
    Init(player);
}

Service.TryGetFor

The Service.TryGetFor method can be used by a client object to try and retrieve a local or global service previously registered using Service.Set or Service.AddFor.

if(Service.TryGetFor(this, out Player player))
{
    Init(player);
}

Leave a Reply

Your email address will not be published. Required fields are marked *