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.
,


