浏览代码

Added ItemStack Serialization on Save

/UI
Bronson Zgeb 3 年前
当前提交
9b794d57
共有 6 个文件被更改,包括 37 次插入7 次删除
  1. 8
      UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset
  2. 4
      UOP1_Project/Assets/Scripts/Inventory/ScriptableObjects/Item.cs
  3. 7
      UOP1_Project/Assets/Scripts/SaveSystem/Save.cs
  4. 10
      UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs
  5. 12
      UOP1_Project/Assets/Scripts/SaveSystem/SerializedItemStack.cs
  6. 3
      UOP1_Project/Assets/Scripts/SaveSystem/SerializedItemStack.cs.meta

8
UOP1_Project/Assets/ScriptableObjects/SaveSystem/SaveSystem.asset


m_Script: {fileID: 11500000, guid: 428b35af66b1ab44c9cca9bb53864745, type: 3}
m_Name: SaveSystem
m_EditorClassIdentifier:
locationDatabase: {fileID: 11400000, guid: 9abcccc3f16b8c54e984de463d147899, type: 2}
_onSceneReady: {fileID: 11400000, guid: b729e40fc41dd8b4ea7aaf5c857f7186, type: 2}
_playerInventory: {fileID: 11400000, guid: 59c84467f7726dc4587a8373b0936f03, type: 2}
_locationId:
_locationId: d102ba8fe3b291249aeb9de4b95c1904
_itemStacks:
- Guid: 0a7c779a15b2c4143ae2068725b8ece9
Amount: 1

4
UOP1_Project/Assets/Scripts/Inventory/ScriptableObjects/Item.cs


using System.Collections.Generic;
using System.Collections.Generic;
public class Item : ScriptableObject
public class Item : SerializableScriptableObject
{
[Tooltip("The name of the item")]
[SerializeField] private LocalizedString _name = default;

7
UOP1_Project/Assets/Scripts/SaveSystem/Save.cs


using UnityEngine;
using System;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
[Serializable]
public class Save
{
// This is test data, written according to TestScript.cs class

public string _locationId;
public List<SerializedItemStack> _itemStacks = new List<SerializedItemStack>();
public string ToJson()
{

10
UOP1_Project/Assets/Scripts/SaveSystem/SaveSystem.cs


public class SaveSystem : ScriptableObject
{
[SerializeField] private LoadEventChannelSO _loadLocation = default;
[SerializeField] private Inventory _playerInventory;
public string saveFilename = "save.chop";
public Save saveData = new Save();

{
saveData._locationId = locationSo.SerializableGuid;
}
SaveGame();
}
public void LoadGame()

}
}
saveData._itemStacks.Clear();
foreach (var itemStack in _playerInventory.Items)
{
saveData._itemStacks.Add(new SerializedItemStack(itemStack.Item.SerializableGuid, itemStack.Amount));
}
if (FileManager.WriteToFile(saveFilename, saveData.ToJson()))
{
Debug.Log("Save successful");

12
UOP1_Project/Assets/Scripts/SaveSystem/SerializedItemStack.cs


[System.Serializable]
public class SerializedItemStack
{
public string itemGuid;
public int amount;
public SerializedItemStack(string itemGuid, int amount)
{
this.itemGuid = itemGuid;
this.amount = amount;
}
}

3
UOP1_Project/Assets/Scripts/SaveSystem/SerializedItemStack.cs.meta


fileFormatVersion: 2
guid: f9d1e93be0b44303b4235a4be6430925
timeCreated: 1613596846
正在加载...
取消
保存