Good day everyone. Just want to share some code if anyone in need for it. Recently i was working on extending import of some supported image formats so i came in need for a custom TextureImporterInspector. The problem was to show custom Inspector for some formats and usual for others. So here is the solution based on some reflection black magic:
[CustomEditor(typeof(TextureImporter))]
public class TextureImporterCustomEditor : Editor
{
private SerializedObject serializedTarget;
private Editor nativeEditor;
public void OnEnable()
{
serializedTarget = new SerializedObject(target);
SceneView.onSceneGUIDelegate = TargetUpdate;
Type t = null;
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (Type type in assembly.GetTypes())
{
if (type.Name.ToLower().Contains("textureimporterinspector"))
{
t = type;
break;
}
}
}
nativeEditor = Editor.CreateEditor(serializedObject.targetObject, t);
}
void TargetUpdate(SceneView sceneview)
{
Event e = Event.current;
}
public override void OnInspectorGUI()
{
if (nativeEditor != null)
{
bool isCustom = // check ((TextureImporter)serializedObject.targetObject).assetPath for file formats you need
if (isCustom)
{
// Do your stuff here
// you can use ((TextureImporter)serializedObject.targetObject).userData to store your stuff between sessions
}
else
nativeEditor.OnInspectorGUI();
}
// Unfortuanaly we cant hide ImportedObject section because InspectorWindow check it by
// if (editor is AssetImporterEditor) and all flags that this check sets are method local variables
// so aside from direct patching UnityEditor.dll no luck for reflection here :(
// in my case i just moved ImportedObject section out of view
GUILayout.Space(2048);
SceneView.RepaintAll();
}
protected override void OnHeaderGUI()
{
// We cant use nativeEditor.OnHeaderGUI because some internal variables not sets correctly by:
// Editor.CreateEditor so do some stuff to emulate native inspector header or customize it for your likes
}
}