Search Unity

Save File in localpath WebGL

Discussion in 'Web' started by clown_hacke, Oct 9, 2020.

  1. clown_hacke

    clown_hacke

    Joined:
    Jul 29, 2019
    Posts:
    11
    Hi,

    I'm searching for a solution to save file in unity WebGL local path.. All I found is storing in persistentdatapath.
    I need to store file in Application.datapath in WebGL. If I use Application.datapath, it gives error as "no file found". So please recommend a way for saving in local path.
    Thank you
     
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    In WebGL builds, Application.dataPath gives you the URL location of the page data file, not a path on the virtual filesystem.

    What are you trying to solve?

    The Unity data path in the virtual filesystem is currently the root of the filesystem, i.e. "/"; But that directory will not persist across page reloads.

    If you want to save a file so that it persists across page reloads, then persistentDataPath is the solution to use.

    If you are trying to save a file on the local hard drive of the user, then the only way to do that is to offer a "Save As..." dialog box, because browsers do not let JavaScript code directly access user's hard drive for security reasons. One source that goes through how to implement a "Save As..." dialog can be found at https://developers.google.com/web/updates/2011/08/Saving-generated-files-on-the-client-side.

    If you are trying to upload a file to the web server, then the web server should implement some kind of HTTP upload API/entry point to receive in data, and server-side code that would write the data on disk.
     
  3. clown_hacke

    clown_hacke

    Joined:
    Jul 29, 2019
    Posts:
    11
    Thanks @jukka_j

    I'll explain what I do so far and what are the issues faced.

    I have to run an application in raspberry pi.. So What I do is..

    1. Create an application in unity and build in WebGL format.
    2. Then I use Node and electron in raspberry pi to make the WebGL build run as an application.
    3. I have to save some user data so that every time the user closes and opens the application it will return the last state of the application.. Ex. if the user sets a value of a text from 0 to 1, then close the application, then reopens it, i have to retain the text value to 1.
    4. I used Playerprefs for this and it works perfect in editor.
    5. But in the Build, playerprefs not works sometimes.. It returns the same value always even I change the value.
    I also use Playerprefs.save().

    I searched for file saving and loading.. But it not works in WebGL..

    So Can you send some source code or reference for save and load in webgl (not playerprefs).. That will be useful for me.

    Thank you
     
  4. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    How much data do you need to save? If the amount is less than 10MB, then you can use JavaScript LocalStorage API (https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage) to persist data, by creating a .jslib for the purpose.

    PlayerPrefs and PlayerPrefs.save() should work on the web, and it backs on top of IndexedDB. Does it work on e.g. a desktop build outside Raspberry Pi/Electron environment? I wonder if that environment does not have IndexedDB available at all, that would prevent saving.
     
  5. clown_hacke

    clown_hacke

    Joined:
    Jul 29, 2019
    Posts:
    11
    I checked the same build on window's browser. It worked perfect. In raspberry pi also the playerprefs works when we quit the game normally.. But when the game crashes or quits abnormally the playerprefs not saved. I even use Playerprefs.save() occasionally.

    Any solutions for that?
     
  6. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    That is unfortunately hard to solve. If user quits out from the page, the only method for that is to use custom JS solution with beforeUnload event and localStorage instead of IndexedDB, but that will restrict the save size to 10MB.