Opening The Window You can open the Service Debugger Window using the main menu item Window > Analysis > Service Debugger. Using The Window You can switch between seeing all currently active services and all currently loaded clients by pressing the Services / Clients toggle in the the top toolbar. Services The Services view lists the ..
Author : Timo Naskali
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 ..
Issue An client component does not receive the objects that it depends on in its Init method during its initialization. Cause: Not All Dependencies Are Services One possible reason for this is that the component depends on one or more objects that have not been registered as services. Components that derive from MonoBehaviour<T…> can only ..
Service Injection 101 Here is a quick showcase of how easily a service can be injected to a component: Service.cs: using Sisus.Init; using UnityEngine; [Service] class Service { public void Greet() => Debug.Log(“Hello, World!”); } Client.cs: using Sisus.Init; class Client : MonoBehaviour<Service> { protected override void Init(Service service) => service.Greet(); } You can test the ..
AddComponent with Arguments A new component that derives from MonoBehaviour<T…> or implements IInitializable<T…> can be attached to GameObject and initialized with arguments using the AddComponent<TComponent, T…> extension methods. The methods work like the built-in AddComponent<T> methods, except they allow you to also pass in Init arguments: using Sisus.Init; using UnityEngine; public static class GameManager { ..
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 manually retrieving their dependencies using various methods like GameObject.Find, FindObjectOfType, ..
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 ..
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, ..
Represents an object that can provide a value of type TValue on demand. A scriptable object or component that implements the IValueProvider<TValue> interface can be assigned to an Initializer field of type TValue. During initialization the Initializer will request the Init argument from the value provider, and pass it to the client’s Init method. Example ..
Methods void Init(TFirstArgument firstArgument, … TwelfthArgument twelfthArgument); The Init method Classes that implement one of the generic IInitializable<T…> interfaces are called clients. Clients have an Init method through which they can receive up to twelve objects that they need during their initialization. using UnityEngine; using Sisus.Init; class Example : MonoBehaviour { void Start() => new ..