I’ve been wondering if there is any way in Unity to make nice user-friendly (i.e. - that have a simple and intuitive interface in the Inspector) arrays where the indices are based on an enum. I’m trying to make something that is both intuitive for non-programmers who will be entering and editing the data, and for programmers who will be accessing the data from script.
Here’s a trivial example. If I were making a data structure for fruits, currently I might do something like this:
public enum FruitID {
Apple,
Orange,
Grape,
Banana
}
public class FruitInfo {
public FruitID id;
public string name;
public string description;
}
public FruitInfo[] fruits;
void Start( ) {
Debug.Log( "Grape description: " + fruits[ ( int )FruitID.Grape ].description );
}
But this all seems very inelegant and I’m really not happy with the solution. The id field is either unnecessary or unwieldy and inefficient. If I assume that the data will always have the id field set correctly to match the index, then the id field is really kind of pointless. I wouldn’t want to trust that anyway.
However, if I assume that the array may actually be unordered, then I have to do some kind of foreach search through the array to find the element that actually matches the id I want. That’s unwieldy and inefficient.
Ideally, I would like to be able to take an array in the inspector, pick an enum that will be the index into that array and then have it automatically create and name enough elements to match the enum. That way the data entry people don’t have to enter the enum index and there is no chance for error. I feel like this would involve some custom editor work, but I’m not sure if it’s even possible.
Any ideas on how to do what I mentioned above or another elegant way to implement a system like this?
You can do this with an Attribute and PropertyDrawer:
using UnityEngine;
#if UNITY_EDITOR
using System;
using UnityEditor;
#endif
// Defines an attribute that makes the array use enum values as labels.
// Use like this:
// [NamedArray(typeof(eDirection))] public GameObject[] m_Directions;
public class NamedArrayAttribute : PropertyAttribute {
public Type TargetEnum;
public NamedArrayAttribute(Type TargetEnum) {
this.TargetEnum = TargetEnum;
}
}
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(NamedArrayAttribute))]
public class NamedArrayDrawer : PropertyDrawer {
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
// Properly configure height for expanded contents.
return EditorGUI.GetPropertyHeight(property, label, property.isExpanded);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
// Replace label with enum name if possible.
try {
var config = attribute as NamedArrayAttribute;
var enum_names = System.Enum.GetNames(config.TargetEnum);
int pos = int.Parse(property.propertyPath.Split('[', ']')[1]);
var enum_label = enum_names.GetValue(pos) as string;
// Make names nicer to read (but won't exactly match enum definition).
enum_label = ObjectNames.NicifyVariableName(enum_label.ToLower());
label = new GUIContent(enum_label);
} catch {
// keep default label
}
EditorGUI.PropertyField(position, property, label, property.isExpanded);
}
}
#endif
[System.Serializable]
public enum FruitID {
Apple,
Orange,
Grape,
Banana
}
[System.Serializable]
public class FruitInfo {
public string name;
public FruitID id;
public string description;
}
public class items : MonoBehaviour {
public FruitInfo[] fruits;
}
The difference is that the name of the fruit which is a string type comes first, as the first FruitInfo member. What unity does is , replace the ‘Element 0’, ‘Element 1’ with the name you list in that string. So you will need to right the name 'Apple" next to the enum FruitID.Apple, so that you will actually see the name and not the element index.