Case (1249167) Why might Unity destroy an Editor after AssetDatabase.ImportAsset?

This seems to be a problem I’ve only encountered in 2020.1.
Where sometimes unity will destroy a Custom Editor after calling AssetDatabase.ImportAsset. e.g:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test), true)]
public class TestEditor : Editor {

    private SerializedProperty propertyName;

    private void OnEnable() {

        propertyName = serializedObject.FindProperty("m_Name");
    }

    public override void OnInspectorGUI() {

        serializedObject.Update();

        NameField(propertyName);
   
        serializedObject.ApplyModifiedProperties();
    }

    private void NameField(SerializedProperty property) {

        EditorGUI.BeginDisabledGroup(serializedObject.isEditingMultipleObjects);

        EditorGUI.BeginChangeCheck();

        EditorGUILayout.DelayedTextField(property);

        if (EditorGUI.EndChangeCheck()) {

            RenameAsset(target, property.stringValue);
        }

        EditorGUI.EndDisabledGroup();
    }

    private void RenameAsset(Object asset, string name) {

        if (asset) {

            asset.name = name;

            EditorUtility.SetDirty(asset);
            AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(asset));

            //exit gui after rename, sometimes Unity will destroy the Editor upon re-importing the asset
            //GUIUtility.ExitGUI();
        }
    }
}

*NOTE this Editor is used on a SubAsset

If I don’t call GUIUtility.ExitGUI after the import, the Editor OnDestroy gets called and Unity throws an error saying “serializedObject has been disposed”.

Strange thing is, in an Empty project I cannot reproduce this. The Editor doesn’t get destroyed.

So I’m wondering, maybe there’s some other code in my main project that is causing Editors to be destroyed after import? Or, is Unity using some editor caching system that when there are too many Editors Unity starts destroying them? (I have a lot of CustomEditors in the main project). If it is some custom code causing this, where might I start looking into a cause?

Annoying that the behavior is not consistent. Would be fine if it always destroyed Editors after ImportAsset.
Any ideas?

There’s also the issue of the project view not actually refreshing the name change. You have to collapse and expand the main asset for the name to appear changed in project view. Another bug which I’ll report some day…

Oh… this is interesting.
A tasty new bug for the TilemapEditor team.

Stack trace after calling “AssetDatabase.ImportAsset” then listening to “OnDestroy” on the Editor:

This issue occurs when Tile Palette Window is visible (or docked)
Issue does not occur when Tile Palette window is removed.

not sure if this is a TileEditor package issue, or Prefab issue.
Anyway. Bug report coming up

Bug report submitted: Case (1249167)

Thanks a lot for the report! It looks like it’s been fixed in 2020.2.0a4. The backport request for 2020.1 is still pending.

1 Like