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
1
Question by casimps1 · Mar 08, 2012 at 05:21 PM · arrayinspectorenumindex

Making a user-friendly array with indices based on an enum

I've been wondering if there is any way in Unity to make nice user-friendly (i.e. - that have a simple and intuitive interface in the Inspector) arrays where the indices are based on an enum. I'm trying to make something that is both intuitive for non-programmers who will be entering and editing the data, and for programmers who will be accessing the data from script.

Here's a trivial example. If I were making a data structure for fruits, currently I might do something like this:

 public enum FruitID {
    Apple,
    Orange,
    Grape,
    Banana
 }
 
 public class FruitInfo {
    public FruitID id;
    public string name;
    public string description;
 }

 public FruitInfo[] fruits;

 void Start( ) {
    Debug.Log( "Grape description: " + fruits[ ( int )FruitID.Grape ].description );
 }

But this all seems very inelegant and I'm really not happy with the solution. The id field is either unnecessary or unwieldy and inefficient. If I assume that the data will always have the id field set correctly to match the index, then the id field is really kind of pointless. I wouldn't want to trust that anyway.

However, if I assume that the array may actually be unordered, then I have to do some kind of foreach search through the array to find the element that actually matches the id I want. That's unwieldy and inefficient.

Ideally, I would like to be able to take an array in the inspector, pick an enum that will be the index into that array and then have it automatically create and name enough elements to match the enum. That way the data entry people don't have to enter the enum index and there is no chance for error. I feel like this would involve some custom editor work, but I'm not sure if it's even possible.

Any ideas on how to do what I mentioned above or another elegant way to implement a system like this?

Comment
Add comment · Show 3
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 SirGive · Mar 08, 2012 at 05:45 PM 0
Share

$$anonymous$$aybe try looking into a dictionary?

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

avatar image casimps1 · Mar 08, 2012 at 06:11 PM 0
Share

true, I always forget about Dictionary... but they don't show up in the Inspector and that's one of my requirements so that designers can input the data easily. Also, Dictionary has significantly slower performance than a standard array.

avatar image Bunny83 · Mar 08, 2012 at 06:16 PM 0
Share

Sure, they don't show up in the inspector by default, but you can create your own custom inspector for your class.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by idbrii · Feb 22, 2018 at 07:53 PM

You can do this with an Attribute and PropertyDrawer:

 using UnityEngine;

 #if UNITY_EDITOR
 using System;
 using UnityEditor;
 #endif

 // Defines an attribute that makes the array use enum values as labels.
 // Use like this:
 //      [NamedArray(typeof(eDirection))] public GameObject[] m_Directions;

 public class NamedArrayAttribute : PropertyAttribute {
     public Type TargetEnum;
     public NamedArrayAttribute(Type TargetEnum) {
         this.TargetEnum = TargetEnum;
     }
 }

 #if UNITY_EDITOR
 [CustomPropertyDrawer(typeof(NamedArrayAttribute))]
 public class NamedArrayDrawer : PropertyDrawer {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
         // Properly configure height for expanded contents.
         return EditorGUI.GetPropertyHeight(property, label, property.isExpanded);
     }
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
         // Replace label with enum name if possible.
         try {
             var config = attribute as NamedArrayAttribute;
             var enum_names = System.Enum.GetNames(config.TargetEnum);
             int pos = int.Parse(property.propertyPath.Split('[', ']')[1]);
             var enum_label = enum_names.GetValue(pos) as string;
             // Make names nicer to read (but won't exactly match enum definition).
             enum_label = ObjectNames.NicifyVariableName(enum_label.ToLower());
             label = new GUIContent(enum_label);
         } catch {
             // keep default label
         }
         EditorGUI.PropertyField(position, property, label, property.isExpanded);
     }
 }
 #endif

Credit to JohnnyA who provided a starting point to figure this out.

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 GuyTidhar · Mar 08, 2012 at 06:50 PM

There is a semi-solution, do this:

 [System.Serializable]
 public enum FruitID {
    Apple,
    Orange,
    Grape,
    Banana
 }
 
 [System.Serializable]
 public class FruitInfo {
    public string name;
    public FruitID id;
    
    public string description;
 }
 public class items : MonoBehaviour {
     public FruitInfo[] fruits;
 }

The difference is that the name of the fruit which is a string type comes first, as the first FruitInfo member. What unity does is , replace the 'Element 0', 'Element 1' with the name you list in that string. So you will need to right the name 'Apple" next to the enum FruitID.Apple, so that you will actually see the name and not the element index.

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

8 People are following this question.

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

Related Questions

Rename array element name in inspector with enum 0 Answers

Array with multiple variables in Inspector 1 Answer

Enum drop down menu in inspector for nested arrays 2 Answers

Creating enum using a string array 3 Answers

populate array values automatically in inspector 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