Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Sphax84 · Jul 25, 2015 at 03:54 PM · runtimepngloadimageloading textures8bit

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:

  1. create a Texture2D

  2. "LoadImage" file with a path

  3. 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 alt text

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _Gkxd · Jul 25, 2015 at 05:19 PM 0
Share

Is your texture format correct? It seems like the green channel was interpreted as the alpha channel on the wrong texture.

avatar image Sphax84 · Jul 25, 2015 at 06:48 PM 0
Share

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

avatar image _Gkxd · Jul 25, 2015 at 07:15 PM 0
Share

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.

alt text

formats.png (3.0 kB)
avatar image Sphax84 · Jul 25, 2015 at 07:24 PM 0
Share

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... :'(

avatar image Sphax84 · Jul 29, 2015 at 12:13 PM 0
Share

No one already had this bug before? :s

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sphax84 · Aug 06, 2015 at 04:17 PM 0
Share

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.

avatar image karl_jones ♦♦ · Aug 06, 2015 at 04:22 PM 0
Share

Public bugs are here although not all bug reports are made public(its up to the reporter) http://issuetracker.unity3d.com/

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Exception Loading PNG Bytes for Texture 1 Answer

when XR settings is enabled and cardboard is added to list, loadImage() functions crashes app when PNG is loaded 1 Answer

Fatal Exception when loading multiple image files on Android from persistent data path 0 Answers

How to show sprite changes made in a Graphics Editor during runtime in Unity Editor 0 Answers

Export objects to a .3DS file at runtime 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges