OnPostprocessAllAssets called in between StartAssetEditing & StopAssetEditing?

From Unity Editor 2018.4.28f1

I’m analyzing a crash a colleague had while using a custom Editor Window tool we’ve made. The stacktrace is something like this

MyCustomAssetProcessor:OnPostprocessAllAssets
...
MonoPostprocessAllAssets
Postprocess
AssetDatabaseV1::WriteSerializedAssets
AssetDatabase::WriteRevertAssets
AssetDatabase::SaveAssets
AssetDatabase::CopyAsset
AssetDatabase_CUSTOM_CopyAsset
UnityEditor.AssetDatabase:CopyAsset
CustomToolMain:CreateNewPrefab

Now, the thing that puzzles me is that we’re calling CopyAsset in between a Start & StopAssetEditing. The abridged version of the code we’re running is this

try
{
    AssetDatabase.StartAssetEditing();
  
    previewScene = EditorSceneManager.NewPreviewScene();
    GameObject object = new GameObject("MyObject");
    SceneManager.MoveGameObjectToScene(baseCard, previewScene.Value);

    // ... do some stuff

    PrefabUtility.SaveAsPrefabAsset(object, prefabPath, out success);
    AssetDatabase.CopyAsset("MyCustomScriptableObj", prefabPath + ".asset");
}
finally
{
    if (previewScene != null)
    {
        EditorSceneManager.ClosePreviewScene(previewScene.Value);
    }
    AssetDatabase.StopAssetEditing();
    AssetDatabase.Refresh();
}

I wasn’t expecting OnPostprocessAllAssets to be called. I don’t think our handler is prepared to work correctly, given that the assets are in some indeterminate state. The documentation says “This is called after importing of any number of assets is complete”, but this stack trace makes it seem that the docs are mistaken.

Should I be adding special handling to OnPostprocessAllAssets for this situation? Is this just a Unity bug that I need to write a workaround for?

1 Like

Hello!

Update here:

It appears that calling CopyAsset in between StartAssetEditing and StopAssetEditing caused an AssetDatabase refresh, and so probably Unity was tripping over itself in some weird way.

We replaced the CopyAsset call with a File.Copy (which will be imported when the asset system detects it) and we haven’t had any crashes since then (4 months running).

So…don’t call CopyAsset in between StartAssetEditing and StopAssetEditing!

1 Like