[ANDROID] Downloading large (4000x4000) textures causes a lot of memory consumption.

So, we’re developing a game where we need to download for each game a certain gif. We have split these gifs into spritesheets and they are stored in our database, which we can access through the Rest communicating with Server.

Problem: After I load a spritesheet from the outside, thats about 4000x4000 pixels, the game eats like 100MB of RAM, which is a problem here. Here is a snippet of my code:

    private IEnumerator LoadImageFromURL(_AnimatedSprite a)
    {
        Debug.Log("Checking url");
        WWW www;
        
        if(a.HasThumbnail)
        {
           www = new WWW(a.URL);
        }

        else
        {
            www = new WWW(a.URLThumnail);
        }
        
       
        Debug.Log("Started downloading");
         
        yield return www;

        if (www.error != null)
        {
              Debug.Log("Error!");
              Switcher.mb.showMessage(MessageBox.MessageType.negative, www.error);
              a.Loaded = true;  
            running = false;
        }

        else
        {
                
            if (!a.HasThumbnail)
            {
                www.LoadImageIntoTexture(a.Thumbnail);
                
                // a.Thumbnail.LoadImage(www.bytes);
               // a.Thumbnail.Compress(false);
                a.HasThumbnail = true;
            }
            
            else
            {
                www.LoadImageIntoTexture(a.SpriteSheet);
                // a.SpriteSheet.LoadImage(www.bytes);
               // a.SpriteSheet.Compress(true);
                a.Loaded = true;
            }
   
            Texture2D.Destroy(www.texture);
            Texture2D.Destroy(www.textureNonReadable);
            Resources.UnloadUnusedAssets();
        }
        
        running = false;
        www.Dispose();
        www = null;
        string s = a.HasThumbnail ? "animation" : "thumbnail";
        Debug.Log("Finished downloading " + s);
    }

Any help is appreciated. Thanks!

As you can read here and on other pages in the docs Unity only supports a few compressed texture formats. However Android devices only support a small subset of these and if the hardware doesn’t support a particular hardware compression it’s loaded as uncomressed texture.

If you do the math:

4000 * 4000 * 4 == 64MB

If you also use mipmapping it’s even more.

ps: Are you actually sure that the gif image is loading? In the past and according to the docs only JPG and PNG images can be loaded at runtime via WWW. Maybe that has been changed. Anyways you should also read this page carefully to understand when a loaded texture is actually hardward compressed and when not

Try to compress images and cut to pieces like part 1,2,3

Here is a nice plugin, its very easy to use. You can download any small or big file from the web.
Must see this plugin

Unity Asset Store - The Best Assets for Game Making


For more details visit this blog