Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by frenchfaso · Mar 25, 2012 at 01:35 PM · serializedpropertyenumpopup

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();
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
6

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));
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jean-Fabre · Dec 08, 2015 at 08:08 AM 0
Share

Odd, it doesn't work for me, maybe because of the way enums are defined?

avatar image
3

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image frenchfaso · Jul 03, 2012 at 02:12 PM 1
Share

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));

avatar image VelociraptorGames frenchfaso · Jun 15, 2017 at 02:06 PM 0
Share

Superb, Works for me!

avatar image
3

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));


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image frenchfaso · Jul 03, 2012 at 02:14 PM 0
Share

tnx for Your answer too! it also helped me find the solution.

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges