What's happening with AssetDatabase.AddObjectToAsset (cases #1262293 and #1262298)

So for a while now (probably since AssetDatabase v2) There’s been some weird things going on with sub assets, AddObjectToAsset, RemoveObjectFromAsset and ImportAsset.

in 2019.3 - 2020.1 (Case #1262298) Sub assets don’t appear in the project view until they are “pinged” and project is manually saved.

in 2020.2 (Case #1262293) Sub assets appear immediately, but a warning appears similar to this:

Importer(NativeFormatImporter) generated inconsistent result for asset when adding SubAsset

Documentation says: Unity - Scripting API: AssetDatabase.AddObjectToAsset

  1. Create the sub asset
  2. Add the sub asset
  3. Import main asset

This used to work flawlessly before AssetDatabase V2.

Here’s the code I used to submit the reports:

using UnityEditor;
using UnityEngine;

public static class AssetUtil {

    #region Public Methods

    public static T CreateSubAsset<T>(string path, SerializedProperty list) where T : ScriptableObject {

        //create new instance of T

        T asset = ScriptableObject.CreateInstance<T>();
        asset.name = $"Child {list.arraySize}";

        //undo support

        Undo.RegisterCreatedObjectUndo(asset, $"Create {typeof(T).Name} Asset");

        //add the asset as a sub asset to path

        AddObjectToAsset(asset, path, list);

        return asset;
    }

    public static void AddObjectToAsset(Object asset, string path, SerializedProperty list) {

        //add the object to path

        AssetDatabase.AddObjectToAsset(asset, path);

        //increase list and reference

        list.arraySize++;
        list.GetArrayElementAtIndex(list.arraySize - 1).objectReferenceValue = asset;
        list.serializedObject.ApplyModifiedProperties();

        //Does not refresh the MainAsset in 2019.3 - 2020.1
        //Warning in 2020.2 - Importer(NativeFormatImporter) generated inconsistent result for asset when adding SubAsset

        AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    }

    public static void RemoveObjectFromAsset(int index, string path, SerializedProperty list) {

        //get the asset on the list

        SerializedProperty property = list.GetArrayElementAtIndex(index);
        Object asset = property.objectReferenceValue;

        if (asset) {

            //remove asset, **this seems to also handle Undo?** Not mentioned in documentation, so a little skeptical :|

            AssetDatabase.RemoveObjectFromAsset(asset);

            //null the reference and remove from list

            property.objectReferenceValue = default;
            list.DeleteArrayElementAtIndex(index);
            list.serializedObject.ApplyModifiedProperties();

            //Does not refresh the MainAsset in 2019.3 - 2020.1
            //Warning in 2020.2 - Importer(NativeFormatImporter) generated inconsistent result for asset when adding SubAsset

            AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
        }
    }

    #endregion
}

Just wondering… Is there some new approach to adding sub assets? Or Unity are aware and fixing?
Does the above code look valid?

6 Likes

sigh. Such a simple thing.

must call

AssetDatabase.SaveAssets before calling AssetDatabase.ImportAsset

found this by looking at some Unity source:

https://github.com/Unity-Technologies/UnityCsReference/blob/master/Modules/TextCoreEditor/Managed/FontAssetCreationMenu.cs#L117
https://github.com/Unity-Technologies/UnityCsReference/blob/master/Modules/TextCoreEditor/Managed/SpriteAssetCreationMenu.cs#L192

Doesn’t seem consistent though. Like in AudioMixerController, that doesn’t call SaveAssets before ImportAsset and I don’t know why!

EDIT - AudioMixerController suffers from same issues as above!
Importer(NativeFormatImporter) generated inconsistent result for asset(guid:cbc928713154550449b94f265b9dc341) "Assets/NewAudioMixer.mixer"

3 cases for the price of 1: (Case #1262303)

4 Likes

I guess my last question is, why would you not SaveAssets before calling ImportAsset?

Maybe there was a time, long long ago, where this SaveAssets call was automatic?

Thanks for the reports! I’ve forwarded the thread to the team.

Issue Tracker links for the active issues:

1 Like

I’d like to know more about this. I am having something similar but when I rename the asset the hierarchy changes. (I have also tested the Import and still happens).

EDIT: Fixed in new version https://issuetracker.unity3d.com/issues/parent-and-child-nested-scriptable-object-assets-switch-places-when-parent-scriptable-object-asset-is-renamed?_ga=2.246476740.1729101191.1608557893-293289097.1608557893

3 Likes

Still happens when I duplicate…

1 Like

The fix landed in 2020.2.0b14 and 2021.1.0a8. If you’re still experiencing this issue in one of those or later versions, it would be much appreciated if you could submit a bug report for it with reproduction steps.

1 Like

Submitted a bug report. Also found out that it only happens on assets created before the fix, and that when they were duplicated after the fix (messing them up), it would never happen again, because this “new” asset was created after the fix.

1 Like

Thank you, what’s the issue ID?

I don’t know… I have still not received any confirmation mail for any of the bugs I have reported.

Edit: Got the email now. Case: 1302399.

1 Like

I am getting this too on some of my imported materials, they look ok and function as usual. I am only using 2020.2 to test my VRGallery with the new features and understand that it’s work in progress, my release version will be using 2019LTS so I am not that concerned. I can wait for a fix in 2020LTS.

This happened to me after updating to Unity 2020.2.5f1
(with AddressableAsset version 1.16.16)

I moved the assets in question to a different folder, switched to Unity, moved my assets back, and then this error didn’t happen anymore. They were all scene files.

The issue was identified and a fix recently landed in 21.2. Backport requests for 2020.2 and 21.1 are being evaluated.

I encountered this error with a simple texture material after upgrading from 2020.2.1f1 to 2020.2.5f1. Moving the .mat file out of the Unity directory, deleting the .meta file, and moving the file back eliminated the error. Simply renaming the file didn’t work.

Happens in our project 100%, when we save a scene with some terrain in it the terrain turns white, and these warnings about inconsistent assetdatabase appear.

Honestly Assetdatabase V2 has so many issues, ever since that was released we have experienced a lot more random importing issues than before.

4 Likes

This is still happening in 2020.2.6f1 whenever a scriptableobject is saved or reimported.
This also happens the first time I click play and it points to my sprite atlas.

Does anyone else recognise this issue; where after you update from source control, ScriptableObjects just dont reflect what is on the disc at all?

https://dl.dropboxusercontent.com/s/xktxiut4iy3zokc/wpzWsuULBo.mp4

Reimporting doesnt help, we usually have to restart the editor. It happens intermittently.

YES! I’ve noticed it. Sometimes when Unity doesn’t import things correctly, it won’t load changes made outside the editor. I’ve prevented a lot of it by saving before/instead of importing, like CDF said, but sometimes it still happens. Sometimes I can fix it without restarting the editor: force the asset to be imported correctly by making it dirty (just modify something and Undo), save so it get’s imported correctly, and then update from version control again.

Also, has anyone else noticed a lot of the Undo functionality doesn’t work correctly with assets? We didn’t see these issues before 2020.2. I believe they have something to do with the new asset importing behavior. They are very ugly.

For example, if you use Undo.RecordObject or Undo.RegisterCompleteObjectUndo and modify an asset without a SerializedObject, the asset won’t be marked dirty. If you save and look at the asset in a text editor you won’t see the change. And if you close and open Unity, the change won’t be there. Ctrl+z/Ctrl+y will still work, though, but it doesn’t mean it’s not a problem.

It’s a lot more ugly when handling the life cycle of subassets though:

  • Destroy a subasset with Undo.DestroyObjectImmediate or by doing Ctrl+z after adding a subasset with Undo.RegisterCreatedObjectUndo. Don’t save.
  • Either Unfocus Unity (i.e. click on another application) or press play.
  • Come back to Unity and save. Notice that the subasset is still in the project view, and it’s in the YAML too. If you select your subasset and the inspector shows nothing, go to another folder and come back; now when you select your subasset you’ll see it’s really there (bonus trouble: it has a different instanceID).

Furthermore, when the asset is between step 2 and 3, before saving, it also won’t import changes from version control. Finally I’ve managed to get the Importer(NativeFormatImporter) generated inconsistent result warning a couple of times while doing Undos and Redos, but I can’t make it happen consistently.

If anyone has any insight or experience with these issues, I’d really appreciate if you share. I’ll submit a bug report later, but I want the examples to be as clear as possible; I’ve noticed it can really help to get things rolling faster. Also, tbh, if anyone has already submited a report, maybe I could avoid doing that work :smile:.

These issues can get us in a lot of problems that aren’t noticeable until much later, when it’s very hard to know the cause. It’s specially awful when hiding subassets from the projectview with hideFlags.

EDIT
Sorry, never mind. I just tested it and the Undo issues also happen in the latest 2019.4 version.

1 Like

can someone fix this for 2020 versions not only 2021?

2 Likes

Still have this bug in 2020.3.2 LTS :frowning:

7 Likes