Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question UnityWebRequest load assets from indexedDB

Discussion in 'Web' started by rchapman, Jan 30, 2021.

  1. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    104
    Our system downloads media files from S3 and plays them from the local file system. We generally use UnityWebRequest (previously WWW) to load the local files so that we can take advantage of the media-specific properties and features (audioClip, texture, etc.).

    This code seems to fail when loading files from indexedDB. We're experimenting with different options, but before we bang our heads against the wall too much--is this supposed to work, or do we need to convert to System.IO calls to access data in indexedDB? I can't seem to find any official guidance on this.
     
  2. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    283
    By using Application.persistentDataPath, you can save / load files in the virtual file system that emscripten built on IndexDB.
    Code (CSharp):
    1. File.WriteAllBytes($"{Application.persistentDataPath}/test.bin", saveData);
    2. var loadData = File.ReadAllBytes($"{Application.persistentDataPath}/test.bin");
     
  3. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    104
    Those are System.IO calls. What I'm wondering is whether UnityWebRequest can be used to load media files from indexedDB. This works on all other platforms, but I haven't been able to get it to work for WebGL.
     
  4. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    283
    Unfortunately it can't.
     
    Chethanreddyakkati likes this.
  5. rchapman

    rchapman

    Joined:
    Feb 13, 2014
    Posts:
    104
    Although I appreciate the insight, it would be great for someone from Unity to chime in on why this doesn't work. There is no documentation that mentions this limitation that I can find. Is this a bug?
     
  6. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    If you enable "Data Caching" under WebGL project Publishing Settings, then UnityWebRequest will utilize IndexedDB as an explicit caching mechanism for downloaded files. In that usage, the downloaded files will persist automatically and transparently into IndexedDB as the downloads occur for the first time. Manually saving the downloaded files to Application.persistentDataPath should not be needed (that would be storing the files twice)

    If you have "Data Caching" disabled, then you should be able to write files under Application.persistentDataPath to persist them in IndexedDB. Anything that is saved there should be stored under "/idbfs" directory in IndexedDB. Check out the Chrome DevTool at https://developers.google.com/web/tools/chrome-devtools/storage/indexeddb . You can use that to debug and diagnose what is persisted to IndexedDB at any given time.