Carsten
Carsten

Reputation: 1

Unity WebGL remain localData after app update

We released our game on several platforms, including WebGL. This game saves some local stuff (app language, local users...) into the PlayerPrefs, using the browser's IndexedDB API. Though we noticed after uploading a new version of the app on our server, the data in the PlayerPrefs is gone. Is there any way of keeping these data, after a new upload of the game?

I've read taht WebGL can't write to disk, so reading/saving a file on disk isn't possible. Some topics about this problem mention the use of cookies, but couldn't find any decent documentation on how to use this.

Is there a decent solution to fix this? Kind regards, Carsten

Upvotes: 0

Views: 3482

Answers (1)

dome12b
dome12b

Reputation: 623

yes Unity is saving all data (including PlayerPrefs) to IndexedDB. You can see these files i.e. in Chrome DevTools under "Application": Storage-> IndexedDB -> /idbfs {{YOUR SERVER}} -> FILE_DATA

We you make a new build with a different Unity-Version or a different machine the hash-value changes. So the saved files from the old build can not be found by Applicaton.persistentDataPath

A workaround can be using the LocalStorage from your browser if you are only saving strings or numbers. You can write some javascript functions (for example in <script> tags in your template file) to store your text data there - it very similar to the PlayerPrefs:

var saveData = function(yourkey, yourdata){
    localStorage.setItem(yourkey, yourdata);
}

to save values. And

var loadData = function(yourkey){
    localStorage.getItem(yourkey);
}

to get them.

Create a plugin.jslib file in Unity Editor and call these functions:

mergeInto(LibraryManager.library, {
    GetData: function(yourkey){
        if(typeof loadData  !== 'undefined'){
            loadData(Pointer_stringify(yourkey));
        } else {
            console.log("Javacript function not available...");
        }
    },
    SetData: function(yourkey, yourdata){
        if(typeof saveData !== 'undefined'){
            saveData(Pointer_stringify(yourkey), Pointer_stringify(yourdata));
        } else {
            console.log("Javacript function not available...");
        }
    }

});

Now you can call them from Script as metioned here

Upvotes: 3

Related Questions