{"id":182,"date":"2021-12-13T17:35:40","date_gmt":"2021-12-13T17:35:40","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=182"},"modified":"2025-07-14T19:41:13","modified_gmt":"2025-07-14T19:41:13","slug":"iinitializable","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/","title":{"rendered":"IInitializable&lt;T&#8230;&gt;"},"content":{"rendered":"<h2><span style=\"font-size: 22px; font-weight: bold;\">Methods<\/span><\/h2>\n<blockquote><p>void Init(TFirstArgument firstArgument, &#8230; TwelfthArgument twelfthArgument);<\/p><\/blockquote>\n<hr \/>\n<h2>The Init method<\/h2>\n<p>Classes that implement one of the generic <em>IInitializable&lt;T\u2026&gt;<\/em> interfaces are called <em>clients<\/em>. Clients have an Init method through which they can receive up to twelve objects that they need during their initialization.<\/p>\n<pre>using UnityEngine;\r\nusing Sisus.Init;\r\n\r\nclass Example : MonoBehaviour\r\n{\r\n  \u00a0void Start() =&gt; new Client().Init(\"Hello, world!\");\r\n}\r\n\r\nclass Client : IInitializable&lt;string&gt;\r\n{\r\n  \u00a0public void Init(string message) =&gt; Debug.Log(message);\r\n}<\/pre>\n<h2>InitArgs<\/h2>\n<p>All classes that implement an <em>IInitializable&lt;T\u2026&gt; <\/em>interface can also be provided initialization arguments via the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/initargs\/\">InitArgs API<\/a>. This makes it possible to deliver initialization arguments to components which they can already receive at the beginning of their Awake event.<\/p>\n<pre>using UnityEngine;\r\nusing Sisus.Init;\r\n\r\nclass Example : MonoBehaviour\r\n{\r\n   IEnumerator Start()\r\n   {\r\n      \/\/ Provide a string argument to Client components\r\n      InitArgs.Set&lt;Client, string&gt;(\"Hello, World!\");\r\n\r\n      \/\/ Load a scene containing Client components\r\n      SceneManager.LoadSceneAsync(\"Clients\", LoadSceneMode.Additive);\r\n\r\n      \/\/ Wait until the argument has been received\r\n      while(!InitArgs.Received&lt;TClient, string&gt;())\r\n      {\r\n            yield return null;\r\n      }\r\n\r\n      \/\/ Release the argument from the cache\r\n      InitArgs.Clear&lt;TComponent, TArgument&gt;()\r\n   }\r\n}\r\n\r\nclass Client : MonoBehaviour, IInitializable&lt;string&gt;\r\n{\r\n   void Awake()\r\n   {\r\n      if(InitArgs.TryGet(this, out string message))\r\n      {\r\n         Init(message);\r\n      }\r\n   }\r\n\r\n   public void Init(string message) =&gt; Debug.Log(message);\r\n}<\/pre>\n<h2>AddComponent with Arguments<\/h2>\n<p>Components that implement an <em>IInitializable&lt;T&#8230;&gt; <\/em>interface can <a href=\"https:\/\/docs.sisus.co\/init-args\/getting-started\/initializing-components-in-code\/\">receive initialization arguments via AddComponent<\/a>.<\/p>\n<h2>Instantiate with Arguments<\/h2>\n<p>Components and scriptable objects that implement an <em>IInitializable&lt;T&#8230;&gt; <\/em>interface can <a href=\"https:\/\/docs.sisus.co\/init-args\/getting-started\/initializing-components-in-code\/\">receive initialization arguments via Instantiate<\/a>.<\/p>\n<h2>Global Services<\/h2>\n<p>Classes that have been registered as <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/global-services\/\">global services<\/a> using the <a href=\"https:\/\/docs.sisus.co\/init-args\/services\/service-attribute\/\">[Service] attribute<\/a> can receive other global services that they depend on by implementing <em>IInitializable&lt;T&#8230;&gt;<\/em>.<\/p>\n<pre>using UnityEngine;\r\nusing Sisus.Init;\r\n\r\n[Service]\r\nclass Service1 { }\r\n\r\n[Service]\r\nclass Service2 : IInitializable&lt;Service1&gt;\r\n{\r\n   public void Init(Service1 Service1)\r\n   {\r\n      Debug.Log($\"Service2 received {service1}.\");\r\n   }\r\n}\r\n\r\n<\/pre>\n<h3>Base Classes<\/h3>\n<p>The <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a> and <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/scriptableobject-t\/\">ScriptableObject&lt;T&#8230;&gt;<\/a> base classes already implement <em>IInitializable&lt;T&#8230;&gt;<\/em>.<\/p>\n<p>When you create components or scriptable object classes that depend on some other objects,\u00a0 you should typically prefer making them derive from one of those base classes, rather tan implementing an <em>IInitializable&lt;T&#8230;&gt;<\/em> interface manually. The base classes contain code for automatically acquiring initialization arguments provided to them via the <em>InitArgs API, as well as some<\/em> other useful functionality.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Methods void Init(TFirstArgument firstArgument, &#8230; TwelfthArgument twelfthArgument); The Init method Classes that implement one of the generic IInitializable&lt;T\u2026&gt; 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 { \u00a0void Start() =&gt; new ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/iinitializable\/\" title=\"read more\">Read more<\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/182"}],"collection":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":10,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"predecessor-version":[{"id":221,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/182\/revisions\/221"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}