Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

WebGL - Accessing IndexedDB Data Saved By JS from within Unity (C#) Code?

Discussion in 'WebGL' started by OChrisJonesO, Feb 27, 2018.

  1. OChrisJonesO

    OChrisJonesO

    Joined:
    Jun 27, 2015
    Posts:
    13
    I am working on a WebGL application that has a lot of HTML/CSS/JS. With that non-unity code, I am storing some data inside of the browser's IndexedDB.

    The current flow goes like this:
    Page load -> Unity build loads/initializes -> Unity automagically creates IDB DB and Object Stores (to save /Unity, /PlayerPrefs, etc) -> JS code calls to Unity, which then calls back to JS with the value of Application.persistentDataPath (which in my case, is /idbfs/eca886cdc5b40e468bd538ad82b50cc7") -> JS code runs to open the same IDB DB Unity is using ("/idbfs" database, "FILE_DATA" objectstore) -> JS code successfully saves data to said DB, theoretically in the same directory that Unity stores it's stuff (/idbfs/eca886cdc5b40e468bd538ad82b50cc7).

    Here's a screenshot showing all of that working. /idbfs/eca886cdc5b40e468bd538ad82b50cc7/Catalog/000018.db is the custom data that my JS is storing.



    So that is all working fine. However, I'm not seeing this data show up on the Unity side of things. I have a C# method that gets called from JS, after all the data has been stored and shows up successfully in the IndexedDB, to simply log out the contents of it's directory:

    Code (CSharp):
    1.     public void ListIndexedDBFiles() {
    2.         foreach (string file in System.IO.Directory.GetFiles(Application.persistentDataPath)) {
    3.             Debug.Log ("File: " + file);
    4.         }
    5.         foreach (string folder in System.IO.Directory.GetDirectories(Application.persistentDataPath)) {
    6.             Debug.Log ("Folder: " + folder);
    7.         }
    8.     }
    The result of doing this call will log out two folders, the /PlayerPrefs and /Unity folder. It does not log out anything about /Catalog/000018.db, like I would expect. I am also manually calling the following code from a .jslib to sync Emscripten's file system, but to no avail.

    Code (JavaScript):
    1. FS.syncfs(false,function (err) {
    2.     // handle callback
    3. });
    Is there any way to access data ("files") from the IndexedDB that were not created inside of Unity, but rather created through the parent page's JS? I've tested and if I save custom files inside Unity to Application.persistentDataPath, and then log them out (using the ListIndexedDBFiles() call) they do show up. However, this isn't an option for us, as we have to generate and store that data on the front-end.

    I also can't just send the data between the two using SendMessage and have Unity save the file from that data, because the SendMessage call blows up when I pass real data through (sending small dummy data works, but using real data does not, I think due to the size).

    I may just be missing something obvious. Any help?
     
    crns13 likes this.
  2. OChrisJonesO

    OChrisJonesO

    Joined:
    Jun 27, 2015
    Posts:
    13
  3. MorbidDesign

    MorbidDesign

    Joined:
    Jun 4, 2017
    Posts:
    4
    Hi @OChrisJonesO, this issue is pretty much like the one I'm suffering right now... Did you find a solution?

    Thanks!
     
  4. AlexHell

    AlexHell

    Joined:
    Oct 2, 2014
    Posts:
    167
    As I know the only one working solution - use JS plugin with code like
    Code (CSharp):
    1. var command = self.db.transaction([self.store], "readonly").objectStore(self.store).get(key);
    2. command.onsuccess = function(e)
    3. {
    4.   callback(null, result);
    5. }
    and invoke this JS code (for read) from C#

    read docs about
    - unity webgl c# <> js exchange
    https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
    - indexedDB in js (not unity related)
    https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API

    good example of indexedDB solution with unity is
    https://github.com/kongregate/Unity...MLHttpRequest/WebGLCachedXMLHttpRequest.jspre
    it's related to cache assets (from assetbundles for example) but not limited, you can use only JS code for IDB read. I cant post minimal complete code as example for you, work with it yourself please
     
  5. WILEz1975

    WILEz1975

    Joined:
    Mar 23, 2013
    Posts:
    368
    The post is old but I cannot find a solution.
    It appears my files are saved:

    Screenshot_27.png

    How can I open the gif file and check everything is ok?

    If I try to open the file directly it won't find it.
    Screenshot_28.png

    Where am I doing wrong?
     
    Marks4 likes this.