06. ValueProviderMenu

  04. Features No Comments

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

[CreateAssetMenu] // <- Create a single asset from the class
[ValueProviderMenu("Hierarchy/Get Component", Is.SceneObject)]
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 Asset

If you create a single asset from the value provider class 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.

This can be useful for preserving memory, in cases where only a single value provider is needed.

Separate Value Providers

If 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.

This can be useful when you want to add serialized fields to the value provider, which each client can then use to configure the value provider to fit their individual needs.

Is Constraints

The following Is constraints can be applied for the targets of value providers with the ValueProviderMenu attribute:

Unconstrained

No constraints applied to the types of Init parameters targeted.

Class

Shown in dropdown menus of reference type Init parameters.

ValueType

Shown in dropdown menus of struct type Init parameters.

Concrete

Shown in dropdown menus of concrete type Init parameters.

Abstract

Shown in dropdown menus of abstract type Init parameters.

Interface

Shown in dropdown menus of interface type Init parameters.

BuiltIn

Shown in dropdown menus of Init parameters of built-in C# types, such as bool, int, float and string.

Component

Shown in dropdown menus of component type Init parameters.
This includes both all component-derived types, as well as any interface types which are implemented by at least one component-derived type.

WrappedObject

Shown in dropdown menus of parameters of plain old C# types that have a Wrapper class.

SceneObject

Shown in dropdown menus of Init parameters of types that can exist as part of a scene.
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.

Asset

Shown in dropdown menus of Init parameters of types that can exist as a standalone asset, or as part of a prefab.
This includes all Object-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.

Collection

Shown in dropdown menus of Init parameters that implement IEnumerable, such as lists and arrays.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *