Last active
February 16, 2020 19:47
-
-
Save wowbroforce/5122d54cf06ae5b62965636cd80e5aae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// How to use | |
[StringToEnum("None", "Road", "House", "Water")] | |
public string menuEntry; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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