In case you are curious, here are my scripts...
InventorySaver :
Code: Select all
using System;
using UnityEngine;
using PixelCrushers;
using GameDevTV.Inventories;
using System.Collections.Generic;
namespace PixelCrushers
{
public class ShumaInventorySaver : Saver
{
[System.Serializable]
public struct InventorySlotRecord
{
public string itemID;
public int number;
}
[System.Serializable]
public class Data
{
public InventorySlotRecord[] slots;
}
public override string RecordData()
{
var data = new Data();
Inventory inventory = FindObjectOfType<Inventory>();
int size = inventory.inventorySize;
var slotStrings = new InventorySlotRecord[size];
data.slots = new InventorySlotRecord[size];
var invSlots = inventory.slots;
for (int i = 0; i < size; i++)
{
if (invSlots[i].item != null)
{
slotStrings[i].itemID = invSlots[i].item.GetItemID();
slotStrings[i].number = invSlots[i].number;
}
}
data.slots = slotStrings;
return SaveSystem.Serialize(data);
}
public override void ApplyData(string s)
{
if (string.IsNullOrEmpty(s)) return; // No data to apply.
Data data = SaveSystem.Deserialize<Data>(s);
if (data == null) return; // Serialized string isn't valid.
int size = data.slots.Length;
var slotStrings = data.slots;
Inventory inventory = FindObjectOfType<Inventory>();
inventory.CreateNewInventory(size);
for (int i = 0; i < size; i++)
{
var slot = inventory.slots[i];
slot.item = InventoryItem.GetFromID(slotStrings[i].itemID);
slot.number = slotStrings[i].number;
inventory.slots[i] = slot;
}
inventory.UpdateInventory();
}
}
}
/**/
Code: Select all
public void UpdateInventory()
{
if (inventoryUpdated != null)
{
inventoryUpdated();
}
}
public void CreateNewInventory(int size)
{
inventorySize = size;
slots = new List<InventorySlot>(new InventorySlot[size]);
}