- Home /
SerializedProperty for Enum with EnumPopup
Hy folks, I can't manage to use EnumPopup with a SerializedProperty here is the code:
[UnityEditor.CustomEditor(typeof(MyClass))]
public class MyClassCustomEditor : Editor {
SerializedProperty mySerProp;
void OnEnable () {
mySerProp = serializedObject.FindProperty("mySerProp");
}
override public void OnInspectorGUI () {
serializedObject.Update();
// here is the problem, the example in the docs is only javascript...
// I get 2 errors:
// The best overloaded method match for `UnityEditor.EditorGUILayout.EnumPopup(string, System.Enum, params UnityEngine.GUILayoutOption[])' has some invalid arguments
// and:
// Argument `#2' cannot convert `UnityEditor.SerializedProperty' expression to type `System.Enum'
// I've tried casting but without results..
mySerProp = EditorGUILayout.EnumPopup("My Enum:", mySerProp);
serializedObject.ApplyModifiedProperties();
}
}
Answer by kamil.slawicki · Oct 05, 2012 at 11:30 AM
Hi guys, I know it's quite late but how about this:
EditorGUILayout.PropertyField( mySerProp );
It worked fine for me. You get a proper enum drop down from it too.
Answer by frenchfaso · Jul 03, 2012 at 02:16 PM
after really long long time... here is what worked for me:
mySerProp.enumValueIndex = (int)(MyEnumType)EditorGUILayout.EnumPopup("My Enum:", (MyEnumType)Enum.GetValues(typeof(MyEnumType)).GetValue(mySerProp.enumValueIndex));
Odd, it doesn't work for me, maybe because of the way enums are defined?
Answer by Gilgrum · May 16, 2012 at 01:55 AM
I know I'm posting quite late on this questions, but I'm trying to do the same thing. I have a very ugly solution that works. I'm posting it in case it is useful to someone else:
1) You have to have the enum type handy. For example we'll say mySerProp is an enum of called MyEnumType.
2) The EnumPopup line now looks like:
mySerProp = (int)(MyEnumType)EditorGUILayout.EnumPopup("My Enum:", (MyEnumType)mySerProp);
If someone else has a more elegant answer to this, I would be very interested in seeing it.
sorry for the eternal delay :-) tnx for the answer, didn't work right 'outofthebox' for me, but led me to the solution:
mySerProp.enumValueIndex = (int)($$anonymous$$yEnumType)EditorGUILayout.EnumPopup("$$anonymous$$y Enum:", ($$anonymous$$yEnumType)Enum.GetValues(typeof($$anonymous$$yEnumType)).GetValue(mySerProp.enumValueIndex));
Answer by unityBerserker · Dec 27, 2016 at 12:44 PM
Hi, here is full method maybe it will help someone in future. First you must get your property:
SerializedProperty ourProperty = serializedObject.FindProperty(propertyPath:"nameOfPropertyInInspector");
From where get property path? Set inspector to debug mode next put mouse coursor under name of field and press alt key - unity display name of field in code - you can use it as propertyPath.
Here is a method for set popup:
void AddPopup (ref SerializedProperty ourSerializedProperty, string nameOfLabel, Type typeOfEnum)
{
Rect ourRect = EditorGUILayout.BeginHorizontal ();
EditorGUI.BeginProperty (ourRect, GUIContent.none, ourSerializedProperty);
EditorGUI.BeginChangeCheck ();
int actualSelected = 1;
int selectionFromInspector = ourSerializedProperty.intValue;
string[] enumNamesList = System.Enum.GetNames (typeOfEnum);
actualSelected = EditorGUILayout.Popup (nameOfLabel, selectionFromInspector, enumNamesList);
ourSerializedProperty.intValue = actualSelected;
EditorGUI.EndProperty ();
EditorGUILayout.EndHorizontal ();
}
Example of how to invoke this method?
SerializedProperty lightProbes = serializedObject.FindProperty ("m_LightProbeUsage");
AddPopup (ref lightProbes, "Light Probes", typeof(LightProbeUsage));
Answer by dylanfries · Jun 29, 2012 at 01:56 PM
The Enum popup just returns an int (which should be the index or an array or List or whatever) then you can manually update the value using that index.
tnx for Your answer too! it also helped me find the solution.
Your answer
Follow this Question
Related Questions
Editor: How to do PropertyField for List elements? 4 Answers
Display Custom Inspectors for each class in a List<> 1 Answer
Serializing an asset as a serialized property 0 Answers
Serializedproperty assignment 2 Answers
Calling ApplyModifiedProperties results in other variables resetting to 0? 0 Answers