Search Unity

Bug SpriteAtlasExtensions are broken

Discussion in '2D' started by ThisIsNotMyName123, May 9, 2023.

  1. ThisIsNotMyName123

    ThisIsNotMyName123

    Joined:
    May 10, 2022
    Posts:
    51
    SpriteAtlasExtensions seem completely broken, do they work for anyone? I have tested this with Unity 2022.2.X and 2023.1.0. I am using atlas V2 (always enabled).

    Code (CSharp):
    1. // all SpriteAtlasExtensions are broken. here are some examples using SetIsVariant.
    2. // they should be fixed, and full functionality (including packable editing and fetching) should be moved to the asset importer.
    3. // this would also make it play nicely with the cache server.
    4. // however I'd be happy if even the existing functionality worked.
    5. var atlasPath = "Assets/Path/To/Atlas.spriteatlasv2";
    6.  
    7. // SpriteAtlasExtensions.SetIsVariant(SpriteAtlas):
    8. // nothing happens
    9. var atlas = AssetDatabase.LoadAssetAtPath<UnityEngine.U2D.SpriteAtlas>(atlasPath);
    10. atlas.SetIsVariant(false);
    11. EditorUtility.SetDirty(atlas);
    12.  
    13. // serialized properties:
    14. // the object in memory seems to be edited as if I load it from the asset database it does have the new value.
    15. // however, the new value is never persisted to the asset file.
    16. // so there is some really bad magic going on with sprite atlases, as you can't even write to them via serialized properties.
    17. using var serializedObject = new SerializedObject(atlas);
    18. using var isVariantProperty = serializedObject.FindProperty("m_IsVariant");
    19. isVariantProperty.boolValue = true;
    20. serializedObject.ApplyModifiedProperties();
    21. EditorUtility.SetDirty(serializedObject.targetObject);
    22.  
    23. // SpriteAtlasExtensions.SetIsVariant(SpriteAtlasAsset):
    24. // nothing happens
    25. var editorAtlas = UnityEditor.U2D.SpriteAtlasAsset.Load(atlasPath);
    26. editorAtlas.SetIsVariant(true);
    27. EditorUtility.SetDirty(editorAtlas);
    28.  
    29. // save
    30. AssetDatabase.SaveAssets();
    31. AssetDatabase.Refresh();
    32. AssetDatabase.ImportAsset(atlasPath);
     
    Last edited: May 9, 2023
  2. DanielTanBK

    DanielTanBK

    Unity Technologies

    Joined:
    Aug 20, 2019
    Posts:
    83
    Hello, the first 2 cases are as expected.
    In the first case, when using Sprite Atlas V2, SpriteAtlasAsset.Load() should be used instead of AssetDatabase.LoadAssetAtPath().
    In the second case, Sprite Atlas V2 is not supported as a SerializedObject
    The third case using SpriteAtlasAsset.Load() should work, however its recommended to use
    SpriteAtlasAsset.Save();
    AssetDatabase.Refresh();

    instead of
    EditorUtility.SetDirty();
    AssetDatabase.SaveAssets();
    AssetDatabase.Refresh();


    More info on the SpriteAtlasAsset APIs
     
    Last edited: May 24, 2023
    fleity likes this.
  3. KaiBornemann

    KaiBornemann

    Joined:
    Nov 14, 2023
    Posts:
    2
    I have the same feeling, all my old code for SpriteAtlas V1 does not work for V2 anymore. So then I switched to use SpriteAtlasAsset, but there I can'f find any method to get the current packables. But I can get them via the old SpriteAtlasExtensions code.

    Next, when I add/remove packables via SpriteAtlasAsset and call SpriteAtlasAsset.Save afterwards, nothing is changed on my asset on disc. It all feels very weird and inconsistent.

    Anyone know how to modify SpriteAtlas V2 properly via code?
     
  4. Venkify

    Venkify

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    644