[solved] Create a texture with a .png at runtime, how to make it transparent ?

Hi
I’m trying to load a png at runtime, and use it to texture a gameObject. It is working, but it is not transparent.

If i do it in the editor mode, i’m able to select the options i’m interested in :

But i can’t find those options in script mode, so when i load my png it is not transparent. Here is my code so far:

Does someone know the solution of this problem ? There has to be a way to do in script what I’m doing in the editor, i know that, and i feel dumb for not finding it :expressionless:

The “From Grey Scale” option on import is the problem. That’s modifying the image data as (presumably) the PNG doesn’t have any alpha it’s copying the greyscale value of the color into the alpha of the texture. You’d have to do this manually when doing this from script, or alternatively actually save your PNG with the alpha you want to begin with.

To do it from script after loading the file data you’d need to iterate over the pixels and override the alpha value.

The code would be something like this:

...
tex.LoadImage(fileData);
Color[] pix = tex.GetPixels(); // get pixel colors
for (int i=-; i<pix.Length; i++)
    pix[i].a = pix[i].grayscale; // set the alpha of each pixel to the grayscale value
tex.SetPixels(pix); // set changed pixel alphas
tex.Apply(); // upload texture to GPU
GetComponent<Renderer>().material.mainTexture = tex;

Generally speaking modifying pixels at runtime isn’t something you want to do too much, or offload onto the GPU, but in this case since you’re already loading the pixels on the CPU to begin with it’s not as bad.

3 Likes

Oh nice finally it works !!
Thanks :slight_smile:
love you <3

I’m facing a similar probelm. I want to load a texture from amazon logo ‘a’ png image. If I don’t use the GetPixels part and just use LoadImage, the texture gets a bluish tint. And when I add the GetPixels part, it shows the black and white properly, but replaces yellow smile with green. Here’s my code.

var fileContent = File.ReadAllBytes(path);
Texture2D tex = new Texture2D(2, 2, TextureFormat.ARGB32, false);
tex.LoadImage(fileContent);

Color[ ] pix = tex.GetPixels();
for (int i=0; i<pix.Length; i++)
pix_.a = pix*.grayscale;*_
tex.SetPixels(pix);
tex.Apply();

Well, first, that can’t be your code, since it wouldn’t compile.
Second, please use code tags when posting code.
https://discussions.unity.com/t/481379
Third, even with that code fixed
, assuming it’s roughly what’s above, it would not produce any of the behaviors you’re seeing. That either means your code looks very different, or there is a completely different issue here and you should post as a new thread. Preferably with pictures showing what the original image looks like, and what you’re seeing, with the actual code you’re using.

Answer to both of those problems (not having alpha and bluish tint of the texture), is: wrong TextureFormat in the creator of the Texture2D. It should be TextureFormat.RGBA32 and not TextureFormat.ARGB32.

File is saved using 32bit number representing one pixel and it’s created by combining R-G-B-A 4x8bits. When texture is created with TextureFormat.ARGB32 it will load bytes from alpha channel in the file, into Blue channel of “tex” texture (and also all the other channels will be wrong: Red into Alpha, Green into Red…).

Same problem can occure if render texture is created with one TextureFormat and texture2D with another, and then ReadPixels method is used to transfer render texture to texture2D.

1 Like