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 because receiving the inputManager service from the // outside is entirely optional, so don't want to be warned about missing Init arguments, // nor initialization to be delayed until the Init arguments have been provided. [Init(Enabled = false, DefaultInitializer = null)] class Player : MonoBehaviour<IInputManager> { IInputManager inputManager; protected override void Init(IInputManager inputManager) { this.inputManager = inputManager; } protected override void OnAwake() { // if no Input Manager was provided from the outside, use the default one inputManager ??= new DefaultInputManager(); } }
WaitForServices
Specifies whether the component’s initialization should be delayed if any of the services that it depends on are missing when the object is loaded.
If this is set to true, and any of the services that the client depends on are missing when it is being loaded, then the component will be disabled and its initialization will delayed until all its services have become available. The moment that all its services become available, they will be pass to the client’s Init method, and then the client will be enabled, causing the OnAwake, OnEnable and Start events to be executed.
If you have a component that does not necessarily require any services to be injected to it via the Init method, then WaitForServices should be set to false. This can be useful if:
- The component relies on serialized fields for resolving its services when it’s loaded as part of a scene or a prefab, and the Init method is only used when the component is created using AddComponent at runtime or during Edit Mode unit tests.
- The component uses default services if no custom services were injected to it. The Init method is only used in rare cases, such as unit tests, to allow replacing those services with different ones.
The default value of WaitForServices is true.
HideInInspector
Specifies whether the Init section should be hidden in the Inspector or not for the component.
You might want to hide the Init section for a component when:
- You know that all the services that the component depends will always be available at runtime (for example, because they all have been registered as global services using the [Service] attribute), and you want to minimize Inspector clutter, or get rid of misleading Service Not Found texts, by hiding the Init section.
- The component does not actually depend on receiving services via its Init method, and you want to minimize Inspector clutter, or get rid of misleading Service Not Found texts, by hiding the Init section.
If an Initializer is attached to a component, the Init section will still be drawn regardless of the value of HideInInspector. You can attach an initializer to a component if its Init section is hidden using the Add Component menu, or by dragging-and-dropping the Initializer’s script to the GameObject.
The default value of HideInInspector is false.
NullArgumentGuard
Specifies Null Argument Guard flags for the component.
When the [Init] attribute is attached to an Initializer type, this only specifies the default NullArgumentGuard flags for the Initializer, but they will still be changeable via the Inspector.
None
EditModeWarning
RuntimeException
All
Both EditModeWarning and RuntimeException are enabled.
The default value of NullArgumentGuard is All.
DefaultInitializer
Specifies which initializer should be used by default.
On MonoBehaviour<T…>
When the [Init] attribute is attached to a type that derives from MonoBehaviour<T…>, assigning a value of null or false to DefaultInitializer indicates that no Initializer should be attached to the component by default when it is attached to a GameObject in Edit Mode.
Assigning the type an initializer class to DefaultInitializer means that an instance of said initializer should be attached to the component by default when it is attached to a GameObject in Edit Mode.
On Initializer
When the [Init] attribute is attached to an Initializer class, assigning a value of null or false to DefaultInitializer indicates that this initializer should be attached to any components by default when they are attached to a GameObject in Edit Mode.
Assigning true to DefaultInitializer means that an instance of said initializer should be attached to instances of its target component by default when they are attached to a GameObject in Edit Mode.
Enabled
Setting Enabled to false is equivalent to setting WaitForServices to false, NullArgumentGuard to None and HideInspector to true.
