Author : Timo Naskali

Sometimes you might need to inject a different instance of the same service to every client that requests the service. This can be achieved in Init(args) using value providers: Create a class that implements IValueProvider<T>, where T is the concrete or defining type of your service. Implement IValueProvider<T>.Value in such a way, that a new ..

Read more

Types that contain generic type parameters can also be registered as global services by adding the [Service] attribute to them: using Sisus.Init; [Service] public class Logger<T> { public void Info(string message) => Debug.Log(typeof(T).Name + ” – ” + message); } When this is done, no instance of the global service is created automatically during initialization ..

Read more

Init(args) has built-in support for the Entity Component System (ECS). Systems You can register a system as a global service by adding the [Service] attribute to its type. This makes it possible for clients – including all other services that have the [Service] attribute – to receive the system automatically during their initialization. Containing World ..

Read more

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 Are Value Providers? Value providers are a flexible system that can be used to resolve Init arguments dynamically at runtime. Value providers are ScriptableObjects and components 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