IInitializable<T…>

  08. Reference No Comments

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 Client().Init("Hello, world!");
}

class Client : IInitializable<string>
{
   public void Init(string message) => Debug.Log(message);
}

InitArgs

All classes that implement an IInitializable<T…> interface can also be provided initialization arguments via the InitArgs API. This makes it possible to deliver initialization arguments to components which they can already receive at the beginning of their Awake event.

using UnityEngine;
using Sisus.Init;

class Example : MonoBehaviour
{
   IEnumerator Start()
   {
      // Provide a string argument to Client components
      InitArgs.Set<Client, string>("Hello, World!");

      // Load a scene containing Client components
      SceneManager.LoadSceneAsync("Clients", LoadSceneMode.Additive);

      // Wait until the argument has been received
      while(!InitArgs.Received<TClient, string>())
      {
            yield return null;
      }

      // Release the argument from the cache
      InitArgs.Clear<TComponent, TArgument>()
   }
}

class Client : MonoBehaviour, IInitializable<string>
{
   void Awake()
   {
      if(InitArgs.TryGet(this, out string message))
      {
         Init(message);
      }
   }

   public void Init(string message) => Debug.Log(message);
}

AddComponent with Arguments

Components that implement an IInitializable<T…> interface can receive initialization arguments via AddComponent.

Instantiate with Arguments

Components and scriptable objects that implement an IInitializable<T…> interface can receive initialization arguments via Instantiate.

Global Services

Classes that have been registered as global services using the [Service] attribute can receive other global services that they depend on by implementing IInitializable<T…>.

using UnityEngine;
using Sisus.Init;

[Service]
class Service1 { }

[Service]
class Service2 : IInitializable<Service1>
{
   public void Init(Service1 Service1)
   {
      Debug.Log($"Service2 received {service1}.");
   }
}

Base Classes

The MonoBehaviour<T…> and ScriptableObject<T…> base classes already implement IInitializable<T…>.

When you create components or scriptable object classes that depend on some other objects,  you should typically prefer making them derive from one of those base classes, rather tan implementing an IInitializable<T…> interface manually. The base classes contain code for automatically acquiring initialization arguments provided to them via the InitArgs API, as well as some other useful functionality.

Leave a Reply

Your email address will not be published. Required fields are marked *