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 /
  • Help Room /
avatar image
0
Question by Haedrian · Oct 31, 2021 at 12:54 PM · inspectorenum

Manage lots of enum choices in inspector

I'm writing a RPG, and to keep track of individual item types (ex: each weapon, each resource &c), I use an enum.

At the moment I have 90 or so entries, and so when I need to find a specific item type in the inspector, it's a bit of a headache. The inspector supports searching by the first character (and only that), but that's not that helpful

Is there a way of managing enum values in the inspector - such as being able to group them, or get a better search instead of going through the dropdown?

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

1 Reply

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

Answer by Hellium · Oct 31, 2021 at 01:24 PM

Using an enum might not be the best solution in your case. But I don't know your system enough to judge.


Anyway, InspectorName attribute lets you group enum values.

 public enum Item
 {
     [InspectorName("Weapon/Melee/Sword")]
     Sword,
     [InspectorName("Weapon/Melee/Axe")]
     Axe,
     [InspectorName("Weapon/Ranged/Bow")]
     Bow,
     [InspectorName("Weapon/Magic/Wand")]
     Wand,
     [InspectorName("Weapon/Magic/Staff")]
     Staff,
     [InspectorName("Equipment/Helmet")]
     Helmet,
     [InspectorName("Equipment/Armor")]
     Armor,
     [InspectorName("Consumable/HealthPotion")]
     HealthPotion,
     [InspectorName("Consumable/ManaPotion")]
     ManaPotion,
     [InspectorName("Consumable/Food")]
     Food,
     Coin
 }

alt text


If you want the enum to be searchable, you'll need more code:

 // SearchableAttribute.cs in your Assets folder

 using UnityEngine;

 public class SearchableAttribute : PropertyAttribute { }


 // SearchableAttributeDrawer.cs >>> IN AN 'Editor' FOLDER <<<

 using System;
 using UnityEngine;
 using UnityEditor;

 [CustomPropertyDrawer(typeof(SearchableAttribute))]
 public class SearchableAttributeDrawer : PropertyDrawer
 {
     private string search;
     private string[] options;
     private GUIStyle searchTextFieldStyle;
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         float height = base.GetPropertyHeight(property, label);
 
         if (property.propertyType == SerializedPropertyType.Enum)
             height = height * 2 + EditorGUIUtility.standardVerticalSpacing;
 
         return height;
     }
 
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         if (property.propertyType == SerializedPropertyType.Enum)
         {
             if (searchTextFieldStyle == null)
                 searchTextFieldStyle = GUI.skin.FindStyle("ToolbarSeachTextField");
 
             if (options == null)
                 UpdateOptions(property.enumDisplayNames);
 
             Rect searchRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
             DrawSearchBar(searchRect, label, property.enumDisplayNames);
 
             Rect popupRect = new Rect(position.x, searchRect.y + searchRect.height + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUIUtility.singleLineHeight);
             DrawEnumPopup(popupRect, property);
         }
         else
         {
             EditorGUI.PropertyField(position, property, label);
         }
     }
 
     private void DrawSearchBar(Rect position, GUIContent label, string[] allOptions)
     {
         EditorGUI.BeginChangeCheck();
         search = EditorGUI.TextField(position, label, search, searchTextFieldStyle);
         if(EditorGUI.EndChangeCheck())
             UpdateOptions(allOptions);
     }
 
     private void DrawEnumPopup(Rect position, SerializedProperty property)
     {
         Rect fieldRect = EditorGUI.PrefixLabel(position, new GUIContent(" "));
         int currentIndex = Array.IndexOf(options, property.enumDisplayNames[property.enumValueIndex]);
         int selectedIndex = EditorGUI.Popup(fieldRect, currentIndex, options);
         if (selectedIndex >= 0)
         {
             int newIndex = Array.IndexOf(property.enumDisplayNames, options[selectedIndex]);
             if (newIndex != currentIndex)
             {
                 property.enumValueIndex = newIndex;
                 search = string.Empty;
                 UpdateOptions(property.enumDisplayNames);
             }
         }
     }
 
     private void UpdateOptions(string[] allOptions)
     {
         options = Array.FindAll(allOptions, name => string.IsNullOrEmpty(search) || name.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) >= 0);
     }
 }


 // In your code

 [Searchable]
 public Item Item;

alt text


enuminspectorname.png (5.6 kB)
enumsearch.png (7.0 kB)
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 Haedrian · Oct 31, 2021 at 03:31 PM 0
Share

The inspector name is incredibly helpful. Thank you so much.

Not sure about the enums myself either, but if I need to make references in crafting (ex: sword needs 5 ingots, and 3 coal), I thought enums would be the safest way to keep references.

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

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

Related Questions

How to limit the types shown at the inspector in a public enum based on another public enum type selected? 1 Answer

Why does rotating via viewport give different values than inspector rotations? 2 Answers

Collider is deleting itself when I press play 0 Answers

Is there a way for a public/serializedfield to be uneditable and just grayed out in the inspector? 1 Answer

assign variable by inspector failed... 2 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