UnityWebRequest Consume 4MB ram when downloading 150KB Image

The size of texture increase by 30X when I download. How can I reduce it? Can anyone help me please.

UnityWebRequest www = UnityWebRequestTexture.GetTexture(url,true);

            DownloadHandler handle = www.downloadHandler;

            yield return www.SendWebRequest();

            if (!(www.isHttpError || www.isNetworkError))
            {
                Texture2D texture2d = DownloadHandlerTexture.GetContent(www);

                //copy on the CPU
                //texture2d.LoadRawTextureData(texture2d.GetRawTextureData<byte>());
                //texture2d.Apply(true,true);

                Sprite sprite = null;
                sprite = Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), Vector2.zero);
                if(sprite!=null){
                    AdSprites.Add(sprite);
                    AdTextures.Add(texture2d);
                }
            }

It’s actually not when you download it but when you decompress the image to use it, ie., to send it to the graphics card, read/write pixels from it, etc…

You can keep it in its raw JPG / PNG format (compressed array of bytes that you had downloaded) without decompressing it, but there’s not much you can do with it from the Unity standpoint until you decompress it.

1 Like

Thank you for answer. Am I decompressing it with this line.

Texture2D texture2d = DownloadHandlerTexture.GetContent(www);

I’m not using comment lines at actual code. Just creating sprite with this texture but unfortunately it consume 4mb for each photo.

A Sprite is the same thing as a Texture, essentially, plus it has extra information to tell Unity where on the entire Texture surface the Sprite comes from, which is how you get multiple sprites on a single texture, for instance, how big it is, details of its shape, etc.

So yes, by making a sprite out of it you are “using” it because the Sprite retains a reference to the Texture so that it can render if need be.

2 Likes