{"id":422,"date":"2024-09-20T20:13:03","date_gmt":"2024-09-20T20:13:03","guid":{"rendered":"https:\/\/docs.sisus.co\/init-args\/?p=422"},"modified":"2025-07-14T19:31:24","modified_gmt":"2025-07-14T19:31:24","slug":"value-provider-menu","status":"publish","type":"post","link":"https:\/\/docs.sisus.co\/init-args\/initializers\/value-provider-menu\/","title":{"rendered":"3. [ValueProviderMenu] attribute"},"content":{"rendered":"<p>The [ValueProviderMenu] attribute can be used to introduce new menu items that will be shown in the dropdown menus for all Init arguments of targeted types.<\/p>\n<p>The attribute can be added to a ScriptableObject-derived class that implements <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivalueprovider\/\">IValueProvider&lt;TValue&gt;<\/a> or <a href=\"https:\/\/docs.sisus.co\/init-args\/reference\/ivaluebytypeprovider\/\">IValueByTypeProvider<\/a>.<\/p>\n<p>You can then use the Value property to access the value stored inside the field.<\/p>\n<h3>Example<\/h3>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-425\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/get-component-value-provider.gif\" alt=\"\" width=\"381\" height=\"219\" \/><\/p>\n<pre>[ValueProviderMenu(\"Hierarchy\/Get Component\"<span style=\"color: #000000;\"><span style=\"color: #808030;\">,<\/span><\/span> Is<span style=\"color: #000000;\"><span style=\"color: #808030;\">.<\/span><\/span>SceneObject), CreateAssetMenu]<span style=\"color: #000000;\"><span style=\"color: #800080;\">\r\nclass<\/span> GetComponent\u00a0<span style=\"color: #800080;\"><span style=\"color: #808030;\">:<\/span><\/span> ScriptableObject<span style=\"color: #808030;\">,<\/span> IValueByTypeProvider\r\n<span style=\"color: #800080;\"><span style=\"color: #808030;\">{<\/span><\/span>\r\n    <span style=\"color: #800080;\">public bool<\/span> TryGetFor<span style=\"color: #808030;\">&lt;<\/span>TValue<span style=\"color: #808030;\">&gt;<\/span><span style=\"color: #808030;\">(<\/span>Component client<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800080;\">out <\/span>TValue value<span style=\"color: #808030;\">)<\/span>\r\n    <span style=\"color: #800080;\"><span style=\"color: #808030;\">{<\/span><\/span>\r\n        if(client <span style=\"color: #800080;\">==<\/span> null)\r\n        <span style=\"color: #800080;\"><span style=\"color: #808030;\">{<\/span><\/span>\r\n            value = default<span style=\"color: #800080;\">;<\/span>\r\n            return false<span style=\"color: #800080;\">;<\/span>\r\n        <span style=\"color: #800080;\"><span style=\"color: #808030;\">}<\/span><\/span>\r\n\r\n        return Find<span style=\"color: #808030;\">.<\/span>In<span style=\"color: #808030;\">(<\/span>client<span style=\"color: #808030;\">.<\/span>gameObject<span style=\"color: #808030;\">,<\/span> <span style=\"color: #800080;\">out<\/span> value<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n    <span style=\"color: #800080;\"><span style=\"color: #808030;\">}<\/span><\/span><span style=\"color: #800080;\">\r\n<span style=\"color: #808030;\">}<\/span>\r\n<\/span><\/span><\/pre>\n<h2>Shared Value Provider<\/h2>\n<p>If you have a value provider class with no serialized fields, and you create a single asset from it somewhere in your project, then all Init arguments for which the menu item is selected, will be assigned a reference to that same single asset.<\/p>\n<pre>[CreateAssetMenu, ValueProviderMenu(\"Shared Value Provider\", typeof(string))]\r\nclass SharedValueProvider : ScriptableObject, IValueProvider&lt;string&gt;\r\n{\r\n    public string Value =&gt; \"Value\";\r\n}<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-559\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/shared-value-provider.png\" alt=\"\" width=\"317\" height=\"81\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/shared-value-provider.png 317w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/shared-value-provider-300x77.png 300w\" sizes=\"(max-width: 317px) 100vw, 317px\" \/><\/p>\n<p>This can be useful for conserving memory, in cases where only a single value provider is needed.<\/p>\n<h2>Embedded Value Providers<\/h2>\n<p>If your value provider contains serialized fields, or you have zero, or more than one instances of the value provider class in your project, then each Init argument for which the menu item is selected, will be assigned its own instance of the value provider object.<\/p>\n<pre>[ValueProviderMenu(\"Embedded Value Provider\", typeof(string))]\r\nclass EmbeddedValueProvider : ScriptableObject, IValueProvider&lt;string&gt;\r\n{\r\n    [SerializeField] string value;\r\n    public string Value =&gt; value;\r\n}<\/pre>\n<p><img loading=\"lazy\" class=\"alignnone size-full wp-image-560\" src=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/embedded-value-provider.png\" alt=\"\" width=\"641\" height=\"215\" srcset=\"https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/embedded-value-provider.png 641w, https:\/\/docs.sisus.co\/init-args\/wp-content\/uploads\/sites\/6\/2024\/09\/embedded-value-provider-300x101.png 300w\" sizes=\"(max-width: 641px) 100vw, 641px\" \/><\/p>\n<p>This makes it possible to configure each value provider separately.<\/p>\n<p><span style=\"font-size: 24px; font-weight: bold;\">Is Constraints<\/span><\/p>\n<p>The following <em>Is<\/em> constraints can be applied for the targets of value providers with the ValueProviderMenu attribute:<\/p>\n<h3>Unconstrained<\/h3>\n<div>No constraints applied to the types of Init parameters targeted.<\/div>\n<h3>Class<\/h3>\n<div>Shown in dropdown menus of reference type Init parameters.<\/div>\n<h3>ValueType<\/h3>\n<div>Shown in dropdown menus of struct type Init parameters.<\/div>\n<h3>Concrete<\/h3>\n<div>Shown in dropdown menus of concrete type Init parameters.<\/div>\n<h3>Abstract<\/h3>\n<div>Shown in dropdown menus of abstract type Init parameters.<\/div>\n<h3>Interface<\/h3>\n<div>Shown in dropdown menus of interface type Init parameters.<\/div>\n<h3>BuiltIn<\/h3>\n<div>Shown in dropdown menus of Init parameters of built-in C# types, such as bool, int, float and string.<\/div>\n<h3>Component<\/h3>\n<div>\n<div>Shown in dropdown menus of component type Init parameters.<\/div>\n<div><\/div>\n<div>This includes both all component-derived types, as well as any interface types which are implemented by at least one component-derived type.<\/div>\n<\/div>\n<h3>WrappedObject<\/h3>\n<div>\n<div>Shown in dropdown menus of parameters of plain old C# types that have a <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/wrapper\/\">Wrapper<\/a> class.<\/div>\n<\/div>\n<h3>SceneObject<\/h3>\n<div>\n<div>Shown in dropdown menus of Init parameters of types that can exist as part of a scene.<\/div>\n<div><\/div>\n<div>This includes all component-derived types, all plain old C# types that have a Wrapper class, all interface types that are implemented by at least one component-derived type, and the GameObject type.<\/div>\n<\/div>\n<h3>Asset<\/h3>\n<div>\n<div>Shown in dropdown menus of Init parameters of types that can exist as a standalone asset, or as part of a prefab.<\/div>\n<div><\/div>\n<div>This includes all <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/Object.html\">Object<\/a>-derived types, all plain old C# types that have a Wrapper class, all interface types that are implemented by at least one Object-derived type, and the GameObject type.<\/div>\n<\/div>\n<h3>Collection<\/h3>\n<div>\n<div>Shown in dropdown menus of Init parameters that implement IEnumerable, such as <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.collections.generic.list-1\">lists<\/a> and <a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/language-reference\/builtin-types\/arrays\">arrays<\/a>.<\/div>\n<\/div>\n<h3>Service<\/h3>\n<p>Shown in dropdown menus of Init parameters whose type is the defining type of a Service (registered using the <a href=\"https:\/\/docs.sisus.co\/init-args\/features\/services\/\">ServiceAttribute<\/a>).<\/p>\n<blockquote><p>Note that you can add multiple constraints to one ValueProviderMenu attribute using the | operator.<\/p><\/blockquote>\n<h2>Type Constraints<\/h2>\n<p>You can also provide one or more Type arguments in the ValueProviderMenu attribute&#8217;s constructor, to constrain it to only appear in the dropdown menus of Init parameters that match those types.<\/p>\n<h2>Not Type Constraints<\/h2>\n<p>You can also assign a Type to the Not property in the ValueProviderMenu attribute&#8217;s constructor, to constrain it to NOT appear in the dropdown menus of Init parameters whose type matches that type.<\/p>\n<p>If you would like to stop it from appearing in the dropdown menus of more than one types of Init parameters, then you can assign an array of types to the NotAny property instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The [ValueProviderMenu] attribute can be used to introduce new menu items that will be shown in the dropdown menus for all Init arguments of targeted types. The attribute can be added to a ScriptableObject-derived class that implements IValueProvider&lt;TValue&gt; or IValueByTypeProvider. You can then use the Value property to access the value stored inside the field. ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/docs.sisus.co\/init-args\/initializers\/value-provider-menu\/\" 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":[12],"tags":[],"_links":{"self":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/422"}],"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=422"}],"version-history":[{"count":15,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/422\/revisions"}],"predecessor-version":[{"id":607,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/posts\/422\/revisions\/607"}],"wp:attachment":[{"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/media?parent=422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/categories?post=422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.sisus.co\/init-args\/wp-json\/wp\/v2\/tags?post=422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}