/// <summary> /// Custom editor for the inventory panel. /// </summary> public override void OnInspectorGUI() { serializedObject.Update(); EditorGUI.BeginChangeCheck(); // if there's a change in the inspector, we resize our inventory and grid, and redraw the whole thing. if (InventoryTarget.InventoryType == Inventory.InventoryTypes.Main) { Editor.DrawPropertiesExcluding(serializedObject, new string[] { "TargetChoiceInventory" }); } if (InventoryTarget.InventoryType == Inventory.InventoryTypes.Equipment) { Editor.DrawPropertiesExcluding(serializedObject, new string[] { }); } // if for some reason we don't have a target inventory, we do nothing and exit if (InventoryTarget == null) { Debug.LogWarning("inventory target is null"); return; } // if we have a content and are in debug mode, we draw the content of the Content (I know) variable in the inspector if (InventoryTarget.Content != null && InventoryTarget.DrawContentInInspector) { EditorGUILayout.Space(); EditorGUILayout.LabelField("Debug Content", EditorStyles.boldLabel); if (InventoryTarget.NumberOfFilledSlots > 0) { for (int i = 0; i < InventoryTarget.Content.Length; i++) { GUILayout.BeginHorizontal(); if (!InventoryItem.IsNull(InventoryTarget.Content[i])) { EditorGUILayout.LabelField("Content[" + i + "]", InventoryTarget.Content[i].Quantity.ToString() + " " + InventoryTarget.Content[i].ItemName); if (GUILayout.Button("Empty Slot")) { InventoryTarget.DestroyItem(i); } } else { EditorGUILayout.LabelField("Content[" + i + "]", "empty"); } GUILayout.EndHorizontal(); } } // we draw the number of slots (total, free and filled) to the inspector. EditorGUILayout.Space(); EditorGUILayout.LabelField("Free slots", InventoryTarget.NumberOfFreeSlots.ToString()); EditorGUILayout.LabelField("Filled slots", InventoryTarget.NumberOfFilledSlots.ToString()); EditorGUILayout.Space(); } // we add a button to manually empty the inventory EditorGUILayout.Space(); if (GUILayout.Button("Empty inventory")) { InventoryTarget.EmptyInventory(); } if (GUILayout.Button("Reset saved inventory")) { InventoryTarget.ResetSavedInventory(); } if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); SceneView.RepaintAll(); } // we apply our changes serializedObject.ApplyModifiedProperties(); }
/// <summary> /// Equips the item at the specified slot /// </summary> /// <param name="item">Item.</param> /// <param name="index">Index.</param> /// <param name="slot">Slot.</param> public virtual void EquipItem(InventoryItem item, int index, InventorySlot slot = null) { if (InventoryType == InventoryTypes.Main) { InventoryItem oldItem = null; if (InventoryItem.IsNull(item)) { MMInventoryEvent.Trigger(MMInventoryEventType.Error, slot, this.name, null, 0, index); return; } // if the object is not equipable, we do nothing and exit if (!item.IsEquippable) { return; } // if a target equipment inventory is not set, we do nothing and exit if (item.TargetEquipmentInventory == null) { Debug.LogWarning("InventoryEngine Warning : " + Content[index].ItemName + "'s target equipment inventory couldn't be found."); return; } // if the object can't be moved, we play an error sound and exit if (!item.CanMoveObject) { MMInventoryEvent.Trigger(MMInventoryEventType.Error, slot, this.name, null, 0, index); return; } // call the equip method of the item if (!item.Equip()) { return; } // if this is a mono slot inventory, we prepare to swap if (item.TargetEquipmentInventory.Content.Length == 1) { if (!InventoryItem.IsNull(item.TargetEquipmentInventory.Content[0])) { if ( (item.CanSwapObject) && (item.TargetEquipmentInventory.Content[0].CanMoveObject) && (item.TargetEquipmentInventory.Content[0].CanSwapObject) ) { // we store the item in the equipment inventory oldItem = item.TargetEquipmentInventory.Content[0].Copy(); item.TargetEquipmentInventory.EmptyInventory(); } } } // we add one to the target equipment inventory item.TargetEquipmentInventory.AddItem(item.Copy(), item.Quantity); // remove 1 from quantity RemoveItem(index, item.Quantity); if (oldItem != null) { oldItem.Swap(); AddItem(oldItem, oldItem.Quantity); } MMInventoryEvent.Trigger(MMInventoryEventType.ItemEquipped, slot, this.name, item, item.Quantity, index); } }