title: How do you save game data when playing a Unity webGl game on a mobile browser?
body: <p>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.</p>
  <pre><code>using System;
  using System.IO;
  using System.Runtime.InteropServices;
  using System.Runtime.Serialization.Formatters.Binary;
  using UnityEngine;
  
  public class DataAccess
  {
      [DllImport ( &quot;__Internal&quot; )]
      private static extern void SyncFiles ( );
  
      [DllImport ( &quot;__Internal&quot; )]
      private static extern void WindowAlert ( string message );
  
      public static void Save ( Stats stats )
      {
          string dataPath = string.Format ( &quot;{0}/Stats.dat&quot;, 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 ( &quot;Failed to Save: &quot; + e.Message );
          }
      }
  
      public static Stats Load (Stats stats )
      {
          //Stats stats = null;
          string dataPath = string.Format ( &quot;{0}/Stats.dat&quot;, 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 ( &quot;Failed to Load: &quot; + e.Message );
          }
  
          return stats;
      }
  
      private static void PlatformSafeMessage ( string message )
      {
          if ( Application.platform == RuntimePlatform.WebGLPlayer )
          {
              WindowAlert ( message );
          }
          else
          {
              Debug.Log ( message );
          }
      }
  }
  </code></pre>
  <p>Does anyone know whether this can be done, if so how?</p>