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

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
Add comment · 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 $$anonymous$$m. 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 ins$$anonymous$$d of camelCase (sadly Unity didn't follow this recommendation with their own properties).

$$anonymous$$ay I ask how large your $$anonymous$$m 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
0

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity editor and inspector header 0 Answers

How to make a global settings Editor Window for my game 0 Answers

Large serializable field in a MonoBehaviour causing poor performance in Inspector 1 Answer

How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers

Questions Regarding Images in Custom Inspector/Editor 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