Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

WebGL Filesystem?

Discussion in 'Unity 5 Pre-order Beta' started by DarkPixel, Jan 29, 2015.

  1. DarkPixel

    DarkPixel

    Joined:
    Sep 13, 2013
    Posts:
    79
    I've done some test with Application.persistentDataPath in WebGL. I can read / write without any problem. But I don't understand where my data is.

    I can't find any anwsers, I've read that is problably using IndexedDB. Anyone can explain me how the filesystem work for WebGL?

    Thanks!
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    It should indeed work to use .NET File I/O to write to IndexedDB - not even intentionally on our side, but simply because IL2CPP implements .NET File I/O on top of posix file functions, and emscripten implements those on top of IndexedDB. However, because posix file io is synchronous (you can call fread and have it block until you get a result), and IndexedDB is not, emscripten will cache file I/O locally in memory. You may need to call

    Code (csharp):
    1.  
    2. FS.syncfs(false, function (err) {
    3.     });
    4.  
    To flush cached file writes to IndexedDB. Let us know if it works.
     
    codestage, yodda, youka_ho and 5 others like this.
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Oh, just if it wasn't clear, the above called is JavaScript, so you'd need to call it through Application.ExternalCall or using a .jslib file.
     
  4. Doddler

    Doddler

    Joined:
    Jul 12, 2011
    Posts:
    269
    I want to bump this thread to confirm what Jonas mentioned, calling FS.syncfs in javascript is necessary for persistent storage to survive between different launches of the app. You'll need to stick that code into a javascript (browser javascript) and call it via Application.ExternalCall after you finish writing to your file. It does work however, and works quite well!
     
    AblTrifork and samurai926 like this.