{"id":65,"date":"2021-10-24T07:37:56","date_gmt":"2021-10-24T07:37:56","guid":{"rendered":"https:\/\/docs.sisus.co\/inity\/?p=65"},"modified":"2025-11-06T07:55:27","modified_gmt":"2025-11-06T07:55:27","slug":"any-t","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/any-t\/","title":{"rendered":"Any&lt;TValue&gt;"},"content":{"rendered":"<p>The <code>Any&lt;TValue&gt;<\/code> type can be used to create serialized fields into which you can assign any objects of the given type using the Inspector.<\/p>\n<p><div id=\"attachment_1070\" style=\"width: 362px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-1070\" loading=\"lazy\" class=\"wp-image-1070 size-full\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/any-t-array.png\" alt=\"\" width=\"352\" height=\"280\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/any-t-array.png 352w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2021\/10\/any-t-array-300x239.png 300w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><p id=\"caption-attachment-1070\" class=\"wp-caption-text\">Any&lt;ICommand&gt;[] field filled with different types of values<\/p><\/div>The value can be of any type which Unity can serialize, using <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/SerializeField.html\">[SerializeField]<\/a> <em>or<\/em> <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/SerializeReference.html\">[SerializeReference]<\/a>, including:<\/p>\n<ul>\n<li>Plain-old C# class<\/li>\n<li>UnityEngine.Object<\/li>\n<li>An interface type<\/li>\n<\/ul>\n<p>You&#8217;ll be able to select any assignable value for the serialized field using the Inspector, and it will be serialized for you.<\/p>\n<h2>Service Support<\/h2>\n<p>If a <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/global-services\/\">service<\/a>\u00a0has the <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/service-defining-type\/\">defining type<\/a> <code>TValue<\/code>, then serialized fields of type Any&lt;TValue&gt; will automatically receive a reference to the service.<\/p>\n<p>You are however still able to manually drag-and-drop some other Object into the field, to use that instead of the default Service instance.<\/p>\n<h2>Value Provider Support<\/h2>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivalueprovider\/\">Value provider<\/a> components and scriptable objects that can provide a value of type TValue can be drag-and-dropped into a serialized field of type <code>Any&lt;TValue&gt;<\/code>.<\/p>\n<p>Value provider scriptable objects that have the <a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/value-provider-menu\/\">[ValueProviderMenu] attribute<\/a> set to target fields of type TValue will appear in the dropdown menu next to serialized field of type <code>Any&lt;TValue&gt;<\/code>.<\/p>\n<h2>Example<\/h2>\n<p>If you wanted to make it possible to assign any object that implements the <code>IInteractable<\/code> interface using the Inspector, you could add a member field of type <code>Any&lt;IInteractable&gt;<\/code> to your component class:<\/p>\n<pre class=\"western\">[SerializeField] Any&lt;IInteractable&gt; interactable;<\/pre>\n<p>You can then use the <code>Value<\/code> property or the <code>GetValue(Component client)<\/code> method to acquire the value from the <code>Any&lt;TValue&gt;<\/code> field.<\/p>\n<pre>[SerializeField] Any&lt;IInteractable&gt; interactable;\r\n\r\nIInteractable Interactable =&gt; interactable.GetValue(this);\r\n\r\npublic void Interact() =&gt; interactable.Interact(this);<\/pre>\n<p>You will need to use the <code>GetValue<\/code> method and pass in the the client in order to support acquiring <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/local-services\/\">local services<\/a> and to get values from <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivalueprovider\/\">Value Providers<\/a> that provide client-specific values, such as <code>GetComponent<\/code>.<\/p>\n<h2>Use Cases<\/h2>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/features\/initializer\/\">Initializers<\/a> already use <code>Any&lt;TValue&gt;<\/code> fields internally. This means that you typically won&#8217;t need to define <code>Any&lt;TValue&gt;<\/code> fields directly in your <a href=\"https:\/\/docs.sisus.co\/init-args\/clients\/monobehaviour-t\/\">MonoBehaviour&lt;T&#8230;&gt;<\/a> clients.<\/p>\n<p>However, there can still occasional be situations where using <code>Any&lt;TValue&gt;<\/code> fields directly could be useful.<\/p>\n<h3>Value<strong> Providers<\/strong><\/h3>\n<p>since value providers can return client-specific values, can support resolving values lazily, and can sometimes be drawn embedded inside the Init sections of client components within a limited space, it can make sense to use <code>Any&lt;TValue&gt;<\/code> fields in value providers directly.<\/p>\n<pre>class GetParentOfTarget : MonoBehaviour, IValueProvider\r\n{\r\n\t[SerializeField] Any&lt;Transform&gt; target;\r\n\r\n\tpublic Transform Value =&gt; null;\r\n\r\n\tpublic bool TryGetFor(Component client, out Transform parent)\r\n\t{\r\n\t\tparent = target.GetValue(client);\r\n\t\treturn parent != null;\r\n\t}\r\n}<\/pre>\n<h3>InitializerBase&lt;T&#8230;&gt;<\/h3>\n<p>While usually the Initializers you generate for clients will derive from an <code>Initializer&lt;T...&gt;<\/code> base class, and already use <code>Any&lt;TValue&gt;<\/code> fields directly to resolve all the Init arguments, in rare occasions you might want to create an initializer which derives from <code>InitializerBase&lt;T...&gt;<\/code> instead, and specifies how the Init arguments should resolved manually. In such situations it might sometimes be useful to define some <code>Any&lt;TValue&gt;<\/code> fields manually in the initializer.<\/p>\n<pre>class EnemyInitializer : InitializerBase&lt;Enemy, Transform&gt;\r\n{\r\n\t[SerializeField] Any&lt;Player[]&gt; players;\r\n\r\n\tprotected override Transform Argument\r\n\t{\r\n\t\tget =&gt; GetNearestPlayer();\r\n\t\tset { }\r\n\t}\r\n\r\n\tprivate Transform GetNearestPlayer()\r\n\t\t=&gt; players.GetValue(this)\r\n\t\t.OrderBy(player =&gt; Vector3.Distance(player.transform.position, transform.position))\r\n\t\t.FirstOrDefault()?.transform;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Any&lt;TValue&gt; type can be used to create serialized fields into which you can assign any objects of the given type using the Inspector. The value can be of any type which Unity can serialize, using [SerializeField] or [SerializeReference], including: Plain-old C# class UnityEngine.Object An interface type You&#8217;ll be able to select any assignable value ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/any-t\/\" 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\/65"}],"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=65"}],"version-history":[{"count":25,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":1079,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/65\/revisions\/1079"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}