Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Using a custom indexedDB when saving to PLayerPrefs on WebGL

Discussion in 'WebGL' 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:
    317
    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
  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,335
    We would need this too, currently using 5.3.x
     
  5. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    68
    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:
    2,655
    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,335
    Yup that's what I've done for now too.