Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gigos22 · Jan 16, 2021 at 06:00 PM · webglwebrequeststreamingassetsbinary

How do I retrieve a binary file with UnityWebRequest?

I'm building a game for WebGL.

My main goal is to save and load data from a binary file from my StreamingAssets folder.


I've read the documentation that says that I need to use UnityWebRequest for that, but it really doesn't explains anything..


I mostly don't understand how do I de-serialize my binary file and then serialize it again and use a web request to save it back.

Can I access it from UnityWebRequest.downloadHandler?

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
1

Answer by rh_galaxy · Jan 16, 2021 at 11:59 PM

This is what I use to get level files stored as either binary or text. Then I do this to call the subroutine. But this is a windows project not a webgl one if that makes any difference. Also I don't know of the StreamingAssets folder, this works on an url.

 StartCoroutine(GetLevelFile(szMapFile, true));

You will have to write something similar to upload the file. How to create your binary data you can look up elsewhere.

 internal string szLevelTextData;
 internal byte[] aLevelBinaryData;
 public IEnumerator GetLevelFile(string i_szFilename, bool i_bBinary)
 {
     bIsDone = false;

     string url = WEB_HOST + "/user_levels/" + i_szFilename;
     www = UnityWebRequest.Get(url);
     yield return www.SendWebRequest();

     if (www.isNetworkError || www.isHttpError)
     {
         Debug.Log(www.error);
     }
     else
     {
         if (!i_bBinary) szLevelTextData = www.downloadHandler.text;
         else aLevelBinaryData = www.downloadHandler.data;
     }
     bIsDone = true;
 }
Comment
Add comment · Show 4 · 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 gigos22 · Jan 17, 2021 at 07:42 AM 0
Share

Ok, so if I understand correctly, downloadHandler.data is the actual file represented as bytes[]?

If so, can I simply convert the bytes[] into my PlayerData type?

Or can I simply use de-serialization on the bytes[]?

Also, I already have a binary serialize and de-serialize script.

avatar image rh_galaxy gigos22 · Jan 17, 2021 at 07:51 AM 0
Share

Yes, I think so. What makes me unsure if SendWebRequest is even the right way of accessing the data is that that you say you want to work in the Strea$$anonymous$$gAssets folder...

Would this work instead?

 File.ReadAllBytes()
avatar image gigos22 rh_galaxy · Jan 17, 2021 at 09:47 AM 0
Share

I didn't have to try that because your initial comment worked.

I now face another problem which is making a Put request to upload a new file.

I made a similar function to convert a PlayerData[] object to byte[]. But then when I send it through:

UnityWebRequest.Put(putURL,bytesData),

I print the error if there is and it prints:

HTTP/1.1 405 $$anonymous$$ethod Not Allowed.

I am hosting my site on HostGator, and after having a chat with tech support, they say they can't do anything about it..

Any idea how can I solve it?

Show more comments

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

118 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

Related Questions

How to get UnityWebRequest to work for downloading JSON 1 Answer

How do I get into my StreamingAssets folder for the Web? 0 Answers

New UnityWebRequest How Can I Handle the responses ?? 1 Answer

AWS WebGL Securly Get Images? 1 Answer

WebGL play video using VideoPlayer and StreamingAssets 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