{"id":509,"date":"2024-12-16T07:13:32","date_gmt":"2024-12-16T07:13:32","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=509"},"modified":"2025-07-14T19:29:34","modified_gmt":"2025-07-14T19:29:34","slug":"ivaluebytypeprovider","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/ivaluebytypeprovider\/","title":{"rendered":"IValueByTypeProvider"},"content":{"rendered":"<p>Represents an object that can provide values of requested types on demand.<\/p>\n<p>A scriptable object or component that implements the <em>IValueByTypeProvider<\/em> interface can be assigned to an <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/initializer\/\">Initializer<\/a> field, if <em>IValueByTypeProvider.CanProvideValue<\/em> returns <em>true<\/em> when provided the type of the field and a reference to the initializer.<\/p>\n<p>During initialization the Initializer will request the Init argument from the value provider, and pass it to the client&#8217;s Init method.<\/p>\n<h2>Example<\/h2>\n<pre class=\"code_syntax\"><span class=\"line_wrapper\">[CreateAssetMenu] <\/span>\r\n<span class=\"line_wrapper\">class GetComponent : ScriptableObject, IValueByTypeProvider<\/span>\r\n<span class=\"line_wrapper\">{<\/span>\r\n<span class=\"line_wrapper\">    public bool TryGetFor&lt;TValue&gt;(Component client, out TValue value)<\/span>\r\n<span class=\"line_wrapper\">    {<\/span>\r\n<span class=\"line_wrapper\">        if(client == null)<\/span>\r\n<span class=\"line_wrapper\">        {<\/span>\r\n<span class=\"line_wrapper\">            value = default;<\/span>\r\n<span class=\"line_wrapper\">            return false;<\/span>\r\n<span class=\"line_wrapper\">        }<\/span>\r\n        \r\n<span class=\"line_wrapper\">        return client.gameObject.TryGetComponent(out value);<\/span>\r\n<span class=\"line_wrapper\">    }<\/span>\r\n\r\n<span class=\"line_wrapper\">    public bool CanProvideValue&lt;TValue&gt;(Component client)<\/span>\r\n<span class=\"line_wrapper\">        =&gt; client != null &amp;&amp;\r\n           (typeof(Object).IsAssignableFrom(typeof(TValue)) || typeof(TValue).IsInterface);<\/span>\r\n<span class=\"line_wrapper\">}<\/span><\/pre>\n<h2>Use Cases<\/h2>\n<p>Value providers can provide functionality such as:<\/p>\n<ul>\n<li>Cross-scene references.<\/li>\n<li>Providing a reference to prefab instance that is only created at runtime.<\/li>\n<li>Providing an asset that is loaded asynchronously using <a href=\"https:\/\/docs.unity3d.com\/Manual\/com.unity.addressables.html\">Addressables<\/a>.<\/li>\n<li>A generic service locator that lazily creates and returns requested services.<\/li>\n<li>Providing a component found in the children of the client component.<\/li>\n<li>And much more&#8230;<\/li>\n<\/ul>\n<p>Here are some pre-existing <em>IValueByTypeProvider<\/em>\u00a0implementations that come bundled with Init(args):<\/p>\n<ul>\n<li><strong>CrossSceneReference<\/strong> &#8211; Returns a reference to a component or game object that can located in a different scene than the client.<\/li>\n<li><strong>AddComponent<\/strong> &#8211; Attaches a new instance of the given type to the client&#8217;s game object and returns it.<\/li>\n<li><strong>GetComponent<\/strong> &#8211; returns an object of the requested type attached to the client&#8217;s game object.<\/li>\n<li><strong>GetOrAddComponent<\/strong> &#8211; Returns an object of the given type attached to the client&#8217;s game object if one is found; otherwise, attaches a new instance of the type to the client and returns it.<\/li>\n<li><strong>GetComponentInChildren<\/strong> &#8211; Returns an object of the requested type attached to the client&#8217;s game object or any of its children.<\/li>\n<li><strong>GetComponentInParent<\/strong> &#8211; Returns an object of the requested type attached to the client&#8217;s game object or any of its parents.<\/li>\n<li><strong>GetComponents<\/strong>\u00a0&#8211; Returns all objects of the requested type attached to the client&#8217;s game object.<\/li>\n<li><strong>GetComponentsInChildren<\/strong> &#8211; Returns all objects of the given type attached to the client&#8217;s game object and all of its children.<\/li>\n<li><strong>GetComponentsInParent<\/strong> &#8211; Returns all objects of the given type attached to the client&#8217;s game object and all of its parents.<\/li>\n<li><strong>FindAnyObjectByType<\/strong> &#8211; Returns an object of the given type that is currently loaded using.<\/li>\n<\/ul>\n<h3><strong>See Also<\/strong>:<\/h3>\n<ul>\n<li><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivalueprovider\/\">IValueProvider&lt;TValue&gt;<\/a><\/li>\n<li><a href=\"https:\/\/docs.sisus.co\/init-args\/features\/value-provider-menu\/\">ValueProviderMenu<\/a><\/li>\n<li><a href=\"https:\/\/docs.sisus.co\/init-args\/features\/initializer\/\">Initializer<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Represents an object that can provide values of requested types on demand. A scriptable object or component that implements the IValueByTypeProvider interface can be assigned to an Initializer field, if IValueByTypeProvider.CanProvideValue returns true when provided the type of the field and a reference to the initializer. During initialization the Initializer will request the Init argument ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivaluebytypeprovider\/\" 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\/509"}],"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=509"}],"version-history":[{"count":8,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":529,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/509\/revisions\/529"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}