Advertisement
Guest User

Inventory Object Script

a guest
Feb 21st, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5. using System.IO;
  6. using UnityEditor;
  7. using System.Runtime.Serialization;
  8.  
  9. [CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System/Inventory")]
  10. public class InventoryObject : ScriptableObject
  11. {
  12. public string savePath;
  13. public ItemDatabaseObject database;
  14. public Inventory Container;
  15.  
  16.  
  17. public void AddItem(Item _item, int _amount)
  18. {
  19. if (_item.buffs.Length > 0)
  20. {
  21. SetEmptySlot(_item, _amount);
  22. return;
  23. }
  24.  
  25. for (int i = 0; i < Container.Items.Length; i++)
  26. {
  27. if (Container.Items[i].ID == _item.Id)
  28. {
  29. Container.Items[i].AddAmount(_amount);
  30. return;
  31. }
  32. }
  33. SetEmptySlot(_item, _amount);
  34.  
  35. }
  36. public InventorySlot SetEmptySlot(Item _item, int _amount)
  37. {
  38. for (int i = 0; i < Container.Items.Length; i++)
  39. {
  40. if (Container.Items[i].ID <= -1)
  41. {
  42. Container.Items[i].UpdateSlot(_item.Id, _item, _amount);
  43. return Container.Items[i];
  44. }
  45. }
  46. //set up functionality for full inventory
  47. return null;
  48. }
  49.  
  50. public void MoveItem(InventorySlot item1, InventorySlot item2)
  51. {
  52. InventorySlot temp = new InventorySlot(item2.ID, item2.item, item2.amount);
  53. item2.UpdateSlot(item1.ID, item1.item, item1.amount);
  54. item1.UpdateSlot(temp.ID, temp.item, temp.amount);
  55. }
  56.  
  57. public void RemoveItem(Item _item, int _amount)
  58. {
  59. for (int i = 0; i < Container.Items.Length; i++)
  60. {
  61. if (Container.Items[i].item == _item)
  62. {
  63. if (Container.Items[i].amount >= 1)
  64. {
  65. //Container.Items[i].UpdateSlot(-1, null, 0);
  66. Container.Items[i].RemoveAmount(_amount);
  67. }
  68. if (Container.Items[i].amount == 0)
  69. {
  70. Container.Items[i].UpdateSlot(-1, null, 0);
  71.  
  72. }
  73. }
  74. }
  75. }
  76.  
  77. [ContextMenu("Save")]
  78. public void Save()
  79. {
  80. //string saveData = JsonUtility.ToJson(this, true);
  81. //BinaryFormatter bf = new BinaryFormatter();
  82. //FileStream file = File.Create(string.Concat(Application.persistentDataPath, savePath));
  83. //bf.Serialize(file, saveData);
  84. //file.Close();
  85.  
  86. IFormatter formatter = new BinaryFormatter();
  87. Stream stream = new FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Create, FileAccess.Write);
  88. formatter.Serialize(stream, Container);
  89. stream.Close();
  90. }
  91. [ContextMenu("Load")]
  92. public void Load()
  93. {
  94. if (File.Exists(string.Concat(Application.persistentDataPath, savePath)))
  95. {
  96. //BinaryFormatter bf = new BinaryFormatter();
  97. //FileStream file = File.Open(string.Concat(Application.persistentDataPath, savePath), FileMode.Open);
  98. //JsonUtility.FromJsonOverwrite(bf.Deserialize(file).ToString(), this);
  99. //file.Close();
  100.  
  101. IFormatter formatter = new BinaryFormatter();
  102. Stream stream = new FileStream(string.Concat(Application.persistentDataPath, savePath), FileMode.Open, FileAccess.Read);
  103. Inventory newContainer = (Inventory)formatter.Deserialize(stream);
  104. for (int i = 0; i < Container.Items.Length; i++)
  105. {
  106. Container.Items[i].UpdateSlot(newContainer.Items[i].ID, newContainer.Items[i].item, newContainer.Items[i].amount);
  107. }
  108. stream.Close();
  109. }
  110. }
  111. [ContextMenu("Clear")]
  112. public void Clear()
  113. {
  114. Container = new Inventory();
  115. }
  116. }
  117. [System.Serializable]
  118. public class Inventory
  119. {
  120. public InventorySlot[] Items = new InventorySlot[50];
  121. }
  122. [System.Serializable]
  123. public class InventorySlot
  124. {
  125. public int ID = -1;
  126. public Item item;
  127. public int amount;
  128.  
  129. public InventorySlot()
  130. {
  131. ID = -1;
  132. item = null;
  133. amount = 0;
  134. }
  135. public InventorySlot(int _id, Item _item, int _amount)
  136. {
  137. ID = _id;
  138. item = _item;
  139. amount = _amount;
  140. }
  141. public void UpdateSlot(int _id, Item _item, int _amount)
  142. {
  143. ID = _id;
  144. item = _item;
  145. amount = _amount;
  146. }
  147. public void AddAmount(int value)
  148. {
  149. amount += value;
  150.  
  151. }
  152. //Added this meself
  153. public void RemoveAmount(int value)
  154. {
  155. amount -= value;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement