Is it possible to write a custom editor for DefaultAsset?

I have a custom file “MyFile.myExt” in the project and would like to write a custom editor for this special file. Unity imports the file as a DefaultAsset, so I’ve tried to override the existing editor for DefaultAsset. I believe this should work or at least has worked in the past, so I wanted to confirm if this should be possible or does not work anymore:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(DefaultAsset), editorForChildClasses: true, isFallback = false)]
public class MyCustomEditor : Editor
{
    public override void OnInspectorGUI()
    {
        Debug.Log("This is never called.");

        string path = AssetDatabase.GetAssetPath(target);

        if (path.EndsWith(".myExt"))
        {
            EditorGUILayout.LabelField("My Custom File Type");
        }
        else
        {
            base.OnInspectorGUI();
        }
    }
}

MyCustomEditor is simply never created and no callbacks are received. If I change the type to e.g. Transform, I can replace all Transform component editors. However, it does not seem to work for assets in the project, e.g. SceneAsset also does not work. I still believe that I’ve done exactly that in previous Unity versions, but I’m not entirely sure.

You probably want a Scripted Importer.

Ah sorry I forgot to add that I do not need a ScriptImporter, since there is nothing to import. I only want the editor to display read-only information. Additonally, the inspector should work for files placed within the StreamedAssets folder. So, in fact my custom file asset is never imported, it just stays a text file, which I want to preview in the inspector window.

Looks like this should be possible the way I tried but I may be affected by this issue: Unity Issue Tracker - Overridden OnInspectorGUI method is not called when using [CustomEditor(typeof(DefaultAsset))]

You can get around this for the affected versions by overriding the OnHeaderGUI method, calling base.OnHeaderGUI first and then implementing your inspector there.

2 Likes

I’ve tested this and it works fine as a workaround. :slight_smile: