Browse Source

Exposed Save Data key to support multiple game save files in the same project

master
Christopher 8 years ago
parent
commit
7294c7810e
  1. 13
      Assets/Fungus/Scripts/Components/SaveManager.cs
  2. 19
      Assets/Fungus/Scripts/Components/SaveMenu.cs
  3. 5
      Assets/Fungus/Scripts/Utils/FungusConstants.cs
  4. 2
      Assets/FungusExamples/SaveGame/SaveGame.unity

13
Assets/Fungus/Scripts/Components/SaveManager.cs

@ -169,11 +169,6 @@ namespace Fungus
#region Public members #region Public members
/// <summary>
/// The default key used for storing save game data in PlayerPrefs.
/// </summary>
public const string DefaultSaveDataKey = "save_data";
/// <summary> /// <summary>
/// The scene that should be loaded when restarting a game. /// The scene that should be loaded when restarting a game.
/// </summary> /// </summary>
@ -192,7 +187,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Writes the Save History to persistent storage. /// Writes the Save History to persistent storage.
/// </summary> /// </summary>
public virtual void Save(string saveDataKey = DefaultSaveDataKey) public virtual void Save(string saveDataKey)
{ {
WriteSaveHistory(saveDataKey); WriteSaveHistory(saveDataKey);
} }
@ -200,7 +195,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Loads the Save History from persistent storage and loads the latest Save Point. /// Loads the Save History from persistent storage and loads the latest Save Point.
/// </summary> /// </summary>
public void Load(string saveDataKey = DefaultSaveDataKey) public void Load(string saveDataKey)
{ {
// Set a load action to be executed on next update // Set a load action to be executed on next update
var key = saveDataKey; var key = saveDataKey;
@ -210,7 +205,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Deletes a previously stored Save History from persistent storage. /// Deletes a previously stored Save History from persistent storage.
/// </summary> /// </summary>
public void Delete(string saveDataKey = DefaultSaveDataKey) public void Delete(string saveDataKey)
{ {
PlayerPrefs.DeleteKey(saveDataKey); PlayerPrefs.DeleteKey(saveDataKey);
PlayerPrefs.Save(); PlayerPrefs.Save();
@ -219,7 +214,7 @@ namespace Fungus
/// <summary> /// <summary>
/// Returns true if save data has previously been stored using this key. /// Returns true if save data has previously been stored using this key.
/// </summary> /// </summary>
public bool SaveDataExists(string saveDataKey = DefaultSaveDataKey) public bool SaveDataExists(string saveDataKey)
{ {
return PlayerPrefs.HasKey(saveDataKey); return PlayerPrefs.HasKey(saveDataKey);
} }

19
Assets/Fungus/Scripts/Components/SaveMenu.cs

@ -14,6 +14,9 @@ namespace Fungus
/// </summary> /// </summary>
public class SaveMenu : MonoBehaviour public class SaveMenu : MonoBehaviour
{ {
[Tooltip("String key used when storing save game data in PlayerPrefs.")]
[SerializeField] protected string saveDataKey = FungusConstants.DefaultSaveDataKey;
[Tooltip("Automatically load the most recently saved game on startup")] [Tooltip("Automatically load the most recently saved game on startup")]
[SerializeField] protected bool loadOnStart = true; [SerializeField] protected bool loadOnStart = true;
@ -88,9 +91,9 @@ namespace Fungus
if (loadOnStart) if (loadOnStart)
{ {
if (saveManager.SaveDataExists()) if (saveManager.SaveDataExists(saveDataKey))
{ {
saveManager.Load(); saveManager.Load(saveDataKey);
} }
} }
} }
@ -118,7 +121,7 @@ namespace Fungus
} }
if (loadButton != null) if (loadButton != null)
{ {
loadButton.interactable = saveManager.SaveDataExists(); loadButton.interactable = saveManager.SaveDataExists(saveDataKey);
} }
} }
@ -158,7 +161,7 @@ namespace Fungus
if (autoSave && if (autoSave &&
saveManager.NumSavePoints > 0) saveManager.NumSavePoints > 0)
{ {
saveManager.Save(); saveManager.Save(saveDataKey);
} }
} }
@ -216,7 +219,7 @@ namespace Fungus
if (saveManager.NumSavePoints > 0) if (saveManager.NumSavePoints > 0)
{ {
PlayClickSound(); PlayClickSound();
saveManager.Save(); saveManager.Save(saveDataKey);
} }
} }
@ -227,10 +230,10 @@ namespace Fungus
{ {
var saveManager = FungusManager.Instance.SaveManager; var saveManager = FungusManager.Instance.SaveManager;
if (saveManager.SaveDataExists()) if (saveManager.SaveDataExists(saveDataKey))
{ {
PlayClickSound(); PlayClickSound();
saveManager.Load(); saveManager.Load(saveDataKey);
} }
} }
@ -281,7 +284,7 @@ namespace Fungus
saveManager.ClearHistory(); saveManager.ClearHistory();
if (restartDeletesSave) if (restartDeletesSave)
{ {
saveManager.Delete(); saveManager.Delete(saveDataKey);
} }
SceneManager.LoadScene(saveManager.StartScene); SceneManager.LoadScene(saveManager.StartScene);

5
Assets/Fungus/Scripts/Utils/FungusConstants.cs

@ -42,6 +42,11 @@ namespace Fungus
/// </summary> /// </summary>
public static Color DefaultProcessBlockTint = new Color(1.0f, 0.882f, 0.0f, 1.0f); public static Color DefaultProcessBlockTint = new Color(1.0f, 0.882f, 0.0f, 1.0f);
/// <summary>
/// The default key used for storing save game data in PlayerPrefs.
/// </summary>
public const string DefaultSaveDataKey = "save_data";
#endregion #endregion
} }
} }

2
Assets/FungusExamples/SaveGame/SaveGame.unity

@ -2034,7 +2034,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: abe25ebf9ddc8416ead6f30d4671fdbf, type: 3} m_Script: {fileID: 11500000, guid: abe25ebf9ddc8416ead6f30d4671fdbf, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
saveDataKey: save_example_data
loadOnStart: 1 loadOnStart: 1
autoSave: 0
restartDeletesSave: 0 restartDeletesSave: 0
saveMenuGroup: {fileID: 178630601} saveMenuGroup: {fileID: 178630601}
saveMenuButton: {fileID: 1058705593} saveMenuButton: {fileID: 1058705593}

Loading…
Cancel
Save