How to force webrequest to return texture with alpha?

My webrequests are returning a texture that has an alpha channel, as RGB8. I need it to return with an alpha so I can use transparency.

Can anyone tell me how I would force the texture to return with an alpha channel?

Thankyou!

did you try this?

Yes, that is just getting a texture - it does not force it to come in as RGBA etc

True the same here. I never used a transparent image, but it does not work.
I also tried with this code instead of UnityWebRequestTexture, but also no luck.

using (UnityWebRequest www = UnityWebRequest.Get(url))
{
    yield return www.SendWebRequest();
    if (www.error != null)
    {
        Debug.Log(www.error);
    }
    else
    {
        Texture2D tex = new Texture2D(0, 0, TextureFormat.ARGB32, false);
        tex.LoadImage(www.downloadHandler.data);
    }
}

It works if i load the texture in a standalone build, without a webrequest.

If the source image file has been verified to indeed contain an alpha channel and it works on native win32 etc., this does sound like a bug. Can someone report it as an issue please?

(also if you have a project that you can host or zip up, that might help)

i have a online demo here: .…
and the zipped project here: .…

you get a transparent image in the editor, but in the browser it’s a black background.
the bug report thing is not my friend… :hushed:

EDIT: links removed…

Thanks! Filed a bug on your behalf on this for verification.

Thank you for posting the demo project above.

When debugging this, I found it convenient to add a Debug.Log call to print the texture format to the console, e.g. in LoadTexture:

Debug.Log($"format = {returnTexture.format}");

This showed that even though I loaded a PNG file with alpha channel, the texture format used was RGB24.

Short-circuiting the call to resize_image in the UploadTexturePlugin UploadTexture function, and simply calling SendMessage to send the target file blob directly back to the Unity script worked and gave a ARGB32 texture format.

Looking at the resize resize_image implementation in the UploadTexture.jslib, it’s encoding the canvas image as JPEG, which doesn’t support an alpha channel. If image/png is used instead then again a ARGB32 texture can be created.

canvas.toDataURL("image/png");

I’ve closed out the bug for this on our end. If you have another case where it appears that the unity texture/sprite creation isn’t working correctly, please post again here, or create a bug report directly (using Help → Report a Bug… in the Unity Editor).

1 Like

:roll_eyes:
yep, that solved the problem…