{"id":191,"date":"2021-12-27T12:12:27","date_gmt":"2021-12-27T12:12:27","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=191"},"modified":"2025-07-14T19:35:30","modified_gmt":"2025-07-14T19:35:30","slug":"ivalueprovider","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/reference\/ivalueprovider\/","title":{"rendered":"IValueProvider&lt;TValue&gt;"},"content":{"rendered":"<p>Represents an object that can provide a value of type TValue on demand.<\/p>\n<p>A scriptable object or component that implements the <em>IValueProvider&lt;TValue&gt;<\/em> interface can be assigned to an <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/initializer\/\">Initializer<\/a> field of type TValue.<\/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<p>If your value provider always returns the same value, regardless of the client that is requesting it, then you can just implement the <em>IValueProvider&lt;TValue&gt;.Value<\/em> property.<\/p>\n<pre class=\"code_syntax\"><span class=\"line_wrapper\">[CreateAssetMenu]<\/span>\r\n<span class=\"line_wrapper\">class MainCamera : ScriptableObject, IValueProvider&lt;Camera&gt;<\/span>\r\n<span class=\"line_wrapper\">{<\/span>\r\n<span class=\"line_wrapper\">    public Camera Value =&gt; Camera.main;<\/span>\r\n<span class=\"line_wrapper\">}<\/span><\/pre>\n<h2>Example 2<\/h2>\n<p>If your value provider can return a different value based on the client that is requesting it, then you can also implement the <em>IValueProvider&lt;TValue&gt;.TryGetFor<\/em> method.<\/p>\n<pre class=\"code_syntax\"><span class=\"line_wrapper\">class NearestCamera : ScriptableObject, IValueProvider&lt;Camera&gt;<\/span>\r\n<span class=\"line_wrapper\">{<\/span>\r\n<span class=\"line_wrapper\">    public Camera Value =&gt; TryGetFor(null, out Camera camera) ? camera : null;<\/span>\r\n\r\n<span class=\"line_wrapper\">    public bool TryGetFor(Component client, out Camera camera)<\/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\">            var camera = client.GetComponentInChildren&lt;Camera&gt;();<\/span>\r\n<span class=\"line_wrapper\">            if(camera != null)<\/span>\r\n<span class=\"line_wrapper\">            {<\/span>\r\n<span class=\"line_wrapper\">                return true;<\/span>\r\n<span class=\"line_wrapper\">            }<\/span>\r\n\r\n<span class=\"line_wrapper\">            camera = client.GetComponentInParent&lt;Camera&gt;();<\/span>\r\n<span class=\"line_wrapper\">            if(camera != null)<\/span>\r\n<span class=\"line_wrapper\">            {<\/span>\r\n<span class=\"line_wrapper\">                return camera;<\/span>\r\n<span class=\"line_wrapper\">            }<\/span>\r\n\r\n<span class=\"line_wrapper\">            camera = Object.FindObjectsByType&lt;Camera&gt;()<\/span>\r\n<span class=\"line_wrapper\">                .FirstOrDefault(c =&gt; c.gameObject.scene == client.gameObject.scene);<\/span>\r\n<span class=\"line_wrapper\">            if(camera != null)<\/span>\r\n<span class=\"line_wrapper\">            {<\/span>\r\n<span class=\"line_wrapper\">                return camera;<\/span>\r\n<span class=\"line_wrapper\">            }<\/span>\r\n<span class=\"line_wrapper\">        }<\/span>\r\n\r\n<span class=\"line_wrapper\">        return Camera.main;<\/span>\r\n<span class=\"line_wrapper\">    }<\/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>Providing a reference to prefab instance that is only created at runtime.<\/li>\n<li>Providing a string that has been localized.<\/li>\n<li>Providing a randomized name for a character.<\/li>\n<li>And much more&#8230;<\/li>\n<\/ul>\n<p>Here are some pre-existing <em>IValueProvider&lt;TValue&gt;<\/em> implementations that come bundled with Init(args)<\/p>\n<ul>\n<li><strong>MainCamera<\/strong> &#8211; Returns the current main camera with the tag &#8220;MainCamera&#8221;.<\/li>\n<li><strong>LocalizedString<\/strong> &#8211; Provides a string that has been localized by Unity&#8217;s Localization package.<\/li>\n<\/ul>\n<h3><strong>See Also<\/strong>:<\/h3>\n<ul>\n<li><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivaluebytypeprovider\/\">IValueByTypeProvider<\/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 a value of type TValue on demand. A scriptable object or component that implements the IValueProvider&lt;TValue&gt; 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&#8217;s Init method. Example ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivalueprovider\/\" 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\/191"}],"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=191"}],"version-history":[{"count":19,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/191\/revisions\/630"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}