Game.EnumValueAttribute
Assembly: Assembly-CSharp.dll
Namespace: Game.Common
Type: class
Base: UnityEngine.PropertyAttribute
Summary:
Attribute that captures the names of an enum type and exposes them via a public string[] (names). Intended to be used as a Unity PropertyAttribute so custom property drawers or serialization helpers can display or map enum options by name. The constructor takes a System.Type expected to be an enum type and populates names using Enum.GetNames(type). Note: no runtime validation is performed in the class itself—Enum.GetNames will throw if the provided type is null or not an enum.
Fields
public string[] names
Holds the string names of the enum values retrieved from the provided enum Type (via Enum.GetNames). These names can be used by custom inspectors/property drawers to build popup lists, labels, or mappings. If the constructor receives null or a non-enum Type, Enum.GetNames will throw (ArgumentNullException or ArgumentException).
Properties
- None
Constructors
public EnumValueAttribute(Type type)
Initializes the attribute and fills the names array with Enum.GetNames(type). The provided Type should be an enum Type; otherwise an exception is thrown by Enum.GetNames.
Methods
- None
Usage Example
// Define an enum
public enum BuildingCategory
{
Residential,
Commercial,
Industrial,
Park
}
// Use the attribute on a field (commonly an int storing the selected index)
public class ExampleBehaviour : MonoBehaviour
{
[EnumValue(typeof(BuildingCategory))]
public int buildingCategoryIndex;
}
// Typical custom property drawer (Editor-only) that reads EnumValueAttribute.names
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(EnumValueAttribute))]
public class EnumValueAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var attr = (EnumValueAttribute)attribute;
var names = attr.names ?? new string[] { "<invalid enum>" };
// Assuming the field is stored as an int index
int current = property.intValue;
current = EditorGUI.Popup(position, label.text, current, names);
property.intValue = current;
}
}
#endif