Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 valentin-simian · Jan 30, 2018 at 01:11 AM · texture2dserversidebytearraybackendheaders

Texture2D.LoadImage unable to parse bytes

I'm downloading a raw byte array to be used as a Texture2D using a technique by at SO.

 UnityWebRequest www = UnityWebRequest.Get("myurl");
 
 DownloadHandler handle = www.downloadHandler;
 
 yield return www.Send();
 
 if (www.isNetworkError)
 {
 
     UnityEngine.Debug.Log("Error while Receiving: " + www.error);
 }
 else
 {
     UnityEngine.Debug.Log("Success");
 
     Texture2D texture2d = new Texture2D(8, 8);
 
     texture2d.LoadImage(handle.data);
                     
 }

LoadImage, however, can't parse the byte array and gives me 'red question mark' fallback.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by valentin-simian · Jan 30, 2018 at 01:14 AM

So the issue was in LoadImage not being able to parse the image's header. The function starts reading the texture as a bytearray, however first encounters the header, and doesn't know what to do with it.

Furthermore, the image was hosted on an S3 bucket, which assigned a header by default. Removing the header on S3 solved the issue.

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

Answer by sunwugong · May 19 at 11:04 AM

I know I am reviving a necro-thread, but I am having a similar issue. I have a png on S3 that I am accessing via an API endpoint that retrieves the raw binary via a node.js use of AWS SDK's getObject(). What is returned also fails to parse. I get a byte array on the Unity side fine, but the LoadImage(byteArray) call returns false. I am a bit stuck, not sure how to remove a header, or even check if one is there, etc.


EDIT: I solved my issue, but I have no idea why I had to do it this way. I will share here for others in case they have a similar issue, and maybe those more knowledgeable than me can point out why I ran into this and potentially suggest better solutions.


For background, I am using Node.js Express to expose API endpoints that allow image access. Within this Node code I uses the AWS SDK getObject() function to fetch the PNG and send it back as so (bucket junk in the params):


     s3.getObject(params, function(err, data) {
         res.write(Buffer.from(data.Body));
         res.end(null);
       });

Theoretically, the Buffer.from(data.Body) here should return the byte array I need for LoadImage() in Unity, and sure enough, if I check the length in Node for my test image it is exactly the number of bytes I expect so seems to be correct. When I actually hit the end point in Unity, however, it comes out as a much longer byte[] that won't parse. On the Unity side I was using the following on a successful request (where uwr is the UnityWebRequest that succeeded):


     byte[] binaryImageWeb = uwr.downloadHandler.data;

My workaround (which I don't love because I don't understand why it is needed, but it works) is to more explicitly return the byte array as a stringified JSON array and then process it on the Unity side through deserialization of the text return rather than the provided .data byte[]. The new Node code is as follows:


     s3.getObject(params, function(err, data) {
         res.write(JSON.stringify(Buffer.from(data.Body).toJSON('hex').data));
         res.end(null);
       });

And the Unity code changes to this:


     byte[] binaryImage = JsonConvert.DeserializeObject<byte[]>(uwr.downloadHandler.text);

Using this approach I get the correct expected-length byte array which successfully parses into a Texture.

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

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

78 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

Related Questions

Unity 5.0 Trying to Change the Image - Source Image via Script 3 Answers

Create / Modify Texture2D to Read/Write Enabled at Runtime 3 Answers

How to not see white background on texture 1 Answer

How to create texture from multiple sprite in unity2d? 0 Answers

Casting Error C# between Object[] and Texture2D[] 2 Answers


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