Still looking for possible help with this although I feel like I have made a lot of progress.
I now have 3 lists:
ItemDatabase
Inventory
EquippedItems
These lists all sit inside their own classes and are all of type . Using a GetItemByID () and AddToList () I am able to populate my inventory, and then in a similar fashion I can populate my EquippedItems.
I have a UI set up for my inventory which simply instantiates a slot and an image when an Item is added (currently initiated in start but I will add a shop that will do this). So for my inventory part I think I am pretty much there.
The next step is to get the items in my EquippedItems list to appear in the correct slot when they are equipped, using an equip button, and removing them using an unequip button. I have 8 equipment slots whose types are identified using an enum (Head, Chest, Hands, Legs, Feet, Weapon1, Weapon2, Weapon3). My issues:
-
How can I pass the itemID to the equip/unequip buttons (these are instantiated as part of the slot prefab in the inventory).
-
How can I ensure an item will only go into the correct slot? This would involve comparing 2 enums (itemType and slotType) and I’m not sure on how to do this.
-
With my weapon slots I would obviously need to click the relevant equip button and then the slot that I would like to equip the weapon to. Again, how should I go about doing this?
I’m starting to feel like I’m in way over my head here. I feel like I’m confident enough with the C# but I don’t really know how to implement this system for a mobile device and to work with Unitys UI effectively. ANY input whatsoever would be very much appreciated! Many thanks.
BaseItem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class BaseItem
{
public enum ItemType
{
Head,
Chest,
Hands,
Legs,
Feet,
Weapon,
Resource,
Consumable
}
public int itemID;
public string itemName;
public string itemDescription;
public ItemType itemType;
public Sprite itemIcon;
public GameObject itemModel;
public BaseItem (int id, string name, string description, ItemType type)
{
itemID = id;
itemName = name;
itemDescription = description;
itemType = type;
itemIcon = Resources.Load<Sprite> ("Items/Icons/" + itemName);
itemModel = Resources.Load<GameObject> ("Items/Models/" + itemName);
}
public BaseItem ()
{
}
}
ItemDatabase
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDatabase : MonoBehaviour
{
public List<BaseItem> itemDatabase = new List<BaseItem> ();
private void Start ()
{
CreateDatabase ();
}
private void CreateDatabase ()
{
//Weapons
itemDatabase.Add(new BaseItem(0, "12Gauge", "A basic shotgun", BaseItem.ItemType.Weapon));
itemDatabase.Add(new BaseItem(1, "AK47", "A powerful rifle", BaseItem.ItemType.Weapon));
itemDatabase.Add(new BaseItem(2, "MiniUzi", "A powerful rifle", BaseItem.ItemType.Weapon));
itemDatabase.Add(new BaseItem(3, "RocketLauncher", "A powerful rifle", BaseItem.ItemType.Weapon));
itemDatabase.Add(new BaseItem(4, "TrenchGun", "A powerful rifle", BaseItem.ItemType.Weapon));
//Armor
itemDatabase.Add(new BaseItem(1000, "LeatherArmor", "A lightweight body armor", BaseItem.ItemType.Chest));
itemDatabase.Add(new BaseItem(1001, "IronHelmet", "The helmet of a true knight", BaseItem.ItemType.Head));
//Consumables
itemDatabase.Add(new BaseItem(2000, "MedKit", "Ooo look, health!", BaseItem.ItemType.Consumable));
itemDatabase.Add(new BaseItem(2001, "Caffeine", "Use sparingly!", BaseItem.ItemType.Consumable));
//Resources
itemDatabase.Add(new BaseItem(3000, "Coal", "A jet black substance used for crafting", BaseItem.ItemType.Resource));
itemDatabase.Add(new BaseItem(3001, "Diamond", "A beautiful gem used for crafting", BaseItem.ItemType.Resource));
}
public BaseItem GetItemByID (int id)
{
foreach (BaseItem item in itemDatabase)
{
if (item.itemID == id)
{
return item;
}
}
return null;
}
}
Inventory
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public GameObject inventoryPanel;
public GameObject inventorySlotParent;
public GameObject inventorySlot;
public GameObject itemImage;
public List<BaseItem> inventory = new List<BaseItem> ();
private ItemDatabase itemDatabase;
private void Start ()
{
itemDatabase = GameObject.FindGameObjectWithTag ("ItemDatabase").GetComponent<ItemDatabase> ();
//Testing purposes only. Populates inventory with all items in itemdatabase
for (int i = 0; i < itemDatabase.itemDatabase.Count; i++)
{
AddItemToInventoryList (itemDatabase.itemDatabase [i].itemID);
}
}
public void AddItemToInventoryList (int id)
{
BaseItem itemToAdd = itemDatabase.GetItemByID (id);
inventory.Add (new BaseItem (itemToAdd.itemID, itemToAdd.itemName, itemToAdd.itemDescription, itemToAdd.itemType));
AddItemToSlot (itemToAdd.itemIcon);
}
private void AddItemToSlot (Sprite icon)
{
//Create an empty slot
var slotInstance = Instantiate (inventorySlot);
slotInstance.transform.SetParent (inventorySlotParent.transform);
//Create an empty image in that slot
var iconInstance = Instantiate (itemImage);
iconInstance.transform.SetParent (slotInstance.transform);
//Change image to correct item image
itemImage.GetComponent<Image> ().sprite = icon;
}
//Used for adding items from the inventory into equipment list. Should be called when a suitable item is clicked from the inventory
public BaseItem GetItemByID (int id)
{
foreach (BaseItem item in inventory)
{
if (item.itemID == id)
{
return item;
}
}
return null;
}
}
EquippedItems
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EquippedItems : MonoBehaviour
{
//List of equipped items
public List<BaseItem> equippedItems = new List<BaseItem> ();
private Inventory inventory;
void Start ()
{
equippedInSlot = new GameObject[8];
inventory = GameObject.FindGameObjectWithTag ("InventoryManager").GetComponent<Inventory> ();
//Test Purposes, this function will be called by passing a parameter in using an equip button
AddItemFromInventoryToEquipmentList (0);
AddItemFromInventoryToEquipmentList (1);
AddItemFromInventoryToEquipmentList (2);
AddItemFromInventoryToEquipmentList (1000);
AddItemFromInventoryToEquipmentList (1001);
}
public void AddItemFromInventoryToEquipmentList (int id)
{
BaseItem itemToAdd = inventory.GetItemByID (id);
equippedItems.Add (new BaseItem (itemToAdd.itemID, itemToAdd.itemName, itemToAdd.itemDescription, itemToAdd.itemType));
}
}
EquipmentSlot
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EquipmentSlot : MonoBehaviour
{
public enum SlotType
{
Head,
Chest,
Hands,
Legs,
Feet,
Weapon
}
public SlotType slot;
public GameObject itemImage;
}