Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by WF_Bart · Mar 02, 2016 at 05:33 PM · editorinspectoreditorguilayoutenumpopup

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

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

3 Replies

· Add your reply
  • Sort: 
avatar image
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.

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 Gru · Mar 29, 2016 at 05:02 PM 0
Share

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.

avatar image
0

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'.

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
0

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

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

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

58 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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