Skip to content

Instantly share code, notes, and snippets.

@wowbroforce
Last active February 16, 2020 19:47
Show Gist options
  • Save wowbroforce/5122d54cf06ae5b62965636cd80e5aae to your computer and use it in GitHub Desktop.
Save wowbroforce/5122d54cf06ae5b62965636cd80e5aae to your computer and use it in GitHub Desktop.
// How to use
[StringToEnum("None", "Road", "House", "Water")]
public string menuEntry;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.AttributeUsage(System.AttributeTargets.Field)]
public class StringToEnumAttribute : PropertyAttribute
{
public string[] values;
public StringToEnumAttribute(params string[] values)
{
this.values = values;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(StringToEnumAttribute))]
public class StringToEnumDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!(attribute is StringToEnumAttribute att)) return;
var index = System.Array.IndexOf(att.values, property.stringValue);
if (index < 0) index = 0;
EditorGUI.BeginChangeCheck();
index = EditorGUI.Popup(position, property.displayName, index, att.values);
if (EditorGUI.EndChangeCheck())
property.stringValue = att.values[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment