Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by The_Kakaze · May 19, 2020 at 02:02 PM · webglc# to javascript

Passing Byte[] to JSLIB method

I am attempting to write a save file to disk in a WebGL build of my game. In the other versions, I created a file using:

BinaryFormatter.Serialize(filestreamPath, saveData);

where saveData is a serializable struct with the data i want to store in it. However, this method doesn't work because it will only write to Application.persistentDataPath and from there the data seems to be inacessable by my JSLIB methods as it is protected in IndexedDB. I can confirm that it is written there with the correct name, but as for downloading it to disk I am stuck. I have the path name but i get errors when trying to access the file.

So attempt two is to create a byte[] that is the same as what is being written to filestreamPath in the above example like so:

var ms = new MemoryStream();

BinaryFormatter.Serialize(ms, saveData);

byte[] formattedFS = ms.ToArray();

DownloadBytes(filename, formattedFS);

This appears to create a byte array with the proper length in C#, but once in JSLIB the method I'm using will not write this to file correctly:

 DownloadBytes: function(filename, fstream) {
     var element = document.createElement('a');
     element.setAttribute('href', window.URL.createObjectURL(new Blob([fstream], { type: 'application/octet-stream' })));
     element.setAttribute('download', (Pointer_stringify(filename)));
     element.style.display = 'none';
     document.body.appendChild(element);
     element.click();
     document.body.removeChild(element);
 },

This method downloads to a file with the name 'filename'-- but it only writes 9 ints into that file rather than the byte[] that i passed it with a length closer to 600 and presumably contains no ints, only binary data.

My question is basically, what am I doing wrong here? Do I need to assign the incoming argument to a byte array in JS then write it to the file? If so, how do I do that?

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
Best Answer

Answer by The_Kakaze · May 20, 2020 at 11:49 AM

Byte [] passed from C# require a specified length to be properly re assembled in JS:

 DownloadBytes: function(filename, data, size) {
     var contentType = 'application/octet-stream';

     var bytes = new Uint8Array(size);
     for (var i = 0; i < size; i++)
     {
         bytes[i] = HEAPU8[filename + i];
     }

     var element = document.createElement('a');
     element.setAttribute('href', window.URL.createObjectURL(new Blob([bytes], { type: contentType })));
     element.setAttribute('download', (Pointer_stringify(filename)));

     element.style.display = 'none';
     document.body.appendChild(element);

     element.click();

     document.body.removeChild(element);
 }
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 clown_hacke · Oct 19, 2020 at 12:53 PM 0
Share

Hi @The_Kakaze Can you send the full code for save load in webgl? I'm stuck in it. Thank you

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

207 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

What is the best way to write Serialized Save Data to a file using WebGL 1 Answer

C# to WebGL 2 Answers

WebGL issue - Release Build triggers error, Development Build runs fine 0 Answers

How to Make Proper Font used in WebGL build? 2 Answers

Pass mouseevents from Iframe to unity application (WebGL) 0 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