emorphus
emorphus

Reputation: 560

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?

Upvotes: 3

Views: 13079

Answers (5)

fox 545
fox 545

Reputation: 1

Faced with a similar problem. I found a strange solution. If you save the data and then reload the scene, the progress is saved. But if you save the progress and don't reload the scene and then reload the page in the browser, then the progress won't be saved. It's all about saving progress locally on the device. However, if you save the progress to the server, then there is no such strange behavior

Upvotes: 0

coder _F
coder _F

Reputation: 9

Man save yourself the pain and just use playfab

Upvotes: 0

Florin Florea
Florin Florea

Reputation: 59

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.

Upvotes: 0

TEEBQNE
TEEBQNE

Reputation: 6276

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.

Upvotes: 5

codenaem
codenaem

Reputation: 59

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.

Upvotes: 1

Related Questions