Sprite Atlas Generation in android build

Alright, so this is my first post on the Unity forums. So excuse my ignorance of any unwritten rules.

I have built an automatic sprite atlas generator, that searches my project for specific scripts and constructs an atlas from them. This all works fairly well, I have even been able to make use of this for my 3D game objects, by setting the UVs accordingly.

However, I am running into a bit of an issue. Its not major but I am trying to clean up my tool for quicker use. Essentially, I use PackTextures to build the texture, convert it to ARGB32 and then into my desired format (be it DXT or PVRTC etc). This however, seems only to work correctly when my build settings are set to PC Standalone, not Android or iOS.

Texture2D newTexture = new Texture2D(maxSize
            , maxSize
            , format
            , enableMipMapping);

_TextureRects = newTexture.PackTextures(textures.ToArray()
            , padding
            , maxSize
            , makeNoLongerReadable);

Texture2D correctedTexture = new Texture2D(newTexture.width
            , newTexture.height
            , TextureFormat.ARGB32
            , enableMipMapping);

correctedTexture.SetPixels(newTexture.GetPixels());
EditorUtility.CompressTexture(correctedTexture
	     , format
	     , TextureCompressionQuality.Normal);

The UVs seem to be remapping correctly regardless of platform. It seems to be the generated texture that is causing the issue. As shown below:

874-StdAlnCap.png

(As seen on PC)

875-AndroidCap.png

(As seen on Android)

Any help, would be greatly appreciated. Thanks in advance!

This looks like a bug that will live forever in Unity 3.x. I’ve seen it happen with every UI plug-in I’ve tried that relies on Unity’s texture packer.

It seems to happen when your textures’ combined width or height is exactly or over the atlas limit you are targeting, but apparently only on mobile build targets. Although I’ve not seen your exact example have it happen… personally I’ve only seen this when one of the textures is actually equal or larger than the full extent of the final atlas. For example, if I’m trying to make a 2048x2048 atlas and my largest texture is actually 2048 high or wide, this error happens, even if it is the only texture I’m trying to put in the atlas.

I think there must be some kind of padding math error on this codepath in Unity.

I’ve found a slightly ugly workaround, if you reduce your offending texture by a couple of pixels to be less than the final atlas boundary, it no longer happens.

Not sure if this will help you or not, but thought I’d share.