Skip to content

Instantly share code, notes, and snippets.

@hasanbayatme
Last active December 6, 2019 00:27
Show Gist options
  • Save hasanbayatme/45e07eaec2533c55ceecdf2e872bc470 to your computer and use it in GitHub Desktop.
Save hasanbayatme/45e07eaec2533c55ceecdf2e872bc470 to your computer and use it in GitHub Desktop.
Learn how to make a inventory save and load using Save Game Pro.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SaveGamePro;
public class Inventory : MonoBehaviour {
public struct Item {
public string name;
public int amount;
}
protected Dictionary<string, Item> m_Items;
void Awake () {
m_Items = new Dictionary<string, Item> ();
Load ();
}
void OnApplicationQuit () {
Save ();
}
public void Pick (GameObject item) {
if (m_Items.ContainsKey (item.name)) {
m_Items[item.name].amount++;
} else {
Item value = new Item ();
value.name = item.name;
value.amount = 1;
m_Items.Add (item.name, value);
}
}
public void Save () {
SaveGame.Save<Item> (m_Items, "inventory-items");
}
public void Load () {
if (SaveGame.Exists ("inventory-items")) {
m_Items = SaveGame.LoadDictionary ("inventory-items");
} else {
m_Items = new Dictionary<string, Item> ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment