- Home /
How do you get the default inspector to draw for Prefabs ?
I am extending the inspector for prefabs so that I can include custom functionality. I want to draw the regular inspector gui, in addition to my own custom gui.
However, I cannot seem to get the default inspector for Prefabs (Project Window) to draw properly. Neither DrawDefaultInspector() nor base.OnInspectorGUI() seem to work in this case.
Question: In the case of wanting to extend an existing inspector (GameObject / Prefab), how do you get the original inspector to draw ?
Reference Code:
[CanEditMultipleObjects, CustomEditor (typeof(GameObject))]
public class PrefabEditor : Editor {
public override void OnInspectorGUI(){
//draw regular inspector gui
DrawDefaultInspector(); //THIS ISN'T WORKING AS EXPECTED
//Pseudo Code
bool isPrefab = CheckIfPrefab(target);
if(isPrefab == false)
return;
//Add custom gui....
}
}
Do you really want to have a custom editor for all GameObject
instances? If yes, what is your final goal?
What I want to do is customize the inspector for prefabs.
Because a prefab is a general game object, I have to do an additional check. The OnInspectorGUI logic would look like:
1.In the case that the game object is not a prefab, draw the default inspector and exit the gui function.
2.In the event that it is a prefab, draw the default inspector and additional gui for extended functionality.
1 isn't working because the default gui is not drawing as expected.
Answer by borro56 · Sep 08, 2015 at 10:09 PM
base.OnInspectorGUI() is what you`re looking
That's pretty pointless ^^. OnInspectorGUI inside the Editor class is defined like this:
public virtual void OnInspectorGUI()
{
this.DrawDefaultInspector();
}
So it just calls "DrawDefaultInspector" which actually has a bad name. It should be called "DrawDefault$$anonymous$$onoBehaviourInspector" or DrawSerializedFieldsInspector because all it does is draw property GUI for each serialized value that the object has, just like a $$anonymous$$onoBehaviour script is shown.
Answer by Bunny83 · Sep 09, 2015 at 12:03 AM
You actually can't extend an existing editor. Unity (or actually the InspectorWindow class) will pick one editor per object in the inspector. If you implement a custom inspector for a built-in component / object it will replace the original one.
There is a class inside the UnityEditor assembly that is called "GameObjectInspector" which implements the GUI you see at the very top of the inspector window when you select a gameobject. That class is marked as internal, so you can't derive your own class from it. You really should not replace any of the built-in editors. You might want to implement a custom inspector for a specialized script which you attach to the prefab.
If you want every prefab to have an instance of that script you can implement an AssetPostProcessor which adds your script to each prefab. Already existing prefabs need to be reimported in that case. The script could be empty. You just use it to add your custom GUI.
You might want to downlad ILSpy and take a look at the UnityEditor.dll. It helps to understand how the editor actually works.