• 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
Question by Virayr · Jan 14, 2015 at 01:09 PM · editorinspectoreditor-scriptingeditorguipropertydrawer

How to show type text in EditorGUI.ObjectField?

I am writhing a custom inspector and I have an ObjectField (something like this):

 [CustomEditor(typeof(Test))]
 public class InteractiveObjectEditor : Editor {
     public override void OnInspectorGUI () {
         (target as Test).collider = (Collider2D) EditorGUILayout.ObjectField("Some Collider", (target as Test).collider, typeof(Collider2D), true);
     }
 }

It appears in inspector like this:
Custom inspector screenshot

But I want it to appear like in default inspector (with type written in parentheses): Default inspector screenshot

Is there a way to do it with custom inspector code?

default.png (14.3 kB)
custom.png (8.4 kB)
Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Immanuel-Scholz · Jan 14, 2015 at 01:43 PM

Not without rewriting the whole ObjectField logic by yourself or unless you are up to serious DLL decompile/compile hacking.

The text that is displayed is hardcoded in EditorGUIUtility.ObjectContent and can not be modified by user code.

However, rewriting EditorGUI.ObjectField yield in some more benefits, e.g. you can replace the object picker dialog with something better. (That's what I did for our project here, to support additional Filter-things like "the selected object must also have component X with property Y set to Z...)

And it might turn out easier than you think. Look into EditorStyles.objectField and EditorStyles.objectFieldThumb for some starters.. ;)


Edit: Actually... if I think about it. You could just add the " (xxx)" to the gameObject's name before calling ObjectField. Kinda hackish but could work :D

 public override void OnInspectorGUI () {
     var col = (target as Test).collider;
     string name = col != null ? col.name : null;
     if (col != null)
         col.name += " (" + col.GetType().Name + ")";
     (target as Test).collider = (Collider2D) EditorGUILayout.ObjectField("Some Collider", col, typeof(Collider2D), true);
     if (col != null)
         col.name = name;
 }

Comment

People who like this

0 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 Virayr · Jan 14, 2015 at 03:59 PM 0
Share

Changing the name won't be a solution for me, but thank you anyway. If you can give me some other hints about rewriting EditorGUI.ObjectField that will be much appreciated :)

avatar image Immanuel-Scholz · Jan 15, 2015 at 02:45 PM 0
Share

Yea another hint: Download JetBrain DotPeek, drag UnityEditor.dll from your Unity installation folder, Press F3 and enter "EditorGUI.ObjectField". Then copy'n'paste the stuff to your project folder and start tweaking. ;)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GetPropertyHeight infinite recursion on drawer 1 Answer

CustomPropertyDrawer with UnityEvent 1 Answer

Custom Inspector Color Spacer 1 Answer

How to properly handle Undo events in custom inspector? 0 Answers

How to get width at which Inspector does line breaks (for Vectors) 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