2. Value Providers

  6. Initialization No Comments

Value providers are a highly flexible system that can be used to provide Init arguments to initializers dynamically at runtime.

Creating Value Providers

To make a new value provider, create a ScriptableObject that implements IValueProvider<TValue> or IValueByTypeProvider.

In order to be able to create assets from your value provider, you’ll also need to add the [CreateAssetMenu] attribute to it.

[CreateAssetMenu]
class RandomNameProvider : ScriptableObject, IValueProvider<string>
{
    [SerializeField] string[] names;

    public string Value => names[Random.Range(0, names.Length)];
}

Using Value Providers

To use a value provider, simply drag-and-drop an asset created from one into an Init argument field in the Inspector.

Leave a Reply

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