Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
3
Question by P0lT10n · Jul 12, 2015 at 08:02 PM · inspectorheader

Can I change variable name on inspector ?

Hello everyone,

I was wondering if can we change the variable name on inspector without changing it in script.

For example:

 using UnityEngine;
 using System.Collections;
 
 public class Luces : MonoBehaviour {
     
     [Header("Luces")]
     public Light luzRoja;
     
     void Update() {
         
             // Some code...
         
     }
 
 }

Insted of saying "Luz Roja" on inspector, I want to say "Roja".

I have more code, but this is what I see now:

alt text

Thanks !

ANSWERED: Thanks everyone for the answers. I knew that maybe the best options was to create a custom inspector, but for this, is a lot of work and makes no sense... So, it will be Luz Roja :P

Comment
Add comment · Show 2
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 fafase · Jul 12, 2015 at 08:33 PM 1
Share

That does not seem possible at first sight.

The serialize property which is the link between your variable on script and the display in the inspector only provides getter for the name. So it seems modifying the name is not possible. I could be wrong though.

http://docs.unity3d.com/ScriptReference/SerializedProperty.html

avatar image _Gkxd · Jul 12, 2015 at 09:09 PM 1
Share

It's always possible to write your custom inspector, but there's most likely an easier way of doing it. If all else fails though, you can try something like this: https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector

(But at that point it's probably too much effort for what you want to do, so you might as well just deal with the default inspector.)

Here are some more resources if you do decide to go this path:
http://catlikecoding.com/unity/tutorials/editor/custom-data/
http://catlikecoding.com/unity/tutorials/editor/custom-list/

4 Replies

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

Answer by andrew-lukasik · Jul 24, 2017 at 02:40 PM

You can do this with PropertyAttribute:

 using UnityEngine;
 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 public class LabelOverride : PropertyAttribute
 {
     public string label;
     public LabelOverride ( string label )
     {
         this.label = label;
     }
 
     #if UNITY_EDITOR
     [CustomPropertyDrawer( typeof(LabelOverride) )]
     public class ThisPropertyDrawer : PropertyDrawer
     {
         public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
         {
             try
             {
                 var propertyAttribute = this.attribute as LabelOverride;
                 if( IsItBloodyArrayTho( property ) == false )
                 {
                     label.text = propertyAttribute.label;
                     
                 } else
                 {
                     Debug.LogWarningFormat(
                         "{0}(\"{1}\") doesn't support arrays ",
                         typeof(LabelOverride).Name ,
                         propertyAttribute.label
                     );
                 }
                 EditorGUI.PropertyField( position , property , label );
             } catch ( System.Exception ex ) { Debug.LogException( ex ); }
         }
         
         bool IsItBloodyArrayTho ( SerializedProperty property  )
         {
             string path =  property.propertyPath;
             int idot = path.IndexOf('.');
             if( idot==-1 ) return false;
             string propName = path.Substring( 0 , idot );
             SerializedProperty p = property.serializedObject.FindProperty( propName );
             return p.isArray;
             //CREDITS: https://answers.unity.com/questions/603882/serializedproperty-isnt-being-detected-as-an-array.html
         }
     }
     #endif
 }

So you can easily override inspector labels:

 [LabelOverride( "Awesome light" )]
 [SerializeField]
 Light _light;

To see what real name of this field is you must switch Inspector to Debug mode.

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 lodendsg · Nov 01, 2021 at 12:24 PM 0
Share

Just a quick note this wont work for Unity events but for most things its fine

avatar image
2

Answer by idbrii · Apr 26, 2021 at 11:26 PM

If you don't want to write your own PropertyDrawer, you can use NaughtyAttributes.Label:

 public class Luces : MonoBehaviour {
    [Header("Luces")]
    [Label("Roja")]
    public Light luzRoja;
    // ...

NaughtyAttributes is open source and available as a package.

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
1

Answer by xxxmrfunxxx · Jul 12, 2015 at 09:34 PM

If it could be done, it would have to be through serialization. The thing is that it is not a good idea to have a variable name for a script variable because, say that you want to access that variable through another script. Unity wouldn't know what to access once it finds a variable named 'Roja' because in the script it is called 'Luz Roja' so it won't make the connection.

I recommend changing it in the script if you don't want headaches.

Best regards and Good Luck!

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 DanVioletSagmiller · Oct 18, 2019 at 07:38 PM

1) I would recommend against changing the field names as they show up. The big reason (there are others), is think of anytime you have tried to GetComponent<...>() and couldn't find fields that matched the UI names. It makes it difficult to develop against in the future by you or others.

2) A common trick I do to help me organize my script fields, is inside of my MonoBehaviour, I create a small serialized class called (to mimic your system) public class LucesFields. Then I have fields for each one, such as public Light Roja; public Light Verde; etc. Then I create a public field call like this: public LucesFields Luces;

This gives you the header, but also makes the header collapsable. you can name stuff more simply, because if you have Roja field for a UI Slider and a Light, then the header separates it for simplicity. This also reduces junk in Intellisense. Instead of having to find LucesRoja in all of the members in a monobehaviour, you just type Luces (for the lights) then ".R" and intellisense will allready be simplified. I think this might be the better practice.

Don't get me wrong, I initially loved the helper attribute that @Hunter_Towe suggested above. But after consideration, the fact that it adds a "Magic" layer that hides how to find the variable in the code, I couldn't agree with it. Pretty, but the type of thinking that will help make code more complicated in the long run. (I mean no offense by that, I have had hundreds of instances like that, and I'm still catching myself doing things like these days)

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 lodendsg · Nov 01, 2021 at 12:14 PM 0
Share

Cant agree with your assesment here. Your creating an object not to improve management of data in memory or reduce code duplication but to adjust your dev time editor's display. So you are in effect making run time worce to improve your editor experience

Keep in $$anonymous$$d that creating that little class means that when you access the field Roja from your componenet you have the following steps

1. get the commonent pointer e.g. GetComponennet()
2. look up that object from that pointer
3. get the pointer to your Luces class
4. look up that object from that pointer
5. get the value of the Roja attribute of that object (a pointer to a light)
6. look up that light object


Compare that to storing it on the class directly

1. get the commonent pointer e.g. GetComponennet()
2. look up that object from that pointer
3. get the pointer for your light (luzRoja)
4. look up the light object


Increasing the depth that your code must traverse to get to the values desired does have a cost. Its a small cost yes but these sorts of things add up. If there is a good return on the investment of that e.g. if its worth it in making your code more maintainable or something similar then sure but doing it to make your dev time inspector more attractive? no

Either A) deal with the long name if you want to keep names identical
or
B) understand that the insecptor is a UI not a 1 to 1 name match with the code and that intelicense is a wonderful tool well able to locate luzRoja when you start typing ".Ro" it will show you all options wiht Ro in it sorting first yes thoughs that start with Ro but including thouse that simply contain it just below that

So I feel neither point 1 nore 2 is a good reason to avoid custom labels for fields on componenets

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

Unity editor and inspector header 0 Answers

HeaderAttribute [Header()] on ScriptableObject not working? 2 Answers

(Inspector) Change Header Color 0 Answers

Is it possible to create a custom model preview in an Editor window akin to the one displayed when an FBX is selected? 3 Answers

Giving a public Transform field a default value in the inspector 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