- Home /
How to load PNG 8 bit in runtime
Hi,
I'm currently facing a odd bug with Unity when I load PNG files with "LoadImage" method.
What I do is the following:
create a Texture2D
"LoadImage" file with a path
Save texture2D in a file with "EncodeToPNG" and "File.WriteAllBytes" methods
Everything works well with 24b PNG files with or without alpha transparency (the original file loaded is the same as the saved texture2D).
But as soon as I try to load a 8bit PNG file with or without alpha transparency, Unity gets a weird texture and save that weird texture to PNG as well. The original file is not at all like the saved texture2D...
What I get is something semi-transparent (even when there is no transparency in original file) with purple/pink colors.
See this screenshot: On the left, what Unity gets after loading file and/or saving texture On the right the original file that Unity should load
Is your texture format correct? It seems like the green channel was interpreted as the alpha channel on the wrong texture.
The original picture is on the right and it's exported by Photoshop in PNG 8Bit (indexed colors). The alpha is one color of the indexed palette.
I tried many different formats.
Here is my code:
//Load file data
byte[] fileData = File.ReadAllBytes(path);
//Create texture2D
Texture2D newTexture = new Texture2D(2, 2);
//Load bytes into texture2D
newTexture.LoadImage (fileData)
I also tried this:
Texture2D newTexture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
and this:
Texture2D newTexture = new Texture2D(2, 2, TextureFormat.RGB24, false);
and many other formats but it seems that nothing changes as soon as I use LoadImage method...
EDIT: You are ~right about the colors. Unity has loaded the Red channel as alpha, the green as red, the blue as green and left blue entirely at 1... Very strange... How can I solve that? :s
There might be a difference between ARGB and RGBA formats, though I never had a problem using them interchangeably.
This is from the texture import settings, under "Format." Change Texture Type to "Advanced" to see these formats.
The problem is at runtime, not at import as asset.
$$anonymous$$ore precisely, the problem is with loading that PNG at runtime. Saving works as it should. The picture is corrupted while loading.
Have you been able to load correctly (at runtime) a PNG 8Bit? If yes, do you have code please? :)
EDIT: At import as asset, the picture is displayed correctly... I don't understand why it doesn't work at runtime while PNG24 and PNG32 are working... :'(
Answer by karl_jones · Aug 06, 2015 at 02:57 PM
What version of Unity are you using? This is a bug that I fixed recently. Try using the latest patch version.
Edit: Just checked, 5.1.2 has the fix:
Graphics: Fixed issue in Texture2D.LoadImage when loading indexed PNG images that contain an alpha channel.
If you still have problems in this version then please open a bug report:https://unity3d.com/unity/qa/bug-reporting
Karl
Thanks a lot! That means that was a real bug! :) Where can I see the bugs list to check in the future before asking here please? :)
EDIT: I was in Unity 5.1.1f1. Updating to 5.1.2f1 fixed the issue.
Public bugs are here although not all bug reports are made public(its up to the reporter) http://issuetracker.unity3d.com/
Your answer
![](https://koobas.hobune.stream/wayback/20220611233014im_/https://answers.unity.com/themes/thub/images/avi.jpg)