Search Unity

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

Discussion in 'Asset Database' started by CDF, Jul 10, 2020.

  1. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    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: https://docs.unity3d.com/ScriptReference/AssetDatabase.AddObjectToAsset.html
    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:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public static class AssetUtil {
    5.  
    6.     #region Public Methods
    7.  
    8.     public static T CreateSubAsset<T>(string path, SerializedProperty list) where T : ScriptableObject {
    9.  
    10.         //create new instance of T
    11.  
    12.         T asset = ScriptableObject.CreateInstance<T>();
    13.         asset.name = $"Child {list.arraySize}";
    14.  
    15.         //undo support
    16.  
    17.         Undo.RegisterCreatedObjectUndo(asset, $"Create {typeof(T).Name} Asset");
    18.  
    19.         //add the asset as a sub asset to path
    20.  
    21.         AddObjectToAsset(asset, path, list);
    22.  
    23.         return asset;
    24.     }
    25.  
    26.     public static void AddObjectToAsset(Object asset, string path, SerializedProperty list) {
    27.  
    28.         //add the object to path
    29.  
    30.         AssetDatabase.AddObjectToAsset(asset, path);
    31.  
    32.         //increase list and reference
    33.  
    34.         list.arraySize++;
    35.         list.GetArrayElementAtIndex(list.arraySize - 1).objectReferenceValue = asset;
    36.         list.serializedObject.ApplyModifiedProperties();
    37.  
    38.         //Does not refresh the MainAsset in 2019.3 - 2020.1
    39.         //Warning in 2020.2 - Importer(NativeFormatImporter) generated inconsistent result for asset when adding SubAsset
    40.  
    41.         AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    42.     }
    43.  
    44.     public static void RemoveObjectFromAsset(int index, string path, SerializedProperty list) {
    45.  
    46.         //get the asset on the list
    47.  
    48.         SerializedProperty property = list.GetArrayElementAtIndex(index);
    49.         Object asset = property.objectReferenceValue;
    50.  
    51.         if (asset) {
    52.  
    53.             //remove asset, **this seems to also handle Undo?** Not mentioned in documentation, so a little skeptical :|
    54.  
    55.             AssetDatabase.RemoveObjectFromAsset(asset);
    56.  
    57.             //null the reference and remove from list
    58.  
    59.             property.objectReferenceValue = default;
    60.             list.DeleteArrayElementAtIndex(index);
    61.             list.serializedObject.ApplyModifiedProperties();
    62.  
    63.             //Does not refresh the MainAsset in 2019.3 - 2020.1
    64.             //Warning in 2020.2 - Importer(NativeFormatImporter) generated inconsistent result for asset when adding SubAsset
    65.  
    66.             AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
    67.         }
    68.     }
    69.  
    70.     #endregion
    71. }
    72.  
    Just wondering... Is there some new approach to adding sub assets? Or Unity are aware and fixing?
    Does the above code look valid?
     
    Last edited: Jul 10, 2020
    ModLunar, xxaarr3, Malbers and 3 others like this.
  2. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    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-Technologi...o/Mixer/Bindings/AudioMixerController.cs#L443
    https://github.com/Unity-Technologi...eEditor/Managed/FontAssetCreationMenu.cs#L117
    https://github.com/Unity-Technologi...ditor/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)
     
    Last edited: Jul 10, 2020
  3. CDF

    CDF

    Joined:
    Sep 14, 2013
    Posts:
    1,311
    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?
     
  4. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    ModLunar likes this.
  5. BusStopStudios

    BusStopStudios

    Joined:
    Mar 28, 2015
    Posts:
    22
    Last edited: Dec 23, 2020
    ModLunar, shieldgenerator7 and Nexer8 like this.
  6. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    shieldgenerator7 likes this.
  7. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    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.
     
    ModLunar likes this.
  8. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    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.
     
    shieldgenerator7 likes this.
  9. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    Thank you, what's the issue ID?
     
  10. Nexer8

    Nexer8

    Joined:
    Dec 10, 2017
    Posts:
    271
    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.
     
    Last edited: Dec 31, 2020
    LeonhardP likes this.
  11. farrellart

    farrellart

    Joined:
    Dec 14, 2019
    Posts:
    16
    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.
     
  12. shieldgenerator7

    shieldgenerator7

    Joined:
    Dec 20, 2015
    Posts:
    39
    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.
     
  13. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
  14. Supergeek

    Supergeek

    Joined:
    Aug 13, 2010
    Posts:
    103
    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.
     
  15. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    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.
     
    TRWolf, jolix, GXMark and 1 other person like this.
  16. jolix

    jolix

    Joined:
    May 22, 2016
    Posts:
    70
    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.
     
  17. TJHeuvel-net

    TJHeuvel-net

    Joined:
    Jul 31, 2012
    Posts:
    838
    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.
     
  18. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    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:
    1. Destroy a subasset with
      Undo.DestroyObjectImmediate
      or by doing Ctrl+z after adding a subasset with
      Undo.RegisterCreatedObjectUndo
      . Don't save.
    2. Either Unfocus Unity (i.e. click on another application) or press play.
    3. 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 :D.

    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.
     
    Last edited: Mar 13, 2021
    TJHeuvel-net likes this.
  19. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    can someone fix this for 2020 versions not only 2021?
     
    jolix and wlwl2 like this.
  20. SammmZ

    SammmZ

    Joined:
    Aug 13, 2014
    Posts:
    174
    Still have this bug in 2020.3.2 LTS :(
     
  21. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    I got a ton of these warnings on a bunch of assets during a build. First time seeing it. Unity 2020.3.7
    I don't even understand what the warning is trying to tell me when it says "generated inconsistent result". What result? Inconsistent with what?
     
    Deleted User likes this.
  22. Nickjh82

    Nickjh82

    Joined:
    Mar 30, 2020
    Posts:
    11
    Getting this also.

    Code (CSharp):
    1. Importer(NativeFormatImporter) generated inconsistent result for asset(guid:76c03a8e8d5be912ca02c2415250c1b0)
    Using Version 2021.1.11f1.
     
  23. SevenPointRed

    SevenPointRed

    Joined:
    Feb 3, 2016
    Posts:
    218
    Exists in 2021.2.0a19
     
  24. Gillissie

    Gillissie

    Joined:
    May 16, 2011
    Posts:
    305
    I feel like I should mention that this happens on Sprite Atlas Assets, which are completely generated based on folders of sprites. I have no idea how Unity would get different import results each time I reimport them. I also get it on the folders that the atlas assets are in. Folders.
     
    Last edited: Oct 12, 2021
  25. MaxRoetzler

    MaxRoetzler

    Joined:
    Jan 3, 2010
    Posts:
    136
    Getting this exact issue when saving custom ScriptableObjects in the project, Unity 2020.3.30 LTS
     
  26. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    Hi, in this time I've learned that it can happen after calling ImportAsset without saving the asset first. You could call SaveAssets or SaveAssetIfDirty before importing. The docs are a lacking on this, but there is a vague explanation of why this is needed in the issue's resolution note. I've made a good amount of experiments on this, and I believe that the call to ImportAsset is not even needed anymore when doing these kind of things with scriptable objects; you just need to save the asset.

    That could help you avoid the problem appearing in the first place... To get rid of the problem when it has already appeared, you can delete the library folder, but it will cause your whole project to be reimported.
     
  27. MaxRoetzler

    MaxRoetzler

    Joined:
    Jan 3, 2010
    Posts:
    136
    Thanks for the feedback @oscarAbraham.

    I already tried calling SaveAssets and SaveAssetIfDirty with and without ImportAssets instead, and while the error disappeared at first, it came right back when creating the next set of assets in the project. But maybe I have to try and delete the library folder, see if it makes a difference. Did you have any luck eliminating the problem entirely?
     
  28. oscarAbraham

    oscarAbraham

    Joined:
    Jan 7, 2013
    Posts:
    431
    No problem. Sorry, I've been away.

    Hum, well, I haven't seen it anymore. I remember that an SO asset will keep triggering the problem, even if these fixes are implemented, if it has the same id and serialized data as an asset that triggered the problem previously. It's like its imported representation was cached with errors, so deleting the library folder fixes it.

    Other than that. I'm not sure. I have some ideas on possible insights, but they'd probably be too vague, or too many. If you show some code, maybe I could make other suggestions.