I have added an Editor extension for GameObject, like this:
[CustomEditor(typeof(GameObject))]
public class ScaleExtensions : Editor {
// …
}
Now, when viewing game objects in the inspector, the nice GUI items for name, tag, layer, and so on, have been replaced by some simple text fields. Is there some function I should call to draw the inspector properly?
The simple answer is you shouldn’t replace the inspector of builtin classes. They use an internal inspector class which is dedicated for this class. You can’t access this inspector and it’s also not possible to keep the original and “add” something to it since this would require you to integrate your stuff in the inspector.
DrawDefaultInspector only shows all serialized properties as text-value pairs. It’s the default inspector for MonoBehaviour classes. All build in types have their own specialized inspector.
Why do you want to add something to the GameObject inspector? You might want to use the reverse engineered TransformInspector on the wiki. It will replace the default transform inspector.
Keep in mind that a GameObject has actually almost no functionality. Since your extension is called “ScaleExtensions” i guess it should actually target the Transform component and not the GameObject.
I did not come up with a reverse engineered GameObject inspector, however I found an interesting solution of my own to this problem. I created a script that runs in the background and is not CustomEditor dependent. This means it will not interrupt any native or custom editors in the inspector. We manually attach to the sceneGUI delegate and do our drawing there. Here is what I have:
[InitializeOnLoad]
[ExecuteInEditMode]
public class CustomSceneDrawer
{
static CustomSceneDrawer()
{
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
static void OnSceneGUI(SceneView sceneView)
{
if(Selection.activeGameObject == null)
{
return;
}
// Do scene drawing stuff here for Selection.activeGameObject... Handles.DrawWireDisc, ect....
// If you need to get any components from the gameobject, I would recommend finding a way to cache the components for the current selection somewhere instead of slowing down OnSceneGUI with GetComponent calls each draw.
}
}
The only thing I can’t figure out so far is where to detach my event handler for onSceneGUIDelegate. Any ideas on a clean place to remove the delegate?