Basic inventory system
This commit is contained in:
parent
0bedb71a40
commit
770c4f751b
8 changed files with 132 additions and 0 deletions
BIN
MoCha/Assets/Prefabs/ItemSlot.prefab
Normal file
BIN
MoCha/Assets/Prefabs/ItemSlot.prefab
Normal file
Binary file not shown.
10
MoCha/Assets/Prefabs/ItemSlot.prefab.meta
Normal file
10
MoCha/Assets/Prefabs/ItemSlot.prefab.meta
Normal file
|
@ -0,0 +1,10 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ffb3a2b5c7f8d144f83ef1f1166b178f
|
||||
timeCreated: 1524162403
|
||||
licenseType: Free
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 100100000
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
37
MoCha/Assets/Scripts/Inventory.cs
Normal file
37
MoCha/Assets/Scripts/Inventory.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using UnityEngine;
|
||||
using System.Collections;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
||||
public class Inventory : MonoBehaviour
|
||||
{
|
||||
public Image[] itemImages = new Image[numItemSlots];
|
||||
public Item[] items = new Item[numItemSlots];
|
||||
public const int numItemSlots = 4;
|
||||
public void AddItem(Item itemToAdd)
|
||||
{
|
||||
for (int i = 0; i < items.Length; i++)
|
||||
{
|
||||
if (items[i] == null)
|
||||
{
|
||||
items[i] = itemToAdd;
|
||||
itemImages[i].sprite = itemToAdd.sprite;
|
||||
itemImages[i].enabled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void RemoveItem (Item itemToRemove)
|
||||
{
|
||||
for (int i = 0; i < items.Length; i++)
|
||||
{
|
||||
if (items[i] == itemToRemove)
|
||||
{
|
||||
items[i] = null;
|
||||
itemImages[i].sprite = null;
|
||||
itemImages[i].enabled = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
13
MoCha/Assets/Scripts/Inventory.cs.meta
Normal file
13
MoCha/Assets/Scripts/Inventory.cs.meta
Normal file
|
@ -0,0 +1,13 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b43850ea4963ca144a2219ebc0e3b6fd
|
||||
timeCreated: 1524161696
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
39
MoCha/Assets/Scripts/InventoryEditor.cs
Normal file
39
MoCha/Assets/Scripts/InventoryEditor.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
[CustomEditor(typeof(Inventory))]
|
||||
public class InventoryEditor : Editor
|
||||
{
|
||||
private bool[] showItemSlots = new bool[Inventory.numItemSlots];
|
||||
private SerializedProperty itemImagesProperty;
|
||||
private SerializedProperty itemsProperty;
|
||||
private const string inventoryPropItemImagesName = "itemImages";
|
||||
private const string inventoryPropItemsName = "items";
|
||||
private void OnEnable ()
|
||||
{
|
||||
itemImagesProperty = serializedObject.FindProperty (inventoryPropItemImagesName);
|
||||
itemsProperty = serializedObject.FindProperty (inventoryPropItemsName);
|
||||
}
|
||||
public override void OnInspectorGUI ()
|
||||
{
|
||||
serializedObject.Update ();
|
||||
for (int i = 0; i < Inventory.numItemSlots; i++)
|
||||
{
|
||||
ItemSlotGUI (i);
|
||||
}
|
||||
serializedObject.ApplyModifiedProperties ();
|
||||
}
|
||||
private void ItemSlotGUI (int index)
|
||||
{
|
||||
EditorGUILayout.BeginVertical (GUI.skin.box);
|
||||
EditorGUI.indentLevel++;
|
||||
|
||||
showItemSlots[index] = EditorGUILayout.Foldout (showItemSlots[index], "Item slot " + index);
|
||||
if (showItemSlots[index])
|
||||
{
|
||||
EditorGUILayout.PropertyField (itemImagesProperty.GetArrayElementAtIndex (index));
|
||||
EditorGUILayout.PropertyField (itemsProperty.GetArrayElementAtIndex (index));
|
||||
}
|
||||
EditorGUI.indentLevel--;
|
||||
EditorGUILayout.EndVertical ();
|
||||
}
|
||||
}
|
13
MoCha/Assets/Scripts/InventoryEditor.cs.meta
Normal file
13
MoCha/Assets/Scripts/InventoryEditor.cs.meta
Normal file
|
@ -0,0 +1,13 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dd2ab8002901b644bbc724fa04249955
|
||||
timeCreated: 1524163165
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
7
MoCha/Assets/Scripts/Item.cs
Normal file
7
MoCha/Assets/Scripts/Item.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
using UnityEngine;
|
||||
|
||||
[CreateAssetMenu]
|
||||
public class Item : ScriptableObject {
|
||||
|
||||
public Sprite sprite;
|
||||
}
|
13
MoCha/Assets/Scripts/Item.cs.meta
Normal file
13
MoCha/Assets/Scripts/Item.cs.meta
Normal file
|
@ -0,0 +1,13 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2b57d2e6ebe4089498537815ff9350ee
|
||||
timeCreated: 1524162707
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Reference in a new issue