Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Get local file

Discussion in 'WebGL' started by TOES, May 23, 2021.

  1. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    94
    How can I load a binary file from the users local harddisk from WebGL? I assume you will need a javascript plugin for this, as it is possible to launch a filebrowser letting the user select a file from his local harddisk using javascript from a webpage, but how to get the bytes from the WebGL unity app?
     
  2. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    94
    Is this forum completely dead?

    Answering my own question yet again. Need a jslib plugin,do all the stuff Unity cannot do. Then load the file using js's FileReader, and readAsDataURL That gives you a gigantic string you can send back to Unity and decode there. That is a huge waste of memory and also ridiculously slow with loads of overhead, but seems this is the only way unless you want to spend weeks finding a better workaround, since there is basically no documentation if you want to do anything more complex than the basics.

    After grappling with this and other Unity WebGL related issues I would not recommend anyone using Unitys WebGL for any serious work though, insane long compiling time, huge memory overhead, slow loading, poor integration with javascript on the webpage, poor GLSL support, poor documentation are just some problems awaiting you. Whatever time you save by using Unity, you probably have to spend optimizing and working blindfolded.

    Probably better off with three.js or something similar. Runs instantly, gives full access to anything you need on the webpage, low memory footprint, great support for GLSL and so on...
     
  3. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    If you are asking for help on a Sunday, please have patience to wait for at least for a business day before growing impatient. People typically enjoy their leisure time outside work on the weekend.

    This is one way to put it. Similarly, developers should use C# scripts to develop all the things that Unity cannot do.

    The intent for Unity WebGL JavaScript code is not to encompass all the possible web features, but offer a general language interop mechanism that allows developers to bring in general JS code for web-related tasks.

    There will be a +33% size overhead coming from base64 encoded binary data if you use a string as a marshalling primitive. The other way is to directly marshal the binary data as-is without transforming it to a string. Maybe these sources of documentation can get you started:
    - https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
    - https://www.slideshare.net/unity3d/adding-love-to-an-api-or-how-to-expose-c-in-unity
    - https://stackoverflow.com/questions/48258980/marshal-c-sharp-method-to-unity3d-with-c
    - https://emscripten.org/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.html
     
  4. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    94
    Thanks for your reply, it is the first reply to any question I had in months, so I think generally I am not impatient just frustrated. Perhaps it was my complaining and not my question that generated a response this time, who knows. If nothing else, coding in Unity WebGL proves an immense amount of patience, as you have to wait a minimum ten minutes compiling for each time you can test a slightly new approach. Do you see why I have to work on a Sunday to make up for all this waiting and finish before my deadline?

    The editor instant playback is nice, but helps nothing when you need to test webpage integration. And you do need to test in the browser all the time, due to memory problems, javascript bridging issues and so on. I do not like to complain, but these are real issues that has no solution today except avoiding Unity for serious WebGL work, I see a lot of frustration among developers in various forums over Unity WebGL and very few successful projects. Project Tiny looks promising, but is still in an unusable state.

    Unfortunately I found my solution does not work after all. It only works until the string I am sending is larger than 10mb or so, then just an indexoutofbounds crash happening once triggering SendMessage(objectName, funcName, result); from js. Internally the webpage can handle huge strings though, so I found something goes wrong between SendMessage on the webpage and the receiving method in Unity for whatever reason. Got to split the string up into manageable parts and make some kind of streaming system I suppose. Or try my luck with the binary marshal approach and hope it is not bugged for large datasets as well.
     
  5. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    827
    This is very true, agreed. That is why this has been our highest priority item for last year and this year, and now finally in Unity 2021.2 Beta we should be landing the planned improvements to build times, which bring a new faster compiler, and a new incremental compilation pipeline. We are also looking at shipping an inter-build compilation cache to speed up repetitive compilations.

    Also agreed. It is well known that web browsers have issues with scaling up memory usage. That issue is being discussed with the web standards bodies at https://github.com/WebAssembly/design/issues/1397.

    This is unfortunately true in large parts. What I would like to remind to anyone is that there are multiple parties at play, and people sometimes do not see that, but unilaterally (and angrily) blame Unity for providing a bad platform. We are doing the best we can to deploy to a platform that has design issues, all the while trying to collaborate with the standards bodies to actually improve the web so that it would scale to the sizes of applications that Unity developers do.

    There are a lot of Unity use cases that do not have solutions today on the web, and dealing with these day to day, we share the pain with our developers.

    That is peculiar. If you can, please raise a bug report. To my understanding the marshalled string should be allocated on the heap, so its size should be limited only by the size of free space left on the heap, which should be hundreds of megs at page startup on mobile, and 1-2gb on desktops.

    If the issue turns out to be an out of memory issue, then using binary marshalling will not help the issue. Though if the issue is something else, then it might be possible that using binary marshalling sidesteps the problem. Apologies for the trouble - if you're able to report this as a bug with a test case, our QA should be able to figure out the root cause, and offer a fix, rationale for the current behavior, or a workaround.
     
  6. sumpfkraut

    sumpfkraut

    Joined:
    Jan 18, 2013
    Posts:
    204
unityunity