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 /
avatar image
0
Question by Metric · Jul 18, 2011 at 03:25 PM · editorarraysclassnameelement

Naming array elements in editor

I'm setting up a system to spawn enemies in my game. Really this is just a cosmetic issue but I would just like to know how to set it up properly anyway.

I've created a new class that holds information about a round of enemy spawns, and then I'm using an array to store this information so that it appears in the editor like this:

alt text

What I want is instead of it saying "Element 0" I would prefer it to say "Wave 0" and then "Wave 1" etc. I know it's just a cosmetic thing but it's gonna bug me until I get it right :)

I found I could add a string variable to the class with the name "name" and whatever I changed that variable to would update the text "element 0" to be that name, but I would like it to just handle this automatically if possible.

Also if setting this all up in an array is the wrong way to go about it or you have any other suggestions please let me know!

Thank you!

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
1
Best Answer

Answer by Eric5h5 · Jul 18, 2011 at 04:31 PM

You can write a custom inspector, which duplicates the array functionality and adds the ability to change the element names. (By the way, it seems like a bug that having a string in the class changes the element name, especially since it only works if the string variable is first.)

Comment
Add comment · Show 4 · 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 Metric · Jul 18, 2011 at 05:29 PM 0
Share

Thanks! I wasn't aware you could do that and after looking it up it looks perfect.

avatar image jahroy · Jul 19, 2011 at 04:00 AM 0
Share

If that's a bug I think we should just call it a feature...

I've found that using a 'name' property in custom classes is a handy way to make inspector values easier to read and/or more meaningful.

avatar image Eric5h5 · Jul 19, 2011 at 04:08 AM 0
Share

I can't agree; if I have an array in the inspector, I often prefer to know which element is which. If "Element 0", "Element 1", etc. are arbitrarily replaced by a string, that's usually not helpful to me. It's also inconsistent since it only happens if the first variable in the class is a string. If it's not a bug, it sure behaves like one.

avatar image jahroy · Jul 19, 2011 at 04:42 AM 0
Share

I'll agree to disagree, but I've always found it more useful to see a meaningful label (that I choose) to represent elements in my arrays.

For example, if it was an array of weapons that my hero could choose from I'd rather see "Battle Axe" and "Flamethrower" than "Element 1" and "Element 5".

According to this forum post, it occurs whenever the first serializable member of a class is a string:

http://forum.unity3d.com/threads/90477-ScriptableObject-asset-list-names-in-inspector?p=586791&viewfull=1#post586791

Not sure if that's true or not...

/tangent hijack

avatar image
12

Answer by JGriffith · Jun 21, 2013 at 09:18 AM

I believe I have somewhat of an answer to your question. And this is way out of date but in case someone is looking I'll post and example here...

I have a simple container class that just holds the name of something and if it has been used or not:

     [System.Serializable]
     public class DragonsClass 
     {
         public string Name;
         [HideInInspector]
         public bool bUsed;
     }
     
     public DragonsClass[] DragonsInLevel; 

By filling out the name varaibles in the inspector, the "element#" is replaced by the string I enter: alt text

No super useful for this example but you get the jist of an easy way to name your elements in the inspector.

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 whydoidoit · Jun 21, 2013 at 09:18 AM 2
Share

I believe it uses the first property to display the name if that property is a string.

avatar image
4

Answer by joaoborks · Mar 14, 2020 at 05:34 PM

If anyone else struggles with this, I've created a simple solution to name your array elements based on a given enum or array of strings:


- GitHub Gist Source

Code Samples:


Attribute:

 /**
  * LabeledArrayAttribute.cs
  * Created by: Joao Borks [joao.borks@gmail.com]
  * Created on: 28/12/17 (dd/mm/yy)
  * Reference from John Avery: https://forum.unity.com/threads/how-to-change-the-name-of-list-elements-in-the-inspector.448910/
  */
 
 using UnityEngine;
 using System;
 
 public class LabeledArrayAttribute : PropertyAttribute
 {
     public readonly string[] names;
     public LabeledArrayAttribute(string[] names) { this.names = names; }
     public LabeledArrayAttribute(Type enumType) { names = Enum.GetNames(enumType); }
 }

Property Drawer:

 /**
  * LabeledArrayDrawer.cs
  * Created by: Joao Borks [joao.borks@gmail.com]
  * Created on: 28/12/17 (dd/mm/yy)
  * Reference from John Avery: https://forum.unity.com/threads/how-to-change-the-name-of-list-elements-in-the-inspector.448910/
  */
 
 using UnityEngine;
 using UnityEditor;
 using System.Linq;
 
 // Don't forget to put this file inside an Editor folder!
 [CustomPropertyDrawer(typeof(LabeledArrayAttribute))]
 public class LabeledArrayDrawer : PropertyDrawer
 {
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return EditorGUI.GetPropertyHeight(property, label);
     }
 
     public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
     {
         EditorGUI.BeginProperty(rect, label, property);
         try
         {
             var path = property.propertyPath;
             int pos = int.Parse(path.Split('[').LastOrDefault().TrimEnd(']'));
             EditorGUI.PropertyField(rect, property, new GUIContent(((LabeledArrayAttribute)attribute).names[pos]), true);
         }
         catch
         {
             EditorGUI.PropertyField(rect, property, label, true);
         }
         EditorGUI.EndProperty();
     }
 }

Example:

 using UnityEngine;
 
 public class LabeledArrayExample : MonoBehaviour
 {
     [LabeledArray(new string[] { "First", "Second", "Third" })]
     public int[] labeledValues;
     
     public enum Order
     {
         First,
         Second,
         Third
     }
     
     [LabeledArray(typeof(Order))]
     public int[] enumLabeledValues;
 }

Comment
Add comment · Show 5 · 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 CloakingOcaen · Mar 14, 2020 at 11:23 PM 1
Share

Ah, I was able to get everything working! Although you may want to edit your example to use the Given GUILabel rather than the true value:

 return EditorGUI.GetPropertyHeight(property, true);
avatar image joaoborks CloakingOcaen · Mar 15, 2020 at 11:15 PM 0
Share

Glad it worked for you! I will incorporate your suggestion that into my answer. Thanks for pointing that out.

avatar image Mashimaro7 · Dec 17, 2020 at 11:45 AM 1
Share

I feel like an idiot, but I can't figure it out. I put the example in my script, and the editor script in the editor folder, but how do I decide which array I'm changing the names of?

avatar image joaoborks Mashimaro7 · Dec 17, 2020 at 02:58 PM 1
Share

It will only work on arrays that you explicitly set the attribute [LabeledArray]. You will then have to use a string array for the names or an Enum as provided in the examples:

 [LabeledArray(new string[] { "First", "Second", "Third" })]
 public int[] labeledValues;

 [LabeledArray(typeof(Order))]
 public int[] enumLabeledValues;
avatar image Mashimaro7 joaoborks · Dec 17, 2020 at 03:59 PM 1
Share

Ohh, I got it working. I overlooked the fact that it was a customized attribute lol. I actually was not aware you could make those :P Thanks!

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

11 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

Related Questions

Show variable name from a class as String 2 Answers

why do I need to type variables classes? 2 Answers

How do you make a custom inspector for a class or instance? 3 Answers

Access a class from Editor script? (Visual Studio) 1 Answer

File Name And Class Name Dont Match 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