• 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 Mortalyx · Oct 02, 2016 at 05:40 AM · editorinspectormonobehaviourcustomglobal

Can i extend or customise editor for all MonoBehaviours?

The actual issue is to add own convention for inspector view. I mean, if we use the property with prefix "m_", then it will be reduced in PrefixLable, but it doesn't work for others. I need to replace "m_" by "p_", or extend. I tried this kind of code, but it seems not work

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MonoBehaviour))]
 public class MonoBehaviourEditor : Editor {
 
     MonoBehaviour _target;
     System.Reflection.MemberInfo[] _properties;
 
     void OnEnable() {
         _properties = _target.GetType().GetMembers();
     }
 
 
     public override void OnInspectorGUI() {
 
         foreach (var property in _properties) {
 
             EditorGUILayout.BeginHorizontal();
 
             EditorGUILayout.PrefixLabel(property.Name);
             //Here is should be something like EditorGUILayout.Field(someParams)
 
 
             EditorGUILayout.EndHorizontal();
         }
     }
 
 }

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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Oct 04, 2016 at 12:01 PM

Well, you can try using

 [CustomEditor(typeof(MonoBehaviour), true)]

which will enable inheritance of the editor for all derived classes. Classes / components which have already a specialized editor should not be affected.

If this doesn't work, there's no way to do that.

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 Adam-Mechtley · Oct 04, 2016 at 12:06 PM 0
Share

This is possible. What you would need to do then is something like this.serializedObject.GetIterator() and move through all of the non-hidden serialized fields and display them with your patched labels.

avatar image Mortalyx · Oct 04, 2016 at 03:07 PM 0
Share

Wow. That is useful. Thanks for you replies!

avatar image

Answer by Adam-Mechtley · Oct 03, 2016 at 07:04 AM

Unfortunately the automatic "pretty names" for inspector labels only works for fields prefixed with _ or m_.

While what you are wanting to do is certainly possible, it seems like it would be a lot of trouble. Is there a reason your backing fields are not all just prefixed with "m_"? From your code example, you are iterating all members, but only fields (of certain types) can be serialized.

Comment

People who like this

0 Show 3 · 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 Mortalyx · Oct 04, 2016 at 11:29 AM 0
Share

If, Adam, thanks for you reply. It's a new code style in my team. We decide to mark public fields with prefix "p_". Yea, my sample code is like pseudocode for explanation.

avatar image Bunny83 Mortalyx · Oct 04, 2016 at 12:07 PM 0
Share

"p_" for public fields? That's the most strangest thing i've ever heard ^^, If i would see a field prefixed with p_ I would naturally assume it's a protected or private field or maybe a property but never would assume just "public". Why would you prefix public fields? When someone uses the class, public fields are the only fields that are visible...

You can differentiate them from properties since properties should use PascalCase instead of camelCase (sadly Unity didn't follow this recommendation with their own properties).

May I ask how large your team is? (Only the number of programmers)

avatar image Mortalyx Bunny83 · Oct 04, 2016 at 02:55 PM 0
Share

Yea, sure. 4 programmers for now.

We already change this decision with prefix "p_". But i can explain previous situation: Properties are PascaleCased, but we thought about difference between field and chose this: public field -> p_name private field -> m_name static private -> s_name static public -> s_Name

Now we using "m_Name" for public fields

avatar image

Answer by Mortalyx · Oct 04, 2016 at 02:24 PM

I have found the hint to solve issue, but it seems not quite reasonable in use. Anyway, it's possible to use FormerlySerializedAs attribute like this:

 [FormerlySerializedAs("MyVariableName")]
 public string p_MyVariableName;
Comment

People who like this

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I create a list of preset class/objects to be used in the editor 1 Answer

Can a GameObject's full inspector be drawn in a custom Editor Window ? 1 Answer

Call inspector on custom window editor 4 Answers

Custom Inspector. Aligning properties? 1 Answer

How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 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