Custom Editor: Open new inspector window and show asset

Is there a way to create a new inspector window and show asset in it?

I have found a way to create a new inspector window via reflection:

            var inspectorWindowType = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.InspectorWindow");
         
            MethodInfo method = typeof(EditorWindow).GetMethod(nameof(EditorWindow.CreateWindow), new[]{typeof(System.Type[])});
            MethodInfo generic = method.MakeGenericMethod(inspectorWindowType);

            generic.Invoke(null, new object[]{new System.Type[]{}});

However, I couldn’t find a way to show any asset in it.
Calling AssetDatabase.OpenAsset(assetToOpen); will just clear both the newly created and already existed inspector window to empty.

Is there any way this could be done? ?

Finally found a solution.

Instead of trying to use the built-in inspector, we can simply create a custom editor that draws the default editor of the given object. Here’s the code.

using UnityEditor;
using UnityEngine;

public class PopUpAssetInspector : EditorWindow
{
   private Object asset;
   private Editor assetEditor;
   
   public static PopUpAssetInspector Create(Object asset)
   {
      var window = CreateWindow<PopUpAssetInspector>($"{asset.name} | {asset.GetType().Name}");
      window.asset = asset;
      window.assetEditor = Editor.CreateEditor(asset);
      return window;
   }

   private void OnGUI()
   {
      GUI.enabled = false;
      asset = EditorGUILayout.ObjectField("Asset", asset, asset.GetType(), false);
      GUI.enabled = true;

      EditorGUILayout.BeginVertical(EditorStyles.helpBox);
      assetEditor.OnInspectorGUI();
      EditorGUILayout.EndVertical();
   }
}

To show the asset inspector in a new editor window, simply call

PopUpAssetInspector.Create(someAsset);
4 Likes