Browse Source

SaveGameHelper starts a new game by calling SaveGameLoaded event handler with 'new_game' key.

master
Christopher 8 years ago
parent
commit
2984418056
  1. 26
      Assets/Fungus/Scripts/Components/SaveGameHelper.cs
  2. 23
      Assets/Fungus/Scripts/Components/SaveManager.cs
  3. 1818
      Assets/FungusExamples/SaveGame/Menu.unity
  4. 8
      Assets/FungusExamples/SaveGame/Menu.unity.meta
  5. 153
      Assets/FungusExamples/SaveGame/SaveExample.unity
  6. 1205
      Assets/FungusExamples/SaveGame/SceneA.unity
  7. 8
      Assets/FungusExamples/SaveGame/SceneA.unity.meta

26
Assets/Fungus/Scripts/Components/SaveGameHelper.cs

@ -8,8 +8,14 @@ namespace Fungus
{ {
public class SaveGameHelper : MonoBehaviour public class SaveGameHelper : MonoBehaviour
{ {
const string NewGameSaveKey = "new_game";
[SerializeField] protected string startScene = ""; [SerializeField] protected string startScene = "";
[SerializeField] protected bool autoStartGame = true;
[SerializeField] protected bool restartDeletesSave = false;
[SerializeField] protected SaveGameObjects saveGameObjects = new SaveGameObjects(); [SerializeField] protected SaveGameObjects saveGameObjects = new SaveGameObjects();
protected virtual void OnEnable() protected virtual void OnEnable()
@ -22,9 +28,19 @@ namespace Fungus
SaveSignals.OnGameSave -= OnGameSave; SaveSignals.OnGameSave -= OnGameSave;
} }
protected virtual void Start()
{
var saveManager = FungusManager.Instance.SaveManager;
if (autoStartGame &&
saveManager.NumSavePoints == 0)
{
SavePointLoaded.NotifyEventHandlers(NewGameSaveKey);
}
}
protected virtual void OnGameSave(string saveKey, string saveDescription) protected virtual void OnGameSave(string saveKey, string saveDescription)
{ {
// TODO: Play sound effect
} }
#region Public methods #region Public methods
@ -52,7 +68,13 @@ namespace Fungus
public virtual void Restart() public virtual void Restart()
{ {
var saveManager = FungusManager.Instance.SaveManager; var saveManager = FungusManager.Instance.SaveManager;
saveManager.Clear(); saveManager.ClearHistory();
if (restartDeletesSave)
{
saveManager.Delete();
}
SceneManager.LoadScene(startScene); SceneManager.LoadScene(startScene);
} }

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

@ -9,7 +9,7 @@ namespace Fungus
protected static SaveHistory saveHistory = new SaveHistory(); protected static SaveHistory saveHistory = new SaveHistory();
protected virtual void ReadSaveHistory(string saveDataKey) protected virtual bool ReadSaveHistory(string saveDataKey)
{ {
var historyData = PlayerPrefs.GetString(saveDataKey); var historyData = PlayerPrefs.GetString(saveDataKey);
if (!string.IsNullOrEmpty(historyData)) if (!string.IsNullOrEmpty(historyData))
@ -18,22 +18,30 @@ namespace Fungus
if (tempSaveHistory != null) if (tempSaveHistory != null)
{ {
saveHistory = tempSaveHistory; saveHistory = tempSaveHistory;
return true;
} }
} }
return false;
} }
protected virtual void WriteSaveHistory(string saveDataKey) protected virtual bool WriteSaveHistory(string saveDataKey)
{ {
var historyData = JsonUtility.ToJson(saveHistory, true); var historyData = JsonUtility.ToJson(saveHistory, true);
if (!string.IsNullOrEmpty(historyData)) if (!string.IsNullOrEmpty(historyData))
{ {
PlayerPrefs.SetString(saveDataKey, historyData); PlayerPrefs.SetString(saveDataKey, historyData);
PlayerPrefs.Save(); PlayerPrefs.Save();
return true;
} }
return false;
} }
#region Public members #region Public members
public virtual int NumSavePoints { get { return saveHistory.NumSavePoints; } }
public virtual void Save(string saveDataKey = DefaultSaveDataKey) public virtual void Save(string saveDataKey = DefaultSaveDataKey)
{ {
WriteSaveHistory(saveDataKey); WriteSaveHistory(saveDataKey);
@ -41,13 +49,18 @@ namespace Fungus
public void Load(string saveDataKey = DefaultSaveDataKey) public void Load(string saveDataKey = DefaultSaveDataKey)
{ {
ReadSaveHistory(saveDataKey); if (ReadSaveHistory(saveDataKey))
if (saveHistory != null)
{ {
saveHistory.LoadLatestSavePoint(); saveHistory.LoadLatestSavePoint();
} }
} }
public void Delete(string saveDataKey = DefaultSaveDataKey)
{
PlayerPrefs.DeleteKey(saveDataKey);
PlayerPrefs.Save();
}
public virtual void AddSavePoint(string saveKey, string saveDescription) public virtual void AddSavePoint(string saveKey, string saveDescription)
{ {
saveHistory.AddSavePoint(saveKey, saveDescription); saveHistory.AddSavePoint(saveKey, saveDescription);
@ -62,7 +75,7 @@ namespace Fungus
} }
} }
public virtual void Clear() public virtual void ClearHistory()
{ {
saveHistory.Clear(); saveHistory.Clear();
} }

1818
Assets/FungusExamples/SaveGame/Menu.unity

File diff suppressed because it is too large Load Diff

8
Assets/FungusExamples/SaveGame/Menu.unity.meta

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: dbf229e62963146f09d60e4deab20791
timeCreated: 1478862090
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

153
Assets/FungusExamples/SaveGame/SaveExample.unity

@ -1443,7 +1443,7 @@ GameObject:
- 114: {fileID: 1665317944} - 114: {fileID: 1665317944}
- 114: {fileID: 1665317948} - 114: {fileID: 1665317948}
m_Layer: 5 m_Layer: 5
m_Name: Canvas m_Name: SaveGameCanvas
m_TagString: Untagged m_TagString: Untagged
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
@ -1476,12 +1476,12 @@ MonoBehaviour:
m_Script: {fileID: 1980459831, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3} m_Script: {fileID: 1980459831, guid: f5f67c52d1564df4a8936ccd202a3bd8, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
m_UiScaleMode: 0 m_UiScaleMode: 1
m_ReferencePixelsPerUnit: 100 m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1 m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600} m_ReferenceResolution: {x: 800, y: 600}
m_ScreenMatchMode: 0 m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0 m_MatchWidthOrHeight: 0.5
m_PhysicalUnit: 3 m_PhysicalUnit: 3
m_FallbackScreenDPI: 96 m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96 m_DefaultSpriteDPI: 96
@ -1535,7 +1535,10 @@ 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:
startScene: 536176654578616d706c65 startScene: SaveExample
autoStartGame: 1
restartDeletesSave: 0
saveGameObjects:
flowcharts: flowcharts:
- {fileID: 1889213476} - {fileID: 1889213476}
--- !u!1 &1810448172 --- !u!1 &1810448172
@ -1671,16 +1674,14 @@ GameObject:
- 114: {fileID: 1889213476} - 114: {fileID: 1889213476}
- 114: {fileID: 1889213475} - 114: {fileID: 1889213475}
- 114: {fileID: 1889213474} - 114: {fileID: 1889213474}
- 114: {fileID: 1889213471}
- 114: {fileID: 1889213481} - 114: {fileID: 1889213481}
- 114: {fileID: 1889213480} - 114: {fileID: 1889213480}
- 114: {fileID: 1889213479} - 114: {fileID: 1889213479}
- 114: {fileID: 1889213478} - 114: {fileID: 1889213478}
- 114: {fileID: 1889213473} - 114: {fileID: 1889213473}
- 114: {fileID: 1889213472} - 114: {fileID: 1889213472}
- 114: {fileID: 1889213471}
- 114: {fileID: 1889213470} - 114: {fileID: 1889213470}
- 114: {fileID: 1889213483}
- 114: {fileID: 1889213482}
m_Layer: 0 m_Layer: 0
m_Name: Flowchart m_Name: Flowchart
m_TagString: Untagged m_TagString: Untagged
@ -1699,9 +1700,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3} m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 12 itemId: 15
indentLevel: 0 indentLevel: 0
storyText: Step 3 storyText: Step 4
description: description:
character: {fileID: 0} character: {fileID: 0}
portrait: {fileID: 0} portrait: {fileID: 0}
@ -1714,38 +1715,6 @@ MonoBehaviour:
stopVoiceover: 1 stopVoiceover: 1
setSayDialog: {fileID: 0} setSayDialog: {fileID: 0}
--- !u!114 &1889213471 --- !u!114 &1889213471
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1889213469}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3d3d73aef2cfc4f51abf34ac00241f60, type: 3}
m_Name:
m_EditorClassIdentifier:
nodeRect:
serializedVersion: 2
x: 73
y: 127
width: 120
height: 40
tint: {r: 1, g: 1, b: 1, a: 1}
useCustomTint: 0
itemId: 5
blockName: Begin
description:
eventHandler: {fileID: 0}
commandList:
- {fileID: 1889213480}
- {fileID: 1889213479}
- {fileID: 1889213478}
- {fileID: 1889213473}
- {fileID: 1889213472}
- {fileID: 1889213470}
- {fileID: 1889213483}
- {fileID: 1889213482}
--- !u!114 &1889213472
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -1756,12 +1725,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3} m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 11 itemId: 14
indentLevel: 0 indentLevel: 0
saveKey: step_3 saveKey: step_4
saveDescription: saveDescription:
resumeFromHere: 1 resumeFromHere: 1
--- !u!114 &1889213473 --- !u!114 &1889213472
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -1772,9 +1741,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3} m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 10 itemId: 13
indentLevel: 0 indentLevel: 0
storyText: Step 2 storyText: Step 3
description: description:
character: {fileID: 0} character: {fileID: 0}
portrait: {fileID: 0} portrait: {fileID: 0}
@ -1786,20 +1755,35 @@ MonoBehaviour:
waitForClick: 1 waitForClick: 1
stopVoiceover: 1 stopVoiceover: 1
setSayDialog: {fileID: 0} setSayDialog: {fileID: 0}
--- !u!114 &1889213473
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1889213469}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 12
indentLevel: 0
saveKey: step_3
saveDescription:
resumeFromHere: 1
--- !u!114 &1889213474 --- !u!114 &1889213474
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 11462346, guid: 5e7fbc8d4eb714b279eeeef2262c1e1a, m_PrefabParentObject: {fileID: 0}
type: 2}
m_PrefabInternal: {fileID: 0} m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1889213469} m_GameObject: {fileID: 1889213469}
m_Enabled: 1 m_Enabled: 1
m_EditorHideFlags: 0 m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d2f6487d21a03404cb21b245f0242e79, type: 3} m_Script: {fileID: 11500000, guid: 9ca33a026786245d4b10323b87e84d5f, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
parentBlock: {fileID: 1889213475} parentBlock: {fileID: 1889213475}
waitForFrames: 1 saveKey: new_game
--- !u!114 &1889213475 --- !u!114 &1889213475
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
@ -1815,7 +1799,7 @@ MonoBehaviour:
nodeRect: nodeRect:
serializedVersion: 2 serializedVersion: 2
x: 72 x: 72
y: 64 y: 65
width: 120 width: 120
height: 40 height: 40
tint: {r: 1, g: 1, b: 1, a: 1} tint: {r: 1, g: 1, b: 1, a: 1}
@ -1826,6 +1810,13 @@ MonoBehaviour:
eventHandler: {fileID: 1889213474} eventHandler: {fileID: 1889213474}
commandList: commandList:
- {fileID: 1889213481} - {fileID: 1889213481}
- {fileID: 1889213480}
- {fileID: 1889213479}
- {fileID: 1889213478}
- {fileID: 1889213473}
- {fileID: 1889213472}
- {fileID: 1889213471}
- {fileID: 1889213470}
--- !u!114 &1889213476 --- !u!114 &1889213476
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1851,7 +1842,7 @@ MonoBehaviour:
width: 1121 width: 1121
height: 922 height: 922
selectedBlocks: selectedBlocks:
- {fileID: 1889213471} - {fileID: 1889213475}
selectedCommands: [] selectedCommands: []
variables: [] variables: []
description: description:
@ -1878,22 +1869,6 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 3 m_RootOrder: 3
--- !u!114 &1889213478 --- !u!114 &1889213478
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1889213469}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 9
indentLevel: 0
saveKey: step_2
saveDescription:
resumeFromHere: 1
--- !u!114 &1889213479
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -1904,9 +1879,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3} m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 8 itemId: 11
indentLevel: 0 indentLevel: 0
storyText: Step 1 storyText: Step 2
description: description:
character: {fileID: 0} character: {fileID: 0}
portrait: {fileID: 0} portrait: {fileID: 0}
@ -1918,7 +1893,7 @@ MonoBehaviour:
waitForClick: 1 waitForClick: 1
stopVoiceover: 1 stopVoiceover: 1
setSayDialog: {fileID: 0} setSayDialog: {fileID: 0}
--- !u!114 &1889213480 --- !u!114 &1889213479
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -1929,32 +1904,12 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3} m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 7 itemId: 10
indentLevel: 0 indentLevel: 0
saveKey: step_1 saveKey: step_2
saveDescription: saveDescription:
resumeFromHere: 1 resumeFromHere: 1
--- !u!114 &1889213481 --- !u!114 &1889213480
MonoBehaviour:
m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_GameObject: {fileID: 1889213469}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 050fb9e6e72f442b3b883da8a965bdeb, type: 3}
m_Name:
m_EditorClassIdentifier:
itemId: 6
indentLevel: 0
targetFlowchart: {fileID: 0}
targetBlock: {fileID: 1889213471}
startLabel:
stringRef: {fileID: 0}
stringVal:
startIndex: 0
callMode: 0
--- !u!114 &1889213482
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -1965,9 +1920,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3} m_Script: {fileID: 11500000, guid: ec422cd568a9c4a31ad7c36d0572b9da, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 14 itemId: 9
indentLevel: 0 indentLevel: 0
storyText: Step 4 storyText: Step 1
description: description:
character: {fileID: 0} character: {fileID: 0}
portrait: {fileID: 0} portrait: {fileID: 0}
@ -1979,7 +1934,7 @@ MonoBehaviour:
waitForClick: 1 waitForClick: 1
stopVoiceover: 1 stopVoiceover: 1
setSayDialog: {fileID: 0} setSayDialog: {fileID: 0}
--- !u!114 &1889213483 --- !u!114 &1889213481
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 2 m_ObjectHideFlags: 2
m_PrefabParentObject: {fileID: 0} m_PrefabParentObject: {fileID: 0}
@ -1990,9 +1945,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3} m_Script: {fileID: 11500000, guid: 0b115619cb83b4d6ab8047d0e9407403, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
itemId: 13 itemId: 8
indentLevel: 0 indentLevel: 0
saveKey: step_4 saveKey: step_1
saveDescription: saveDescription:
resumeFromHere: 1 resumeFromHere: 1
--- !u!1 &1922034243 --- !u!1 &1922034243

1205
Assets/FungusExamples/SaveGame/SceneA.unity

File diff suppressed because it is too large Load Diff

8
Assets/FungusExamples/SaveGame/SceneA.unity.meta

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 2365f3ac29d33498093d923ed8ef3c42
timeCreated: 1478865289
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
Loading…
Cancel
Save