Hello!
I would like to share a smart solution for extending custom inspectors instead of overriding and replacing the built-ins. How would you, for instance, do something like this?
When write your own inspector for a built-in class, you have to make sure to replicate its same inspector (kind of what Unify wiki does). That, however, is not always an easy solution.
That inspector uses the built-in editor to show the local space coordinates and then expands that functionality to show world coordinates and some extra stuff.
And here’s a snippet of the example above
[CustomEditor(typeof(Transform), true)]
[CanEditMultipleObjects]
public class CustomTransformInspector : Editor {
//Unity's built-in editor
Editor defaultEditor;
Transform transform;
void OnEnable(){
//When this inspector is created, also create the built-in inspector
defaultEditor = Editor.CreateEditor(targets, Type.GetType("UnityEditor.TransformInspector, UnityEditor"));
transform = target as Transform;
}
void OnDisable(){
//When OnDisable is called, the default editor we created should be destroyed to avoid memory leakage.
//Also, make sure to call any required methods like OnDisable
MethodInfo disableMethod = defaultEditor.GetType().GetMethod("OnDisable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
if (disableMethod != null)
disableMethod.Invoke(defaultEditor,null);
DestroyImmediate(defaultEditor);
}
public override void OnInspectorGUI(){
EditorGUILayout.LabelField("Local Space", EditorStyles.boldLabel);
defaultEditor.OnInspectorGUI();
//Show World Space Transform
EditorGUILayout.Space();
EditorGUILayout.LabelField("World Space", EditorStyles.boldLabel);
GUI.enabled = false;
Vector3 localPosition = transform.localPosition;
transform.localPosition = transform.position;
Quaternion localRotation = transform.localRotation;
transform.localRotation = transform.rotation;
Vector3 localScale = transform.localScale;
transform.localScale = transform.lossyScale;
defaultEditor.OnInspectorGUI();
transform.localPosition = localPosition;
transform.localRotation = localRotation;
transform.localScale = localScale;
GUI.enabled = true;
}
}
Note: I am still to figure out a way to show and be able to modify world position (I mean to do it properly. Being able to multi-inspect, Undo and redo and general stuff), that’s why I made it read-only.
I found this to be an easy and clever solution while making my Component Tool Panel editor extension. And it can also be used to expand on GameObject inspector:
I really hope this helps someone =)