using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; namespace Fungus { /// /// Manages the Save History (a list of Save Points) and provides a set of operations for saving and loading games. /// public class SaveManager : MonoBehaviour { protected static SaveHistory saveHistory = new SaveHistory(); protected virtual bool ReadSaveHistory(string saveDataKey) { var historyData = PlayerPrefs.GetString(saveDataKey); if (!string.IsNullOrEmpty(historyData)) { var tempSaveHistory = JsonUtility.FromJson(historyData); if (tempSaveHistory != null) { saveHistory = tempSaveHistory; return true; } } return false; } protected virtual bool WriteSaveHistory(string saveDataKey) { var historyData = JsonUtility.ToJson(saveHistory, true); if (!string.IsNullOrEmpty(historyData)) { PlayerPrefs.SetString(saveDataKey, historyData); PlayerPrefs.Save(); return true; } return false; } /// /// Starts Block execution based on a Save Point Key /// The execution order is: /// 1. Save Point Loaded event handlers with a matching key. /// 2. First Save Point command (in any Block) with matching key. Execution starts at the following command. /// 3. Any label in any block with name matching the key. Execution starts at the following command. /// protected virtual void ExecuteBlocks(string savePointKey) { // Execute Save Point Loaded event handlers with matching key. SavePointLoaded.NotifyEventHandlers(savePointKey); // Execute any block containing a SavePoint command matching the save key, with Resume On Load enabled var savePoints = Object.FindObjectsOfType(); for (int i = 0; i < savePoints.Length; i++) { var savePoint = savePoints[i]; if (savePoint.ResumeOnLoad && string.Compare(savePoint.SavePointKey, savePointKey, true) == 0) { int index = savePoint.CommandIndex; var block = savePoint.ParentBlock; var flowchart = savePoint.GetFlowchart(); flowchart.ExecuteBlock(block, index + 1); // Assume there's only one SavePoint using this key break; } } // Execute any block containing a Label matching the save key var labels = Object.FindObjectsOfType