Browse Source

Merge pull request #635 from stevehalliwell/SaveManagerStorage

SaveManager use persistentStore
master
Steve Halliwell 7 years ago committed by GitHub
parent
commit
76985c7f02
  1. 49
      Assets/Fungus/Scripts/Components/SaveManager.cs

49
Assets/Fungus/Scripts/Components/SaveManager.cs

@ -10,14 +10,34 @@ namespace Fungus
{ {
/// <summary> /// <summary>
/// Manages the Save History (a list of Save Points) and provides a set of operations for saving and loading games. /// Manages the Save History (a list of Save Points) and provides a set of operations for saving and loading games.
///
/// Note WebGL and Webplayer (deprecated) save using playerprefs instead of using a json file in persistent storage
/// -webgl would require additional js to force a sync of FS.syncfs
/// -webplayer does not implement system io
/// </summary> /// </summary>
public class SaveManager : MonoBehaviour public class SaveManager : MonoBehaviour
{ {
protected static SaveHistory saveHistory = new SaveHistory(); protected static SaveHistory saveHistory = new SaveHistory();
public static string STORAGE_DIRECTORY { get { return Application.persistentDataPath + "/FungusSaves/"; } }
private static string GetFullFilePath(string saveDataKey)
{
return STORAGE_DIRECTORY + saveDataKey + ".json";
}
protected virtual bool ReadSaveHistory(string saveDataKey) protected virtual bool ReadSaveHistory(string saveDataKey)
{ {
var historyData = PlayerPrefs.GetString(saveDataKey); var historyData = string.Empty;
#if UNITY_WEBPLAYER || UNITY_WEBGL
historyData = PlayerPrefs.GetString(saveDataKey);
#else
var fullFilePath = GetFullFilePath(saveDataKey);
if (System.IO.File.Exists(fullFilePath))
{
historyData = System.IO.File.ReadAllText(fullFilePath);
}
#endif//UNITY_WEBPLAYER
if (!string.IsNullOrEmpty(historyData)) if (!string.IsNullOrEmpty(historyData))
{ {
var tempSaveHistory = JsonUtility.FromJson<SaveHistory>(historyData); var tempSaveHistory = JsonUtility.FromJson<SaveHistory>(historyData);
@ -36,8 +56,18 @@ namespace Fungus
var historyData = JsonUtility.ToJson(saveHistory, true); var historyData = JsonUtility.ToJson(saveHistory, true);
if (!string.IsNullOrEmpty(historyData)) if (!string.IsNullOrEmpty(historyData))
{ {
#if UNITY_WEBPLAYER || UNITY_WEBGL
PlayerPrefs.SetString(saveDataKey, historyData); PlayerPrefs.SetString(saveDataKey, historyData);
PlayerPrefs.Save(); PlayerPrefs.Save();
#else
var fileLoc = GetFullFilePath(saveDataKey);
//make sure the dir exists
System.IO.FileInfo file = new System.IO.FileInfo(fileLoc);
file.Directory.Create();
System.IO.File.WriteAllText(fileLoc, historyData);
#endif//UNITY_WEBPLAYER
return true; return true;
} }
@ -57,7 +87,7 @@ namespace Fungus
SavePointLoaded.NotifyEventHandlers(savePointKey); SavePointLoaded.NotifyEventHandlers(savePointKey);
// Execute any block containing a SavePoint command matching the save key, with Resume On Load enabled // Execute any block containing a SavePoint command matching the save key, with Resume On Load enabled
var savePoints = Object.FindObjectsOfType<SavePoint>(); var savePoints = UnityEngine.Object.FindObjectsOfType<SavePoint>();
for (int i = 0; i < savePoints.Length; i++) for (int i = 0; i < savePoints.Length; i++)
{ {
var savePoint = savePoints[i]; var savePoint = savePoints[i];
@ -83,7 +113,7 @@ namespace Fungus
// Each scene should have one Save Point with the IsStartPoint property enabled. // Each scene should have one Save Point with the IsStartPoint property enabled.
// We automatically start execution from this command whenever the scene starts 'normally' (i.e. first play, restart or scene load via the Load Scene command or SceneManager.LoadScene). // We automatically start execution from this command whenever the scene starts 'normally' (i.e. first play, restart or scene load via the Load Scene command or SceneManager.LoadScene).
var savePoints = Object.FindObjectsOfType<SavePoint>(); var savePoints = UnityEngine.Object.FindObjectsOfType<SavePoint>();
for (int i = 0; i < savePoints.Length; i++) for (int i = 0; i < savePoints.Length; i++)
{ {
var savePoint = savePoints[i]; var savePoint = savePoints[i];
@ -207,8 +237,16 @@ namespace Fungus
/// </summary> /// </summary>
public void Delete(string saveDataKey) public void Delete(string saveDataKey)
{ {
#if UNITY_WEBPLAYER || UNITY_WEBGL
PlayerPrefs.DeleteKey(saveDataKey); PlayerPrefs.DeleteKey(saveDataKey);
PlayerPrefs.Save(); PlayerPrefs.Save();
#else
var fullFilePath = GetFullFilePath(saveDataKey);
if (System.IO.File.Exists(fullFilePath))
{
System.IO.File.Delete(fullFilePath);
}
#endif//UNITY_WEBPLAYER
} }
/// <summary> /// <summary>
@ -216,7 +254,12 @@ namespace Fungus
/// </summary> /// </summary>
public bool SaveDataExists(string saveDataKey) public bool SaveDataExists(string saveDataKey)
{ {
#if UNITY_WEBPLAYER || UNITY_WEBGL
return PlayerPrefs.HasKey(saveDataKey); return PlayerPrefs.HasKey(saveDataKey);
#else
var fullFilePath = GetFullFilePath(saveDataKey);
return System.IO.File.Exists(fullFilePath);
#endif//UNITY_WEBPLAYER
} }
/// <summary> /// <summary>

Loading…
Cancel
Save