Cancel override inside override?

Hi, I’m doing an Editor script that overrides the Transform inspector.

Only I do not want to override every ones inspectors all the time. Only under certain conditions (when certain prefabs are in play).

So, I am doing

public override void OnInspectorGUI()
{
// Special Transform Inspector here
}

But I’d like it to be either some form of

public override void OnInspectorGUI()
{
if (condition == true)
{
// Special Transform Inspector here
} else
{
// Revert to Default Inspector, no override, even allow others override to happen
}
}

… or …

// Somewhere in a script I would not know where to place or make
if (condition == true)
{
public override void OnInspectorGUI()
{
// Special Transform Inspector here
}
}

Is this possible, do you have any awesome ideas?
The thought is that everyone and his brother makes special transform inspectors these days, and I’d like my project only to interfere when needed

Thanks!

Try doing the following

public override void OnInspectorGUI ()
{
     if(condition){
         //do something in here
     } else {
         base.OnInspectorGUI();
     }
}

Hi @Fritsl! Unfortunately @ashleyjlive’s suggestion won’t work because there is no way for you to inherit from the built-in (internal) TransformInspector class. We’re actually currently working on a solution to address this sort of issue, but in the meantime the best option you have is to use reflection to create an instance of the built-in TransformInspector and call its necessary methods when needed. (Edit: Got to a computer where I could test and updated the code example correspondingly)

using UnityEditor;
using UnityEngine;
using System.Reflection;

[CustomEditor(typeof(Transform)), CanEditMultipleObjects]
public class MyTransformInspector : Editor {

	private Editor m_BuiltinTransformEditor;

	protected virtual void OnEnable () {
		System.Type builtinType = null;
		foreach (System.Type t in typeof(Editor).Assembly.GetTypes ())
		{
			if (t.FullName == "UnityEditor.TransformInspector")
			{
				builtinType = t;
				break;
			}
		}
		m_BuiltinTransformEditor = Editor.CreateEditor (this.targets, builtinType);
		BindingFlags bindingFlags =
			BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod;
		m_BuiltinTransformEditor.GetType ().InvokeMember (
			"OnEnable", bindingFlags, null, m_BuiltinTransformEditor, null
		);
	}

	protected virtual void OnDisable () {
		DestroyImmediate (m_BuiltinTransformEditor);
	}

	public override void OnInspectorGUI () {
		// obviously test your actual condition
		if (condition) {
			// do your inspector here
		}
		else {
			m_BuiltinTransformEditor.OnInspectorGUI ();
		}
	}
}