Author : Timo Naskali

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

Read more

This page aims to answer what makes Init(args) different from other dependency injection solutions for Unity. Versatility Init(args) improves upon just using normal serialized fields (which is also a form of dependency injection) by introducing a lot more flexibility. Serialized fields don’t support the following use cases: Using services from different scenes or prefabs. Using ..

Read more

The [Init] attribute can be added to MonoBehaviour<T…> derived types, or initializer types, to configure default settings related to initialization and the drawing of the Init section in the Inspector. using Sisus.Init; using UnityEngine; // Set Enabled to false, to disable NullArgumentGuard, hide the Init section in the Inspector, // and disable WaitForServices. This is ..

Read more

Issue You have a component that derives from MonoBehaviour<T…> or implements IInitializable<T…>, but you don’t want the Init section to be drawn in the Inspector. One possible reason for this could be that the component only needs to receive Init arguments when it is being initialized at runtime using AddComponent, but never when it is ..

Read more

Cross-scene GameObject and component references can be assigned into Init fields of client components that have an initializer. To assign a cross-scene reference, open multiple scenes in the Editor, and drag-and-drop a GameObject from a different scene into the Init field of the client component. This will make Init(args) automatically generate a unique Cross-Scene Id ..

Read more

What is a Value Provider? Value providers are a highly flexible system that can be used to provide Init arguments dynamically at runtime. Value providers are any objects that implement IValueProvider<TValue> or IValueByTypeProvider (or their asynchronous counterparts, IValueProviderAsync<TValue> or IValueByTypeProviderAsync). Use Cases There are a lot of possible use cases for value providers – here ..

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

Problem: You have a base class that depends on some services. You want to create a class that derives from that base class, which depends on some additional services on top of the ones that the base class does. class Base : MonoBehaviour<A, B> { // The base class depends on some services A 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