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<TValue> or IValueByTypeProvider.
You can then use the Value property to access the value stored inside the field.
Example
[ValueProviderMenu("Hierarchy/Get Component", Is.SceneObject), CreateAssetMenu] class GetComponent : ScriptableObject, IValueByTypeProvider { public bool TryGetFor<TValue>(Component client, out TValue value) { if(client == null) { value = default; return false; } return Find.In(client.gameObject, out value); } }
Shared Value Provider
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.
[CreateAssetMenu, ValueProviderMenu("Shared Value Provider", typeof(string))] class SharedValueProvider : ScriptableObject, IValueProvider<string> { public string Value => "Value"; }
This can be useful for conserving memory, in cases where only a single value provider is needed.
Embedded Value Providers
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.
[ValueProviderMenu("Embedded Value Provider", typeof(string))] class EmbeddedValueProvider : ScriptableObject, IValueProvider<string> { [SerializeField] string value; public string Value => value; }
This makes it possible to configure each value provider separately.
Is Constraints
The following Is constraints can be applied for the targets of value providers with the ValueProviderMenu attribute:
Unconstrained
Class
ValueType
Concrete
Abstract
Interface
BuiltIn
Component
WrappedObject
SceneObject
Asset
Collection
Service
Shown in dropdown menus of Init parameters whose type is the defining type of a Service (registered using the ServiceAttribute).
Note that you can add multiple constraints to one ValueProviderMenu attribute using the | operator.
Type Constraints
You can also provide one or more Type arguments in the ValueProviderMenu attribute’s constructor, to constrain it to only appear in the dropdown menus of Init parameters that match those types.
Not Type Constraints
You can also assign a Type to the Not property in the ValueProviderMenu attribute’s constructor, to constrain it to NOT appear in the dropdown menus of Init parameters whose type matches that type.
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.