using UnityEngine; namespace Fungus { /// /// Manages the Save History (a list of Save Points). /// 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; } #region Public members /// /// Returns the number of Save Points in the Save History. /// public virtual int NumSavePoints { get { return saveHistory.NumSavePoints; } } /// /// Returns the current number of rewound Save Points in the Save History. /// public virtual int NumRewoundSavePoints { get { return saveHistory.NumRewoundSavePoints; } } /// /// Writes the Save History to persistent storage. /// public virtual void Save(string saveDataKey) { WriteSaveHistory(saveDataKey); SaveManagerSignals.DoGameSaved(saveDataKey); } /// /// Loads the Save History from persistent storage. /// public void Load(string saveDataKey) { if (ReadSaveHistory(saveDataKey)) { saveHistory.ClearRewoundSavePoints(); saveHistory.LoadLatestSavePoint(); SaveManagerSignals.DoGameLoaded(saveDataKey); } } /// /// Deletes a previously stored Save History from persistent storage. /// public void Delete(string saveDataKey) { PlayerPrefs.DeleteKey(saveDataKey); PlayerPrefs.Save(); } /// /// Returns true if save data has previously been stored using this key. /// public bool SaveDataExists(string saveDataKey) { return PlayerPrefs.HasKey(saveDataKey); } /// /// Creates a new Save Point using a key and description, and adds it to the Save History. /// public virtual void AddSavePoint(string savePointKey, string savePointDescription) { saveHistory.AddSavePoint(savePointKey, savePointDescription); SaveManagerSignals.DoSavePointAdded(savePointKey, savePointDescription); } /// /// Rewinds to the previous Save Point in the Save History and loads that Save Point. /// public virtual void Rewind() { if (saveHistory.NumSavePoints > 0) { saveHistory.Rewind(); saveHistory.LoadLatestSavePoint(); } } /// /// Fast forwards to the next rewound Save Point in the Save History and loads that Save Point. /// public virtual void FastForward() { if (saveHistory.NumRewoundSavePoints > 0) { saveHistory.FastForward(); saveHistory.LoadLatestSavePoint(); } } /// /// Deletes all Save Points in the Save History. /// public virtual void ClearHistory() { saveHistory.Clear(); } #endregion } }