/// <summary> /// Load data from byte array to the inventory /// </summary> public static void LoadInventory(PlayerInventory inventory, byte[] data, out InventoryData invData) { invData = null; try { using (MemoryStream stream = new MemoryStream(data)) { BinaryFormatter formatter = new BinaryFormatter(); // deserialize InventoryData InventoryData fromFile = (InventoryData)formatter.Deserialize(stream); // load from it to PlayerInventory fromFile.SaveTo(inventory); invData = fromFile; } } catch (Exception e) { Debug.Log("DataSystem::Setting inventory to default as: " + e.Message); inventory.SetDefault(); invData = new InventoryData(); invData.LoadFrom(inventory); } }
public static void LoadInventory(PlayerInventory inventory, out InventoryData invData) { invData = null; try { string path = Application.persistentDataPath + "/" + PlayerDataFileName; if (File.Exists(path)) { // load from file using (FileStream stream = File.Open(path, FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); // reset position in file stream.Position = 0; // deserialize InventoryData InventoryData fromFile = (InventoryData)formatter.Deserialize(stream); // load from it to PlayerInventory fromFile.SaveTo(inventory); invData = fromFile; } } else { // return default inventory.SetDefault(); } } catch (Exception e) { Debug.Log("DataSystem::Setting inventory to default as: " + e.Message); inventory.SetDefault(); invData = new InventoryData(); invData.LoadFrom(inventory); } }