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
1
Question by MaxPower42 · Oct 04, 2015 at 06:00 PM · editorinspectornamespaceproperties

Custom names for variables (array-elements!!) in the inspector??

I've been looking and googling for a way to do that for hours now... is there really no attribute or something to do this simple thing - provide a string to be used in place of the variable name (or Element 0, 1...)?

Do I really need to write some custom attribute and drawer-thingy for 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 meat5000 ♦ · Oct 04, 2015 at 06:08 PM 1
Share

Run it through an enum somehow.

I do think youll need to write some editor code...

avatar image MaxPower42 meat5000 ♦ · Oct 04, 2015 at 06:20 PM 0
Share

Why an enum? Will the enum-values be displayed as text? The problem is, I need something that isn't constant. Basically, I have an array/list of structures with a string as description/name and a float (with slider) as value. The rest is private data. I only want the description-string to be shown per class as the name (left) for the float-slider (right).

avatar image meat5000 ♦ MaxPower42 · Oct 04, 2015 at 06:26 PM 1
Share

Yeah an enum is just a simple way of giving strings an index. I cant say Ive ever used them though. You may be facing the need to create a custom inspector, as you fear.

There are some hits on google.

http://answers.unity3d.com/questions/145262/na$$anonymous$$g-array-elements-in-editor.html

https://www.google.co.uk/search?q=unity+inspector+array+names+ins$$anonymous$$d+of+numbers&ie=utf-8&oe=utf-8&gws_rd=cr&ei=224RVpHLIIH4UILXgogE

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Oct 04, 2015 at 06:42 PM

Forget drawers and attributes - you just need to write a custom inspector class for your script - it only takes a couple of lines, and you get a lot of flexibility as a result:

https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

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 MaxPower42 · Oct 04, 2015 at 09:38 PM 0
Share

Thanks. Are you sure it's just a few lines?...

I figured out how to create a custom label text and a slider that represents a property for a simple class.

What I have yet to figure out is

1) how to actually CHANGE the associated property from the inspector.

2) how to deal with nested classes or arrays/lists of their instances.

  • when I use the editor/inspector-override on the "base"-class (composition-wise), I don't know how to access properties of nested classes, especially when there is a whole list of them

  • when I try to create an override for the nested classes themselves, the override never gets called at all

avatar image tanoshimi MaxPower42 · Oct 04, 2015 at 09:43 PM 1
Share

To change the associated property, you call serializedObject.Apply$$anonymous$$odifiedProperties().

For styling "nested" classes/arrays in a custom inspector, you can use GetArrayElementAtIndex(i) and FindPropertyRelative.

Here's another more detailed tutorial that covers these and other issues: http://catlikecoding.com/unity/tutorials/editor/custom-list/

avatar image
1

Answer by idbrii · Feb 05, 2018 at 08:10 PM

If the first variable in the class is a string, then Unity will use that variable as the array item label.

I've found other sources that say the variable needs to be named title, name, or key, but it doesn't seem to matter anymore:

     // Here, Funky is used as the array item label. Title and ToString are ignored.
     [Serializable]
     public class Pairs {
         public string Funky;
         public string Title;
         public bool IsMeshVisible;

         public string ToString()
         {
             return string.Format("{0} {1}", Funky, IsMeshVisible);
         }
     }


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 kyle-misner-kriel · Jun 06, 2018 at 07:28 PM 0
Share

I totally thought it had to be called "name" and modified your test to confirm it. Thanks!

avatar image harlekintiger · Jul 09, 2019 at 09:01 PM 0
Share

Please tell us what to do if the first is not a string. I Just want all elements named after the class/struct name it represents. (In your example "Pairs")

avatar image
0

Answer by joaoborks · Mar 14, 2020 at 05:32 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, true);
     }
 
     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 · 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

33 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

Related Questions

If I have a bool method, how can I see in the inspector whether it returns true or false? 2 Answers

Custom Inspector. Aligning properties? 1 Answer

Showing properties from a base class in inspector? 0 Answers

Hide/Show properties dynamically in inspector. 6 Answers

What should be handled in the custom inspector version of a script? 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