using UnityEngine; using System.Collections.Generic; namespace Fungus { /// /// This component encodes and decodes a list of game objects to be saved for each Save Point. /// It knows how to encode / decode concrete game classes like Flowchart and FlowchartData. /// To extend the save system to handle other data types, just modify or subclass this component. /// public class SaveData : MonoBehaviour { protected const string FlowchartDataKey = "FlowchartData"; [Tooltip("A list of Flowchart objects whose variables will be encoded in the save data. Boolean, Integer, Float and String variables are supported.")] [SerializeField] protected List flowcharts = new List(); #region Public methods /// /// Encodes the objects to be saved as a list of SaveDataItems. /// saveDataItems) { for (int i = 0; i < flowcharts.Count; i++) { var flowchart = flowcharts[i]; var flowchartData = FlowchartData.Encode(flowchart); var saveDataItem = SaveDataItem.Create(FlowchartDataKey, JsonUtility.ToJson(flowchartData)); saveDataItems.Add(saveDataItem); } } /// /// Decodes the loaded list of SaveDataItems to restore the saved game state. /// public virtual void Decode(List saveDataItems) { for (int i = 0; i < saveDataItems.Count; i++) { var saveDataItem = saveDataItems[i]; if (saveDataItem == null) { continue; } if (saveDataItem.DataType == FlowchartDataKey) { var flowchartData = JsonUtility.FromJson(saveDataItem.Data); if (flowchartData == null) { Debug.LogError("Failed to decode Flowchart save data item"); return; } FlowchartData.Decode(flowchartData); } } } #endregion } }