Author : Timo Naskali

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

Methods void Init(TFirstArgument firstArgument, … TFifthArgument fifthArgument); Classes that implement IInitializable<T…> have all the same functionality that they would get by implementing an IArgs<T…> interface, but with additional support for their Init function to be called manually by other classes. This makes it possible for classes to inject dependencies even in cases where the object ..

Read more

IArgs<T…> Classes that implement one of the generic IArgs<T…> interfaces can be provided with arguments during initialization (up to a maximum of six). Methods such as Instantiate<TObject, T…> and AddComponent<TComponent, T…> can only be used to create instances of classes that implement one of the IArgs<T…> interfaces. A contract to receive arguments Any object that ..

Read more

Sometimes you may want to make use of read-only fields or get-only properties in your components and scriptable objects. Read-only members make your data immutable and as such result in code that is less prone to errors and fully thread safe without needing any complicated thread locking. The issue is that you can’t pass any ..

Read more