浏览代码

Keep a backup of previous save. Added Continue from save to Menu.

/UI
Bronson Zgeb 4 年前
当前提交
5c50ddcf
共有 4 个文件被更改,包括 292 次插入11 次删除
  1. 228
      UOP1_Project/Assets/Scenes/Menus/MainMenu.unity
  2. 22
      UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs
  3. 13
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
  4. 40
      UOP1_Project/Assets/Scripts/SceneManagement/StartGame.cs

228
UOP1_Project/Assets/Scenes/Menus/MainMenu.unity


m_Children:
- {fileID: 1806157293}
m_Father: {fileID: 2140469183}
m_RootOrder: 3
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 403475996}
m_CullTransparentMesh: 0
--- !u!1 &589059405
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 589059406}
- component: {fileID: 589059408}
- component: {fileID: 589059407}
m_Layer: 5
m_Name: Text
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &589059406
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 589059405}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 763294101}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &589059407
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 589059405}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_FontData:
m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0}
m_FontSize: 40
m_FontStyle: 0
m_BestFit: 0
m_MinSize: 4
m_MaxSize: 60
m_Alignment: 4
m_AlignByGeometry: 0
m_RichText: 1
m_HorizontalOverflow: 0
m_VerticalOverflow: 0
m_LineSpacing: 1
m_Text: Reset Save Data
--- !u!222 &589059408
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 589059405}
m_CullTransparentMesh: 0
--- !u!1 &622178817
GameObject:
m_ObjectHideFlags: 0

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2140469183}
m_RootOrder: 4
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 1}
m_AnchorMax: {x: 1, y: 1}

m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 684748716}
m_CullTransparentMesh: 0
--- !u!1 &763294100
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 763294101}
- component: {fileID: 763294105}
- component: {fileID: 763294104}
- component: {fileID: 763294103}
m_Layer: 5
m_Name: Reset Save Game
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &763294101
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 763294100}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 589059406}
m_Father: {fileID: 2140469183}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 280.6552, y: -81}
m_SizeDelta: {x: 240.65524, y: 116.64508}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &763294103
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 763294100}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Navigation:
m_Mode: 3
m_SelectOnUp: {fileID: 0}
m_SelectOnDown: {fileID: 0}
m_SelectOnLeft: {fileID: 0}
m_SelectOnRight: {fileID: 0}
m_Transition: 1
m_Colors:
m_NormalColor: {r: 1, g: 1, b: 1, a: 1}
m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1}
m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1}
m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608}
m_ColorMultiplier: 1
m_FadeDuration: 0.1
m_SpriteState:
m_HighlightedSprite: {fileID: 0}
m_PressedSprite: {fileID: 0}
m_SelectedSprite: {fileID: 0}
m_DisabledSprite: {fileID: 0}
m_AnimationTriggers:
m_NormalTrigger: Normal
m_HighlightedTrigger: Highlighted
m_PressedTrigger: Pressed
m_SelectedTrigger: Selected
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 763294104}
m_OnClick:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 1967191115}
m_MethodName: OnResetSaveDataPress
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
- m_Target: {fileID: 0}
m_MethodName: SetActive
m_Mode: 6
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &763294104
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 763294100}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3}
m_Name:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_Maskable: 1
m_OnCullStateChanged:
m_PersistentCalls:
m_Calls: []
m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0}
m_Type: 1
m_PreserveAspect: 0
m_FillCenter: 1
m_FillMethod: 4
m_FillAmount: 1
m_FillClockwise: 1
m_FillOrigin: 0
m_UseSpriteMesh: 0
m_PixelsPerUnitMultiplier: 1
--- !u!222 &763294105
CanvasRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 763294100}
m_CullTransparentMesh: 0
--- !u!1 &821853654
GameObject:
m_ObjectHideFlags: 0

m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 2140469183}
m_RootOrder: 5
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0}
m_AnchorMax: {x: 1, y: 0}

- {fileID: 11400000, guid: 2f386bbc670c05145834a0f9c7d55798, type: 2}
showLoadScreen: 1
saveSystem: {fileID: 11400000, guid: edc355c4a7d5028408d322b90814e19e, type: 2}
startText: {fileID: 622178819}
resetSaveDataButton: {fileID: 763294103}
--- !u!1 &2140469179
GameObject:
m_ObjectHideFlags: 0

- {fileID: 1312122616}
- {fileID: 1228641980}
- {fileID: 1967191111}
- {fileID: 763294101}
- {fileID: 403475997}
- {fileID: 684748717}
- {fileID: 821853655}

22
UOP1_Project/Assets/Scripts/SaveSystem/FileManager.cs


return false;
}
}
public static bool MoveFile(string fileName, string newFileName)
{
var fullPath = Path.Combine(Application.persistentDataPath, fileName);
var newFullPath = Path.Combine(Application.persistentDataPath, newFileName);
try
{
if (File.Exists(newFullPath))
{
File.Delete(newFullPath);
}
File.Move(fullPath, newFullPath);
}
catch (Exception e)
{
Debug.LogError($"Failed to move file from {fullPath} to {newFullPath} with exception {e}");
return false;
}
return true;
}
}

13
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs


[SerializeField] private Inventory _playerInventory;
public string saveFilename = "save.chop";
public string backupSaveFilename = "save.chop.bak";
public Save saveData = new Save();
void OnEnable()

SaveDataToDisk();
}
public void LoadSaveDataFromDisk()
public bool LoadSaveDataFromDisk()
return true;
return false;
}
public IEnumerator LoadSavedInventory()

saveData._itemStacks.Add(new SerializedItemStack(itemStack.Item.Guid, itemStack.Amount));
}
if (FileManager.WriteToFile(saveFilename, saveData.ToJson()))
if (FileManager.MoveFile(saveFilename, backupSaveFilename))
Debug.Log("Save successful");
if (FileManager.WriteToFile(saveFilename, saveData.ToJson()))
{
Debug.Log("Save successful");
}
}
}
}

40
UOP1_Project/Assets/Scripts/SceneManagement/StartGame.cs


using System.Collections;
using System;
using System.Collections;
using UnityEngine.UI;
/// <summary>
/// This class contains the function to call when play button is pressed

public bool showLoadScreen;
public SaveSystem saveSystem;
public Text startText;
public Button resetSaveDataButton;
private bool _hasSaveData;
private void Start()
{
_hasSaveData = saveSystem.LoadSaveDataFromDisk();
if (_hasSaveData)
{
startText.text = "Continue";
resetSaveDataButton.gameObject.SetActive(true);
}
else
{
resetSaveDataButton.gameObject.SetActive(false);
}
}
onPlayButtonPress.RaiseEvent(locationsToLoad, showLoadScreen);
if (!_hasSaveData)
{
//Start new game
onPlayButtonPress.RaiseEvent(locationsToLoad, showLoadScreen);
}
else
{
//Load Game
StartCoroutine(LoadSaveGame());
}
public void OnLoadButtonPress()
public void OnResetSaveDataPress()
StartCoroutine(LoadSaveGame());
_hasSaveData = false;
startText.text = "Play";
resetSaveDataButton.gameObject.SetActive(false);
saveSystem.LoadSaveDataFromDisk();
yield return StartCoroutine(saveSystem.LoadSavedInventory());
var locationGuid = saveSystem.saveData._locationId;

正在加载...
取消
保存