Author : Timo Naskali

Initializers (and Any<TValue> fields) support cross-scene references. To create a cross-scene reference, open two scenes in the Editor, and drag-and-drop an component from one into an Initializer in another one. This will make Init(args) automatically create a globally unique identifier (Guid) for the dragged Object, and assign a value provider into the Initializer argument field, which ..

Read more

Value providers are a highly flexible system that can be used to provide Init arguments to initializers dynamically at runtime. Creating Value Providers To make a new value provider, create a ScriptableObject that implements IValueProvider<TValue> or IValueByTypeProvider. In order to be able to create assets from your value provider, you’ll also need to add the [CreateAssetMenu] ..

Read more

Issue Due to a limitation with Unity’s serialization system, embedded value providers cannot be serialized directly as part of prefab assets. Solution 1: Create Asset You can get around this limitation by adding the [CreateAssetMenu] attribute to your value provider, and creating assets to back them. You can then drag-and-drop the value provider assets into ..

Read more

Issue You have a base class that depends on some services. You want to create a class that derives from that base class, and depends on some additional services on top of the ones that the base class does. class Base : MonoBehaviour { // The base class itself could depend on some services A ..

Read more

Represents an object that can provide values of requested types on demand. A scriptable object or component that implements the IValueByTypeProvider interface can be assigned to an Initializer field, if IValueByTypeProvider.CanProvideValue returns true when provided the type of the field and a reference to the initializer. During initialization the Initializer will request the Init argument ..

Read more

Issue Init(args) only supports passing a maximum of twelve arguments to a component that derives from MonoBehaviour<T…> or implements IInitializable<T…> during its initialization. What can you do if you have a legacy class that depends on more than twelve external objects, and you want to refactor the class to derive from MonoBehaviour<T…> and receive those ..

Read more

When you register a global or a local service, you will always have to specify – either implicitly or explicitly – one or more defining types for it. In the below example, the Logger class is configured to be a global service with the defining types Logger and ILogger, by explicitly specifying the types in ..

Read more

Services Component Another way to define scene services is through using the Services component. Start by adding the Services component to a GameObject in the scene and adding a new entry to its Provides Services list. Then drag the component you want to define as a Service to this list. If you drag a GameObject ..

Read more

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 ..

Read more

Services that can be registered for clients in Init(args) can be split into two categories: local and global services. A local service, is a service that is only available to select clients, depending on their location in scene hierarchies relative to the service. A local services can be registered in the following ways: By attaching ..

Read more