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 ..
Author : Timo Naskali
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 in the Sisus.Init namespace. The methods work just like the built-in AddComponent<T> methods, except you also list the types of the component’s Init parameters as generic type ..
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 ..
Classes that implement one of the generic IArgs<T…> interfaces can be provided with arguments during initialization (up to a maximum of twelve). 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 implements ..
The generic GameObject structs are builders that can be used to initialize a GameObject and upto three components in a single line of code. If you want a GameObject with a single component to be build, initialize a new instance of the GameObject<T> struct and specify the type of the component you want attached to ..
The Find class is a utility class that helps in locating instances of objects from loaded scenes as well as assets from the project. It has been designed from the ground up to work well with interface types, making it easier to decouple your code from specific concrete classes. All methods in the Find class ..