Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by rodeowild · Nov 17, 2020 at 05:32 PM · texture2dpngloading textures

Exception Loading PNG Bytes for Texture

I am storing a PNG in a textfile. I convert it to a PNG using:

             myTexture.EncodeToPNG()


and then save. I then load the bytes with:

             Texture2D texture = new Texture2D(100, 100);
             texture.LoadRawTextureData(myPNGBytes);


This throws the following error:

            UnityException: LoadRawTextureData: not enough data provided (will result in overread).  


I know that the PNG is stored correctly because when I use an online base64 converter the image is displayed. I've tried loading with several different formats, but all throw the same exception. What am I doing wrong?


EDIT: when I check the format of the Texture2D before saving, it says RGB24. I get the same error when I instantiate the texture with this format and then load.

EDIT: Even when I skip the saving step and just do:

             byte[] bytes = result.Texture.EncodeToPNG();
             Texture2D texture = new Texture2D(100, 100);
             texture.LoadRawTextureData(bytes);


I get the same exception. So my question is what is the inverse of Texture2D.EncodeToPNG()

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andrew-lukasik · Nov 17, 2020 at 07:49 PM

EncodeToPNG bytes aren't raw. Those're, well, encoded.

 byte[] bytes = texture.GetRawTextureData();// raw bytes to save
 texture.LoadRawTextureData( bytes );// this how to load raw bytes
 texture.Apply( updateMipmaps:true , makeNoLongerReadable:true );// uploads changes to GPU

Raw in this context mean GPU-ready. Encoded to png, on the other hand, are file system - ready.


Alternatively, store/read PNG bytes:

 byte[] PNGbytes = texture.EncodeToPNG();// png-encoded bytes to save
 
 texture.LoadImage( PNGbytes );// this is how to load png-encoded bytes
 texture.Apply( updateMipmaps:true , makeNoLongerReadable:true );// uploads changes to GPU
Comment
Add comment · Show 1 · 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 andrew-lukasik · Nov 17, 2020 at 07:50 PM 0
Share

PS: You may want to compress these raw bytes before saving (+decompress after loading) to save on storage space.

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

143 People are following this question.

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

Related Questions

How to convert .png to texture from file path? 1 Answer

How to save a RenderTexture as a 1-channel 8-bit grayscale PNG? 0 Answers

Create .png file from script, then import as asset 1 Answer

Sprite.Create looks compressed 1 Answer

Saving an EncodeToPNG(); to disk 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