Перейти к основному содержимому

Class EnumValues<TEnum, TValue>

Namespace: Aspid.FastTools.Enums
Assembly: Aspid.FastTools.dll

A serializable dictionary that maps members of TEnum to values of type TValue. The typed counterpart of EnumValues<T> for the common case where the enum type is known at compile time — the Inspector type-picker is read-only, and lookups are compile-time safe.

[Serializable]
public sealed class EnumValues<TEnum, TValue> : IEnumerable<KeyValuePair<TEnum, TValue?>>, IEnumerable, ISerializationCallbackReceiver where TEnum : struct, Enum

Type Parameters​

TEnum

The enum type the entries are keyed by.

TValue

The type of the value associated with each enum member.

Inheritance​

object ← EnumValues<TEnum, TValue>

Implements​

IEnumerable<KeyValuePair<TEnum, TValue?>>, IEnumerable, ISerializationCallbackReceiver

Extension Methods​

TextInputBaseFieldTextSelectionExtensions.AddOnCursorIndexChange<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, Action), TextInputBaseFieldTextSelectionExtensions.AddOnSelectIndexChange<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, Action), INotifyValueChangedExtensions.AddValueChanged<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, EventCallback<ChangeEvent<TValue>>), ProfilerMarkerExtensionsForGenerator.Marker<EnumValues<TEnum, TValue>>(EnumValues<TEnum, TValue>), TextInputBaseFieldTextSelectionExtensions.RemoveOnCursorIndexChange<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, Action), TextInputBaseFieldTextSelectionExtensions.RemoveOnSelectIndexChange<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, Action), INotifyValueChangedExtensions.RemoveValueChanged<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, EventCallback<ChangeEvent<TValue>>), TextInputBaseFieldExtensions.SetAutoCorrection<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldTextSelectionExtensions.SetCursorIndex<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, int), TextInputBaseFieldExtensions.SetDelayed<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldTextSelectionExtensions.SetDoubleClickSelectsWord<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldExtensions.SetHideMobileInput<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldExtensions.SetHidePlaceholderOnFocus<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldExtensions.SetHideSoftKeyboard<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), SliderExtensions.SetHighValue<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, TValue), TextInputBaseFieldExtensions.SetKeyboardType<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, TouchScreenKeyboardType), BaseFieldExtensions.SetLabel<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, string), SliderExtensions.SetLowValue<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, TValue), TextInputBaseFieldExtensions.SetMaskChar<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, char), TextInputBaseFieldExtensions.SetMaxLength<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, int), TextInputBaseFieldExtensions.SetPassword<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldExtensions.SetPlaceholder<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, string), TextInputBaseFieldExtensions.SetReadOnly<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldTextSelectionExtensions.SetSelectAllOnFocus<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldTextSelectionExtensions.SetSelectAllOnMouseUp<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldTextSelectionExtensions.SetSelectIndex<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, int), TextInputBaseFieldTextSelectionExtensions.SetSelectable<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), TextInputBaseFieldTextSelectionExtensions.SetTripleClickSelectsLine<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, bool), INotifyValueChangedExtensions.SetValue<EnumValues<TEnum, TValue>, TValue>(EnumValues<TEnum, TValue>, TValue, bool)

Examples​

Map a damage type to a color, with the enum fixed at compile time:

public class HitEffect : MonoBehaviour
{
[SerializeField] private EnumValues<DamageType, Color> _damageColors;

public Color GetColor(DamageType type) =>
_damageColors.GetValue(type);
}

Remarks​

Lookup semantics (including [Flags] handling) are identical to EnumValues<T> — see its remarks for details. Steady-state EnumValues<T1, T2>.GetValue, EnumValues<T1, T2>.Equals and foreach (which binds to the struct EnumValuesEnumerator<T1, T2>) never allocate.

In the editor the serialized layout is compatible with EnumValues<T>: the enum type is still stored in a hidden editor-only _enumType field, auto-filled with TEnum's assembly-qualified name on serialization. Switching a field between the two variants therefore migrates existing data, as long as the configured enum type matches TEnum. Player builds strip the field — at runtime the enum type comes from the generic argument alone.

Internal hot paths are wrapped in profiler markers; define the ASPID_FAST_TOOLS_UNITY_PROFILER_DISABLED scripting symbol to compile them out.

Methods​

Equals(TEnum, TEnum)​

Determines whether two enum values should be considered equal for lookup purposes. The first argument is the value being looked up; the second is the entry's stored key.

public bool Equals(TEnum enumValue1, TEnum enumValue2)

Parameters​

enumValue1 TEnum

The lookup value (must contain the entry's bits to match).

enumValue2 TEnum

The stored entry key.

Returns​

bool

For regular enums: true when both values are identical.
For [Flags] enums: true when enumValue1 has all bits of enumValue2 set, with the additional rule that the zero (None) value is only equal to another zero value.

GetEnumerator()​

Returns a struct enumerator over the explicitly configured (key, value) pairs in serialized order — foreach binds to it directly and does not allocate. Does not include the default value or entries with an unresolved key.

public EnumValuesEnumerator<TEnum, TValue> GetEnumerator()

Returns​

EnumValuesEnumerator<TEnum, TValue>

GetValue(TEnum)​

Returns the value mapped to enumValue, or the configured default value if no mapping exists.

public TValue? GetValue(TEnum enumValue)

Parameters​

enumValue TEnum

The enum member to look up.

Returns​

TValue?

The mapped value, or the default value when no entry matches. A reference-type TValue left unassigned in the Inspector is null.