Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using a custom indexedDB when saving to PLayerPrefs on WebGL

Discussion in 'Web' started by TheMordeus, May 25, 2016.

  1. TheMordeus

    TheMordeus

    Joined:
    Nov 30, 2012
    Posts:
    2
    We have a problem in that we run several apps from the same web domain, and we use PlayerPrefs to store some game data locally. They share the same keys (stupid, but it has always worked because on win/ios/etc, its stored in separate locations depending on the name of the app). On WebGL however, they all seem to be tored on the same location based on the domain the app is running from.

    Is there a way for Unity to change the location of this data, f.ex by opening separate IndexedDBs? CAn it be done with plugins/external calls?

    Reference: https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB
     
  2. alexsuvorov

    alexsuvorov

    Unity Technologies

    Joined:
    Nov 15, 2015
    Posts:
    327
    Hello TheMordeus.

    Starting from version 5.5, the PlayerPrefs storage path will depend on the document url folder (excluding the trailing filename, i.e. index.html, if present), so games hosted at your-domain/game1 and your-domain/game2 will have separate PlayerPrefs storages, which is particularly important for the shared domains like dl.dropboxusercontent.com etc. The cache storage will also be separated for streamed assets. We might consider backporting this scheme to previous versions if the issue appears to be critical.

    Which version of Unity are you using? I will check if it is possible to create a workaround for your version.
     
    Last edited: May 25, 2016
    Nolex likes this.
  3. TheMordeus

    TheMordeus

    Joined:
    Nov 30, 2012
    Posts:
    2
    Hey Alex, thanks for the answer! Great to hear that you are working on a solution :) Im using the 5.4 beta atm, v b17.
     
  4. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    We would need this too, currently using 5.3.x
     
  5. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    236
    As a (temp) workaround, couldn't you save a file separately. This way you could give it an unique name.
    We're saving binary files in the Application.PersistantPath in WebGL. You do need to use syncfs then.

    So far this has been working fairly ok for us

    -PIeter
     
  6. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Another solution is to implement a "namespace" solution, e.g: append a certain prefix to data that is stored to PlayerPrefs per game.

    For example: when storing key "playerHealth", we will store that as "myGame:playerHealth" (or whatever other addition you may want to add).

    All access to PlayerPrefs in our games is done via a wrapper class that does all this work for us (adds the prefix automatically so we don't have to worry about it).
     
  7. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Yup that's what I've done for now too.
     
  8. Nolex

    Nolex

    Joined:
    Dec 10, 2010
    Posts:
    115
    Thanks for this information!

    @alexsuvorov Maybe there is a way how to disable this for new versions of Unity? I use Unity 2019 and I want to use the old system that was in Unity 5 (no folder dependency).
     
    Last edited: Jan 10, 2022
  9. wartron

    wartron

    Joined:
    Jan 23, 2015
    Posts:
    8
    yea im in the same boat. I put webgl builds in a s3 buckets thats like game/{buildnumber}/ and would like playerprefs/indexDb to be without the scope of the url