SetPixels and ExportToPNG - Alpha value missing?,

Hello. I made a script to generate a random map and then export it into PNG format. Everything works fine except for one sprite with alpha values that apparently get lost somewhere.

(Left is scene view, right is the exported PNG).
Code I’m working with:

    void ExportToPNG()
    {    
        int ppu = 16;
        //size x ppu 100x16
        Texture2D tex = new Texture2D(width*ppu, height*ppu, TextureFormat.RGBA32, false);

    
        [removed bits of the big loop code here unrelated to the issue]

        //drawing boss icon on the PNG file
        tex.SetPixels((x*ppu)-8, (y*ppu)-8, ppu*2, ppu*2, GetCurrentSprite(_tilemaps[tm].GetSprite(new Vector3Int(x,y,0))).GetPixels());

        tex.Apply();               
        byte[] bytes = tex.EncodeToPNG();
        File.WriteAllBytes(Application.dataPath + "/../map_"+mapsGenerated+".png", bytes);
        mapsGenerated++;        
        //clearMap();
    }

        
    Texture2D GetCurrentSprite(Sprite sprite)
    {
        Color[] pixels = sprite.texture.GetPixels((int)sprite.textureRect.x, (int)sprite.textureRect.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height);
        Texture2D text = new Texture2D((int)sprite.textureRect.width, (int)sprite.textureRect.height, TextureFormat.RGBA32, false);
        text.SetPixels(pixels);
        text.Apply();        
        return text;
    }

I’ve been stuck here for a while now and can’t seem to find a solution or what’s actually going wrong.
Any ideas? Thanks in advance.

,

@Eno-Khaon @metalted It seems to go missing during the “GetCurrentSprite” call. I’ve tried debugging it as such:

    Texture2D GetCurrentSprite(Sprite sprite)
    {
        Color[] pixels = sprite.texture.GetPixels((int)sprite.textureRect.x, (int)sprite.textureRect.y, (int)sprite.textureRect.width, (int)sprite.textureRect.height);
         //the troubled icon is the only 32x32 sprite
        if(sprite.textureRect.width==32)
        {
            for(int p=0;p<pixels.Length/2;p++)
            {
                pixels[p] = new Color(0f, 0f, 0f, 0f);
            }
        }
        Texture2D text = new Texture2D((int)sprite.textureRect.width, (int)sprite.textureRect.height, TextureFormat.RGBA32, false);
        text.SetPixels(pixels);
        text.Apply();        
        return text;
    }

result:
190025-pic1.png

new Color(1f, 1f, 1f, 0) returns black as well, while new Color(1f, 1f, 1f, 1f) returns white (as it should).
190026-pic2.png

Basicly ignoring the alpha no matter what on the exported PNG.

I think you have the wrong idea about what you’re doing here. You have this line of code and comment:

 //drawing boss icon on the PNG file
 tex.SetPixels((x*ppu)-8, (y*ppu)-8, ppu*2, ppu*2, GetCurrentSprite(_tilemaps[tm].GetSprite(new Vector3Int(x,y,0))).GetPixels());

This does not “draw” your texture onto the other texture. This replaces the pixels in the rectangular region in target image with the pixels of your sprite texture. It does not “draw” the image onto the background. This sounds like you want to do the same that was asked over here.