'How do you save game data when playing a Unity webGl game on a mobile browser?

My Unity WebGL game runs on the mobile web browser after heavily optimizing the Audio and texture data. Saving the game data works fine on the desktop environment. But when I play the same game on a mobile browser, there seems to be no saving of game data anywhere. My code to save and load data locally is given below.

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;

public class DataAccess
{
    [DllImport ( "__Internal" )]
    private static extern void SyncFiles ( );

    [DllImport ( "__Internal" )]
    private static extern void WindowAlert ( string message );

    public static void Save ( Stats stats )
    {
        string dataPath = string.Format ( "{0}/Stats.dat", Application.persistentDataPath );
        BinaryFormatter binaryFormatter = new BinaryFormatter ( );
        FileStream fileStream;

        try
        {
            if ( File.Exists ( dataPath ) )
            {
                File.WriteAllText ( dataPath, string.Empty );
                fileStream = File.Open ( dataPath, FileMode.Open );
            }
            else
            {
                fileStream = File.Create ( dataPath );
            }

            binaryFormatter.Serialize ( fileStream, stats );
            fileStream.Close ( );

            if ( Application.platform == RuntimePlatform.WebGLPlayer )
            {
                SyncFiles ( );
            }
        }
        catch ( Exception e )
        {
            PlatformSafeMessage ( "Failed to Save: " + e.Message );
        }
    }

    public static Stats Load (Stats stats )
    {
        //Stats stats = null;
        string dataPath = string.Format ( "{0}/Stats.dat", Application.persistentDataPath );

        try
        {
            if ( File.Exists ( dataPath ) )
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter ( );
                FileStream fileStream = File.Open ( dataPath, FileMode.Open );

                stats = ( Stats ) binaryFormatter.Deserialize ( fileStream );
                fileStream.Close ( );
            }
        }
        catch ( Exception e )
        {
            PlatformSafeMessage ( "Failed to Load: " + e.Message );
        }

        return stats;
    }

    private static void PlatformSafeMessage ( string message )
    {
        if ( Application.platform == RuntimePlatform.WebGLPlayer )
        {
            WindowAlert ( message );
        }
        else
        {
            Debug.Log ( message );
        }
    }
}

Does anyone know whether this can be done, if so how?



Solution 1:[1]

Tell whoever your client is

Note that Unity WebGL content is not currently supported on mobile devices

Taken directly from Unity Docs. What they want is impossible. For the few devices that it does work, performance will most likely suffer and most functionality will not work as intended.

To answer how to save on webGL, if you implement a generic save / load solution using Application.PersistentDataPath, the data will be saved at the location /idbfs/<md5 hash of data path>. It will allow data to be persistent through multiple sessions. It might work on mobile, but, again, would highly recommend you not do this. If you want mobile game, make a mobile game. The other option is to not use Unity.

Solution 2:[2]

It would be beneficial to covert the webGL game into an app as playing on a mobile browser is slow and users may receive input lag.

Solution 3:[3]

You can call javascript functions from C#, in order to use the local storage for example. You have to add them (the scripts) in a "Plugins" folder with .jslib extension. More details here: Unity Documentation. I have not tested this on mobile devices but I see no reason why this should not work.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TEEBQNE
Solution 2 aafr
Solution 3 Florin Florea