Author : Timo Naskali

Opening The Service Debugger Window You can open the Service Debugger Window using the main menu item Window > Analysis > Service Debugger. Using The Service Debugger Window The window lists all currently active services, which have been registered via ServiceAttributes, Service Tags and Services Components. The first name shown in the list is the ..

Read more

Issue When launching the game, a service does not get injected to any of its clients as expected, and an warning similar to the following can be seen in the Console window: Invalid Service Definition: Class of the registered instance ‘MyService’ does not implement the defining interface type of the service ‘IMyInterface’. UnityEngine.Debug:LogWarning (object) Sisus.Init.ServiceUtility:SetInstance ..

Read more

Issue A client component that derives from MonoBehaviour<T…>, does not receive any services via its Init method. Solution #1: OnAwake One possible reason for this, is that an Awake method has been defined in the client’s class. If this is is done, it will prevent the Awake method in the base class from being called. ..

Read more

Service Injection 101 Here is a quick showcase of how a service can be injected to a component: ExampleService.cs: using Sisus.Init; using UnityEngine; [Service] class ExampleService { public void Hello() => Debug.Log(“Hello, World!”); } ExampleComponent.cs: using Sisus.Init; using UnityEngine; class ExampleComponent : MonoBehaviour<ExampleService> { protected override void Init(ExampleService service) => service.Hello(); } Create script files ..

Read more

To add a component and initialize it with arguments, you can use the GameObject.AddComponent<TComponent, T…> extension methods found in the Sisus.Init namespace. These work very much like the built-in GameObject.AddComponent<T> methods, except you also specify the types of the Init arguments alongside the type of the added component as the generic argument of the method, ..

Read more

What Is Dependency Injection Creating new components using the base classes in Init(args) is very similar to how it would work normally in Unity. The key difference is how the component obtains references to other objects that it depends on (its dependencies). Instead of components retrieving their dependencies using GameObject.Find, FindObjectOfType, GetComponent, static singleton properties ..

Read more

Init(args) contains new generic versions of the StateMachineBehaviour base class, extending it with the ability to receive up to six objects during initialization. For example the following AttackBehaviour class depends on an object that implements the IAttackable interface and a ScriptableObject asset of type AttackBehaviourSettings: public class AttackBehaviour : StateMachineBehaviour<IAttackable, AttackBehaviourSettings> When you create a ..

Read more

Init(args) has been designed with inversion of control in mind and aims to make it as easy as possible to work with interfaces instead of specific classes in Unity. One pain point when using interfaces in Unity is that checking them for null can be problematic. This is because in addition to being actually null, ..

Read more

A base class for MonoBehaviours that depend on receiving upto six objects in their constructor during initialization. For example the following Player class depends on an object that implements the IInputManager interface and an object of type Camera. public class Player : ConstructorBehaviour<IInputManager, Camera> When you create a component that inherits from one of the ..

Read more

Represents an object that can provide a value of type T on demand. The value of a UnityEngine.Object-derived class that implements IValueProvider<T> can be assigned to an Init argument field of type T. When this is done, the value returned by the provider at runtime will be passed to the client during initialization. This can ..

Read more