You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.3 KiB
65 lines
2.3 KiB
using UnityEngine; |
|
using System.Collections.Generic; |
|
|
|
namespace Fungus |
|
{ |
|
/// <summary> |
|
/// 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. |
|
/// </summary> |
|
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<Flowchart> flowcharts = new List<Flowchart>(); |
|
|
|
#region Public methods |
|
|
|
/// <summary> |
|
/// Encodes the objects to be saved as a list of SaveDataItems. |
|
/// </summary |
|
public virtual void Encode(List<SaveDataItem> 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); |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Decodes the loaded list of SaveDataItems to restore the saved game state. |
|
/// </summary> |
|
public virtual void Decode(List<SaveDataItem> 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<FlowchartData>(saveDataItem.Data); |
|
if (flowchartData == null) |
|
{ |
|
Debug.LogError("Failed to decode Flowchart save data item"); |
|
return; |
|
} |
|
|
|
FlowchartData.Decode(flowchartData); |
|
} |
|
} |
|
} |
|
|
|
#endregion |
|
} |
|
} |