Author : Timo Naskali

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

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

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 ..

Read more

The ScriptableWrapper class is a ScriptableObject that can act as a simple wrapper for a plain old class object. It makes it easy to take a plain old class object and serialize it as an asset in the project. Let’s say you had a plain old class called Settings: public class Settings { public float ..

Read more

Wrappers can be used to attach plain old C# objects to a GameObjects, have them receive callbacks during Unity events like Update and OnDestroy, and to start coroutines. Creating a Wrapper Let’s say you had a plain old C# class called Player: using System; using UnityEngine; [Serializable] public class Player { [SerializeField] private Id id; ..

Read more