A Unity package for associating a value with each case of an enum. Allows setting values in the inspector and serialization.
The quickest way is to install via OpenUPM
using openupm add dev.comradevanti.enum-dict
.
Or install manually
as git dependency
from https://github.com/ComradeVanti/UnityEnumDict.git
or download as zip
and import locally.
Works for Unity 2020.x and upwards.
Say, you want to associate a movement-force for a rigidbody with each possible movement state. You could do this by first defining an enum for it like this.
public enum MovementState
{
Walking,
Crouching,
Running
}
Next create a variable for the EnumDict
in your script. All serializable types
are valid, such as strings, serializable classes and structs or in this case,
floats.
public class MyScript : MonoBehaviour
{
public EnumDict<MovementState, float> movementForces;
}
You can then edit it from the inspector.
To access the value for a specific enum-case, use the indexer syntax.
// 0.5
var force = movementForces[MovementState.Crouching];