- Home /
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();
}
}
}
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.
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.
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.
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.
"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)
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
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;
Your answer
Follow this Question
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