Crave For Games

You are in the Article list. If you want a full list of the StoneDrop develompent log notes you can go to the DevLog page.
Saving data in WebGL build (Unity3d)


Saving data in WebGL build (Unity3d)

Goal: save valuable game data as files (not in PlayerPrefs) in WebGL build.

Approach: JS workaround, source code attached.


As I mentioned before, I had problems with saving files between game sessions in browsers. In my C# code I use (following the Unity manual advice) Application.persistentDataPath to store valuable data in (encrypted) files. After a short googling session I've found a forum thread about this problem[1] and came up with the solution: add the following code right after the file you write to was closed


#if UNITY_WEBGL && !UNITY_EDITOR Application.ExternalEval("FS.syncfs(false, function (err) {})"); #endif


To understand this one should be aware of the fact that EMScripten implements file saving as an asynchronous operation in IndexedDB (database) which stored in a cache in the operating memory. To save the database to a hard drive the WebGL application has to explicitly trigger cache synchronization (it's a synchronous operation in the IL2CPP backend thus cannot be automatically triggered by the Unity engine each time the application uses write-to-file-methods).


Now in the WebGL build[2] the game is autosaved if the browser tab was closed or reloaded and next time the progress is loaded.

UPD Jun 2016: right now this feature is disabled. Later there will be aded a "Continue" button to each level with autosaved progress.


Link 1:

Link 2:

This article in social networks: