Author Topic: OnPostProcessAllAssets fails atlas creation  (Read 9410 times)

Amitloaf

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
OnPostProcessAllAssets fails atlas creation
« on: August 18, 2014, 06:15:07 AM »
I have a small script which checks whether atlases are too big by mistake. The script works well but when adding or updating an image in an atlas - it crashes. The error is:
  1. MissingReferenceException: The object of type 'Texture2D' has been destroyed but you are still trying to access it.
  2. Your script should either check if it is null or you should not destroy the object.
  3. UnityEngine.Texture.get_width () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/TextureBindings.cs:48)
  4. UIAtlasMaker.PackTextures (UnityEngine.Texture2D tex, System.Collections.Generic.List`1 sprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:147)
  5. UIAtlasMaker.UpdateTexture (.UIAtlas atlas, System.Collections.Generic.List`1 sprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:561)
  6. UIAtlasMaker.UpdateAtlas (.UIAtlas atlas, System.Collections.Generic.List`1 sprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:676)
  7. UIAtlasMaker.UpdateAtlas (System.Collections.Generic.List`1 textures, Boolean keepSprites) (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:659)
  8. UIAtlasMaker.OnGUI () (at Assets/NGUI/Scripts/Editor/UIAtlasMaker.cs:1005)
  9. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
  10.  

The script itself is:
  1. public class AtlasChecker : MonoBehaviour {
  2.  
  3.     [MenuItem("Archon/Check Atlas Sizes")]
  4.     static void CheckAtlasesMenuCommand()
  5.     {
  6.         CheckAtlases();
  7.     }
  8.  
  9.     public static void CheckAtlases()
  10.     {
  11.         string[] assets = Directory.GetFiles(".\\Assets\\Graphics\\Atlases");
  12.         int curDirLen = 2;// Directory.GetCurrentDirectory().Length + 1;
  13.         float count = 0;
  14.         foreach (string asset in assets)
  15.         {
  16.             count++;
  17.             EditorUtility.DisplayProgressBar("Checking atlases", "Please wait...", (count / assets.Length));
  18.             if (Path.GetExtension(asset) == ".png")
  19.             {
  20.                 Texture2D atlas = AssetDatabase.LoadAssetAtPath(asset.Substring(curDirLen).Replace('\\','/'), typeof(Texture2D)) as Texture2D;
  21.  
  22.                 if (atlas != null)
  23.                 {
  24.                     if (atlas.height > 2048 || atlas.width > 2048)
  25.                     {
  26.                         Debug.LogWarning("<color=yellow>Atlas \"" + atlas.name + "\" is too big (" + atlas.width + "x" + atlas.height + ")!\nMight cause game to crash on iPad.</color>");
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.         EditorUtility.ClearProgressBar();
  32.     }
  33. }
  34.  
  35. class AutoCheckAtlases : AssetPostprocessor
  36. {
  37.     private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  38.     {
  39.         AtlasChecker.CheckAtlases();
  40.     }
  41. }
  42.  

Any idea why that happens or how to work around it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #1 on: August 18, 2014, 12:14:40 PM »
NGUI's atlas maker should be already doing this check when you modify it. What version of NGUI are you using?

Amitloaf

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #2 on: August 19, 2014, 02:43:19 AM »
3.5.9
Where is that check? How can I enable this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #3 on: August 19, 2014, 11:10:39 AM »
You don't need to enable it. It's built-in. Your NGUI is also quite a bit out of date.

Amitloaf

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #4 on: August 20, 2014, 03:40:55 AM »
Ok... So I never saw any indication when my atlas is bigger than 2048x2048...
I'll update NGUI. How is the indication showing?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #5 on: August 20, 2014, 03:59:59 PM »
There is no indication. It simply fails to create an atlas giving you an error message if the texture ends up being big enough to force it to get shrunk.

Note however that this applies to the actual generated texture. It won't affect Unity's auto-shrinking some textures on certain platforms / quality settings.

Amitloaf

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #6 on: August 21, 2014, 01:57:56 AM »
Right but the max size is 4096 right? I need it at 2048..

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #7 on: August 22, 2014, 03:19:28 AM »
It can go up to 4k on desktop devices. When targeting a mobile device you get an option to limit to 2048. I can enable it for desktops as well, but why do you need to? All desktops support 4k atlases natively. You would need to go back to a videocard from the previous century for them not to.

Amitloaf

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: OnPostProcessAllAssets fails atlas creation
« Reply #8 on: August 24, 2014, 05:34:50 AM »
I agree. But we're creating the version for mobiles and we don't want to have major changes between different versions. So the workstations are configured to whichever platform (one of the several) and if someone edits an atlas on a PC configured workstation, we don't want him to ruin the atlases for the mobile version.