I don't know how to save data of an online game but I have already implemented saving in a desktop application. How would it be possible to save modified data in my Unity game played on a browser such that it could be loaded to restore the original state? I already have a saving system script:-
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace GameDevTV.Saving
{
/// <summary>
/// This component provides the interface to the saving system. It provides
/// methods to save and restore a scene.
///
/// This component should be created once and shared between all subsequent scenes.
/// </summary>
public class SavingSystem : MonoBehaviour
{
/// <summary>
/// Will load the last scene that was saved and restore the state. This
/// must be run as a coroutine.
/// </summary>
/// <param name="saveFile">The save file to consult for loading.</param>
private void Update()
{
}
public IEnumerator LoadLastScene(string saveFile)
{
Dictionary<string, object> state = LoadFile(saveFile);
int buildIndex = SceneManager.GetActiveScene().buildIndex;
if (state.ContainsKey("lastSceneBuildIndex"))
{
buildIndex = (int)state["lastSceneBuildIndex"];
}
yield return SceneManager.LoadSceneAsync(buildIndex);
RestoreState(state);
}
/// <summary>
/// Save the current scene to the provided save file.
/// </summary>
public void Save(string saveFile)
{
Dictionary<string, object> state = LoadFile(saveFile);
CaptureState(state);
SaveFile(saveFile, state);
}
/// <summary>
/// Delete the state in the given save file.
/// </summary>
public void Delete(string saveFile)
{
File.Delete(GetPathFromSaveFile(saveFile));
}
// PRIVATE
private void Load(string saveFile)
{
RestoreState(LoadFile(saveFile));
}
private Dictionary<string, object> LoadFile(string saveFile)
{
string path = GetPathFromSaveFile(saveFile);
if (!File.Exists(path))
{
return new Dictionary<string, object>();
}
using (FileStream stream = File.Open(path, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
return (Dictionary<string, object>)formatter.Deserialize(stream);
}
}
private void SaveFile(string saveFile, object state)
{
PlayerPrefs.Save();
string path = GetPathFromSaveFile(saveFile);
print("Saving to " + path);
using (FileStream stream = File.Open(path, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, state);
}
}
private void CaptureState(Dictionary<string, object> state)
{
foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
{
state[saveable.GetUniqueIdentifier()] = saveable.CaptureState();
}
state["lastSceneBuildIndex"] = SceneManager.GetActiveScene().buildIndex;
}
private void RestoreState(Dictionary<string, object> state)
{
foreach (SaveableEntity saveable in FindObjectsOfType<SaveableEntity>())
{
string id = saveable.GetUniqueIdentifier();
if (state.ContainsKey(id))
{
saveable.RestoreState(state[id]);
}
}
}
private string GetPathFromSaveFile(string saveFile)
{
return Path.Combine(Application.persistentDataPath, saveFile + ".sav");
}
}
}
The current saving system can save to a desktop location but im not sure if this game running online could possibly save to the desktop? Any suggestions on how to modify this code to make it save from a browser?