How do I use EditorGUILayout.EnumPopup with an enum with 'holes' in a custom inspector.
I'm trying to create an enum property in a custom inspector which I've done succesfully before, however this time I'm hitting a snag and I wonder if I'm doing something wrong or if it's a bug.
This is my inspector code:
override public void OnInspectorGUI()
{
serializedObject.Update();
hovercursor.enumValueIndex = (int)(CursorType)EditorGUILayout.EnumPopup("Cursor:", (CursorType)hovercursor.enumValueIndex);
serializedObject.ApplyModifiedProperties();
}
This seems to work for a normal enum, but this particular enum looks like this:
public enum CursorType
{
Default = 1,
Walk = 3,
Talk = 5,
ZoomLocation = 7,
ZoomObject = 9,
Camera = 11,
}
When I change the enum in the inspector I get the wrong values. Selecting 'default' gives me 'walk', 'walk' gives me 'zoom location' and 'zoom location' gives me
enum index is out of range
UnityEditor.SerializedProperty:set_enumValueIndex(Int32)
So basically it's (incorrectly) creating the popup with values 0,1,2,3,4,5, and then when you make a selection assigning the values according to the enum (which gives an out of range error over 5).
Answer by WF_Bart · Mar 02, 2016 at 05:37 PM
I actually managed to solve the problem by using
hovercursor.intValue = (int)(CursorType)EditorGUILayout.EnumPopup("Cursor:", (CursorType)hovercursor.intValue);
and foregoing 'enumValueIndex' altogether.
I'm still putting up the question because I wonder if this is how you're supposed to do it, and maybe this might help others.
Thanks for sharing. For those who use EditorGUI.Enum$$anonymous$$askField there seems to be no good way to assign values without using intValue like you did.
Answer by PinkTsunami · Jul 24, 2016 at 07:48 PM
Hi fellow, I think that problem is using 'enumValueIndex' doesn't help anything. I suggest you to store string and cast it later when you have to use, instead store it as enum.
SerializedProperty mySerializedProperty = ((myEnum)EditorGUILayout.EnumPopup(
(myEnum)System.Enum.Parse(typeof(myEnum), "FirstElementOfEnum"))).ToString();
try
{
//this must be failed at first time because string always init with blank
mySerializedProperty.stringValue = ((myEnum)EditorGUILayout.EnumPopup(
(EVTYPE)System.Enum.Parse(typeof(myEnum),
mySerializedProperty.stringValue))).ToString();
}
catch
{
Debug.Log("Fail. Use Default");
mySerializedProperty.stringValue = ((myEnum)EditorGUILayout.EnumPopup(
(myEnum)System.Enum.Parse(typeof(myEnum), "FirstElementOfEnum"))).ToString();
}
: "FirstElementOfEnum" must be implemented in myEnum
I'm still searching another ways, but humbly thought that there wouldn't be clean answer until unity makes proper enum casting function. This was the only way for me to keep using SerializedProperty + enumPopup with 'perforated enum'.
Answer by WoofyWyzpets · Apr 21, 2019 at 04:05 PM
if you want to use EditorGUI (lets you use your custom sized rect) instead of EditorGUILayout you can do something with EditorGUI.IntPopup combined with Enum.GetValues
Your answer
Follow this Question
Related Questions
Dynamically sized list of toggles in custom inspector? 0 Answers
EditorGUILayout.EnumPopup shows always the same selected option but works as programmed 1 Answer
Custom Editor List with child classes 1 Answer
GUILayout.Label with Texture2D on the same line in Inspector ? 1 Answer
Custom Inspector changes not updating target in Edit Mode 1 Answer