// This code is part of the Fungus library (https://github.com/snozbot/fungus)
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
#if UNITY_5_3_OR_NEWER
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Events;
namespace Fungus
{
///
/// Serializable container for a Save Point's data.
/// All data is stored as strings, and the only concrete game class it depends on is the SaveData component.
///
[System.Serializable]
public class SavePointData
{
[SerializeField] protected string savePointKey;
[SerializeField] protected string savePointDescription;
[SerializeField] protected string sceneName;
[SerializeField] protected List saveDataItems = new List();
protected static SavePointData Create(string _savePointKey, string _savePointDescription, string _sceneName)
{
var savePointData = new SavePointData();
savePointData.savePointKey = _savePointKey;
savePointData.savePointDescription = _savePointDescription;
savePointData.sceneName = _sceneName;
return savePointData;
}
#region Public methods
///
/// Gets or sets the unique key for the Save Point.
///
public string SavePointKey { get { return savePointKey; } set { savePointKey = value; } }
///
/// Gets or sets the description for the Save Point.
///
public string SavePointDescription { get { return savePointDescription; } set { savePointDescription = value; } }
///
/// Gets or sets the scene name associated with the Save Point.
///
public string SceneName { get { return sceneName; } set { sceneName = value; } }
///
/// Gets the list of save data items.
///
/// The save data items.
public List SaveDataItems { get { return saveDataItems; } }
///
/// Encodes a new Save Point to data and converts it to JSON text format.
///
public static string Encode(string _savePointKey, string _savePointDescription, string _sceneName)
{
var savePointData = Create(_savePointKey, _savePointDescription, _sceneName);
// Look for a SaveData component in the scene to populate the save data items.
var saveData = GameObject.FindObjectOfType();
if (saveData != null)
{
saveData.Encode(savePointData.SaveDataItems);
}
return JsonUtility.ToJson(savePointData, true);
}
///
/// Decodes a Save Point from JSON text format and loads it.
///
public static void Decode(string saveDataJSON)
{
var savePointData = JsonUtility.FromJson(saveDataJSON);
UnityAction onSceneLoadedAction = null;
onSceneLoadedAction = (scene, mode) => {
// Additive scene loads and non-matching scene loads could happen if the client is using the
// SceneManager directly. We just ignore these events and hope they know what they're doing!
if (mode == LoadSceneMode.Additive ||
scene.name != savePointData.SceneName)
{
return;
}
SceneManager.sceneLoaded -= onSceneLoadedAction;
// Look for a SaveData component in the scene to process the save data items.
var saveData = GameObject.FindObjectOfType();
if (saveData != null)
{
saveData.Decode(savePointData.SaveDataItems);
}
SaveManagerSignals.DoSavePointLoaded(savePointData.savePointKey);
};
SceneManager.sceneLoaded += onSceneLoadedAction;
SceneManager.LoadScene(savePointData.SceneName);
}
#endregion
}
}
#endif