浏览代码

Added a Load function to StartGame.

Can now load a previously saved game from disk.
/UI
Bronson Zgeb 4 年前
当前提交
bd9a16aa
共有 3 个文件被更改,包括 48 次插入6 次删除
  1. 1
      UOP1_Project/Assets/Scenes/Menus/MainMenu.unity
  2. 25
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
  3. 28
      UOP1_Project/Assets/Scripts/SceneManagement/StartGame.cs

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


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

25
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs


using UnityEngine;
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class SaveSystem : ScriptableObject
{

saveData._locationId = locationSo.Guid;
}
SaveGame();
SaveDataToDisk();
public void LoadGame()
public void LoadSaveDataFromDisk()
{
if (FileManager.LoadFromFile(saveFilename, out var json))
{

public IEnumerator LoadSavedInventory()
{
_playerInventory.Items.Clear();
foreach (var serializedItemStack in saveData._itemStacks)
{
var loadItemOperationHandle = Addressables.LoadAssetAsync<Item>(serializedItemStack.itemGuid);
yield return loadItemOperationHandle;
if (loadItemOperationHandle.Status == AsyncOperationStatus.Succeeded)
{
var itemSo = loadItemOperationHandle.Result;
_playerInventory.Add(itemSo, serializedItemStack.amount);
}
}
}
public void SaveGame()
public void SaveDataToDisk()
{
saveData._itemStacks.Clear();
foreach (var itemStack in _playerInventory.Items)

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


using UnityEngine;
using System.Collections;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public class StartGame : MonoBehaviour
{
public LoadEventChannelSO onPlayButtonPress;

public SaveSystem saveSystem;
}
public void OnLoadButtonPress()
{
StartCoroutine(LoadSaveGame());
}
public IEnumerator LoadSaveGame()
{
saveSystem.LoadSaveDataFromDisk();
yield return StartCoroutine(saveSystem.LoadSavedInventory());
var locationGuid = saveSystem.saveData._locationId;
var asyncOperationHandle = Addressables.LoadAssetAsync<LocationSO>(locationGuid);
yield return asyncOperationHandle;
if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
{
var locationSo = asyncOperationHandle.Result;
onPlayButtonPress.RaiseEvent(new[] {(GameSceneSO)locationSo}, showLoadScreen);
}
}
}
正在加载...
取消
保存