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

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

@ -14,6 +14,9 @@ namespace Fungus
/// </summary>
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")]
[SerializeField] protected bool loadOnStart = true;
@ -88,9 +91,9 @@ namespace Fungus
if (loadOnStart)
{
if (saveManager.SaveDataExists())
if (saveManager.SaveDataExists(saveDataKey))
{
saveManager.Load();
saveManager.Load(saveDataKey);
}
}
}
@ -118,7 +121,7 @@ namespace Fungus
}
if (loadButton != null)
{
loadButton.interactable = saveManager.SaveDataExists();
loadButton.interactable = saveManager.SaveDataExists(saveDataKey);
}
}
@ -158,7 +161,7 @@ namespace Fungus
if (autoSave &&
saveManager.NumSavePoints > 0)
{
saveManager.Save();
saveManager.Save(saveDataKey);
}
}
@ -216,7 +219,7 @@ namespace Fungus
if (saveManager.NumSavePoints > 0)
{
PlayClickSound();
saveManager.Save();
saveManager.Save(saveDataKey);
}
}
@ -227,10 +230,10 @@ namespace Fungus
{
var saveManager = FungusManager.Instance.SaveManager;
if (saveManager.SaveDataExists())
if (saveManager.SaveDataExists(saveDataKey))
{
PlayClickSound();
saveManager.Load();
saveManager.Load(saveDataKey);
}
}
@ -281,7 +284,7 @@ namespace Fungus
saveManager.ClearHistory();
if (restartDeletesSave)
{
saveManager.Delete();
saveManager.Delete(saveDataKey);
}
SceneManager.LoadScene(saveManager.StartScene);

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

@ -42,6 +42,11 @@ namespace Fungus
/// </summary>
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
}
}

2
Assets/FungusExamples/SaveGame/SaveGame.unity

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

Loading…
Cancel
Save