elringus / sprite-dicing

Cross-engine tool for lossless compression of sprites with identical areas
https://dicing.elringus.me
MIT License
1.35k stars 144 forks source link

Nullref when serializing asset on Unity 2022 #39

Closed laicasaane closed 7 months ago

laicasaane commented 11 months ago

Unity 2022.3.14 SpriteDicing 1.10.0

I'm receiving this exception when trying tpo build the atlas.

Failed to build diced sprite atlas. System.ArgumentNullException: Value cannot be null.
Parameter name: _unity_self
  at (wrapper managed-to-native) UnityEditor.SerializedObject.ApplyModifiedProperties(UnityEditor.SerializedObject)
  at SpriteDicing.Editors.AtlasBuilder.<PackTextures>g__SaveAtlasTextures|8_2 (System.Collections.Generic.IReadOnlyList`1[T] textures) [0x0003d] in D:\Projects\SpriteDicing\Assets\SpriteDicing\Editor\Editors\AtlasBuilder.cs:106 
  at SpriteDicing.Editors.AtlasBuilder.PackTextures (System.Collections.Generic.IReadOnlyList`1[T] dicedTextures) [0x00068] in D:\Projects\SpriteDicing\Assets\SpriteDicing\Editor\Editors\AtlasBuilder.cs:77 
  at SpriteDicing.Editors.AtlasBuilder.Build () [0x0000f] in D:\Projects\SpriteDicing\Assets\SpriteDicing\Editor\Editors\AtlasBuilder.cs:32 
UnityEngine.Debug:LogError (object)
SpriteDicing.Editors.AtlasBuilder:Build () (at Assets/SpriteDicing/Editor/Editors/AtlasBuilder.cs:38)
SpriteDicing.Editors.DicedSpriteAtlasEditor:DrawInputFolderGUI () (at Assets/SpriteDicing/Editor/Editors/DicedSpriteAtlasEditor.cs:142)
SpriteDicing.Editors.DicedSpriteAtlasEditor:OnInspectorGUI () (at Assets/SpriteDicing/Editor/Editors/DicedSpriteAtlasEditor.cs:43)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
elringus commented 11 months ago

Probably a Unity regression as it's working fine on 2019.4.

github-actions[bot] commented 11 months ago

This issue is stale because it has been open 14 days with no activity. It will be automatically closed in 7 days.

SJT1988 commented 10 months ago

I am also getting this error Unity 2022.3.16f1

elringus commented 10 months ago

Unity seem to dispose/invalidate atlas asset editor before the build is complete (and even before the draw is finished):

https://github.com/elringus/sprite-dicing/blob/365db9c695297fc2b35296cadbea21606d4401dd/Assets/SpriteDicing/Editor/Editors/DicedSpriteAtlasEditor.cs#L141-L143

— after Build() is invoked, editor's OnDisable() gets called (which indicates Unity decides to dispose the editor and serializedAsset instance with it) before control flow exits the method. This is most likely a regression on the Unity side, as the behavior is weird and didn't happen in previous Unity releases.

I'm not using 2022 myself; would appreciate if someone can send a bug report to Unity and/or have an idea of a workaround/temp fix, which doesn't require significant changes on our side.

elringus commented 9 months ago

I've traced it down to AssetDatabase.SaveAssets() call:

https://github.com/elringus/sprite-dicing/blob/365db9c695297fc2b35296cadbea21606d4401dd/Assets/SpriteDicing/Editor/Editors/DicedSpriteSerializer.cs#L81

Seems like invoking those causes Unity to dispose the editor. After removing all the "SaveAssets" in all the editors (except the last one at the end of build), it seems to work fine, until you change any property, at which point it fails again, but works when repeated. As a temp workaround (until the bug is fixed on the Unity side), try removing the save calls and build twice after changing atlas properties.