Using a custom indexedDB when saving to PLayerPrefs on WebGL

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: Using IndexedDB - Web APIs | MDN

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.

1 Like

Hey Alex, thanks for the answer! Great to hear that you are working on a solution :slight_smile: Im using the 5.4 beta atm, v b17.

We would need this too, currently using 5.3.x

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

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).

Yup that’s what I’ve done for now too.

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).

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