Skip to main content

EnumValues Sample

A walker crosses surface tiles, and each surface's color, trail and speed come from tables in the Inspector.

The walker crosses different surfaces and leaves a continuous colored trail.The walker crosses different surfaces and leaves a continuous colored trail.

The walker crosses different surfaces and leaves a continuous colored trail.

Open it​

  1. Import the sample: Tools → Aspid 🐍 → FastTools → Welcome → Samples → Import on EnumValues.
  2. Open Scenes/EnumValues.unity and enter Play Mode: the walker speeds up on metal, slows down on wet and soft tiles, and leaves a trail in each surface's color.

Try​

1. Enum fixed in code​

Select Data/SurfacePalette.asset and change Grass in Tile Colors: the tiles recolor at once.

Tile Colors and Footprint Colors, each with its own Default Value.Tile Colors and Footprint Colors, each with its own Default Value.

Tile Colors and Footprint Colors, each with its own Default Value.

[SerializeField] private EnumValues<SurfaceType, Color> _tileColors;

public Color GetTileColor(SurfaceType surface) =>
_tileColors.GetValue(surface);

2. Default value​

Remove the Stone row from Footprint Colors and enter Play Mode: the trail on stone takes the table's Default Value.

3. Enum picked in the Inspector​

Select Walker: the code of Speed By Terrain sets only the value type, and the TerrainFlags enum is picked in the table header.

[SerializeField] private EnumValues<float> _speedByTerrain;

var speed = _speed * (_tile == null
? 1f
: _speedByTerrain.GetValue(_tile.Flags));

4. [Flags] keys​

GetValue returns the value of one row:

KeyRowMultiplier
Wet, Slipperythe exact row0.5
Wet, Hotthe first row inside the key, Wet0.8
Noneno row, Default Value1

5. A new enum member​

Add Ice to SurfaceType and set one tile's Surface to Ice: the tile and its trail take the default values.

Where to look​

FileShows
Scripts/SurfacePalette.csEnumValues<TEnum, TValue> on a ScriptableObject
Scripts/Walker.csEnumValues<TValue> with a [Flags] key

Reference: EnumValues.