- Home /
Question by
ealva479 · Jun 27, 2018 at 01:20 AM ·
uiinventory systemitem
Can't seem to get my item hotbar working correctly.
I have my inventory system working for the most part, and now I am working on getting my item hotbar connected, the same as minecraft, so if you pick up an item, it goes to the hotbar first, then once it fills up, it starts going to the actual inventory. Also, you can press the keys 1-6 to select a certain one.
Hotbar Code: using UnityEngine;
public class Hotbar : MonoBehaviour { int space = 6; bool hotbarFull = false;
public int selectedWeapon = 0;
public Transform hotbar;
Inventory inventory;
HotSlot[] hotSlots;
[SerializeField]
public GameObject item1;
[SerializeField]
public GameObject item2;
[SerializeField]
public GameObject item3;
[SerializeField]
public GameObject item4;
[SerializeField]
public GameObject item5;
[SerializeField]
public GameObject item6;
private bool showItem1;
private bool showItem2;
private bool showItem3;
private bool showItem4;
private bool showItem5;
private bool showItem6;
public void Start()
{
inventory = Inventory.instance;
if (hotbarFull == false)
{
inventory.onItemChangedCallback += UpdateHotbar;
} else if(hotbarFull == true)
{
inventory.onItemChangedCallback += FindObjectOfType<InventoryUI>().UpdateUI;
}
inventory.onItemChangedCallback += UpdateHotbar;
showItem1 = true;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
hotSlots = hotbar.GetComponentsInChildren<HotSlot>();
}
public void Update()
{
if (showItem1 == false)
{
item1.SetActive(false);
}
if (showItem1 == true)
{
item1.SetActive(true);
}
if (showItem2 == false)
{
item2.SetActive(false);
}
if (showItem2 == true)
{
item2.SetActive(true);
}
if (showItem3 == false)
{
item3.SetActive(false);
}
if (showItem3 == true)
{
item3.SetActive(true);
}
if (showItem4 == false)
{
item4.SetActive(false);
}
if (showItem4 == true)
{
item4.SetActive(true);
}
if (showItem5 == false)
{
item5.SetActive(false);
}
if (showItem5 == true)
{
item5.SetActive(true);
}
if (showItem6 == false)
{
item6.SetActive(false);
}
if (showItem6 == true)
{
item6.SetActive(true);
}
if (Input.GetKeyDown(KeyCode.Alpha1) && showItem1 == false)
{
showItem1 = true;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha1) && showItem1 == true)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
if (Input.GetKeyDown(KeyCode.Alpha2) && showItem2 == false)
{
showItem1 = false;
showItem2 = true;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha2) && showItem2 == true)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
if (Input.GetKeyDown(KeyCode.Alpha3) && showItem3 == false)
{
showItem1 = false;
showItem2 = false;
showItem3 = true;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha3) && showItem3 == true)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
if (Input.GetKeyDown(KeyCode.Alpha4) && showItem4 == false)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = true;
showItem5 = false;
showItem6 = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha4) && showItem4 == true)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
if (Input.GetKeyDown(KeyCode.Alpha5) && showItem5 == false)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = true;
showItem6 = false;
}
else if (Input.GetKeyDown(KeyCode.Alpha5) && showItem5 == true)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
if (Input.GetKeyDown(KeyCode.Alpha6) && showItem6 == false)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = true;
}
else if (Input.GetKeyDown(KeyCode.Alpha6) && showItem6 == true)
{
showItem1 = false;
showItem2 = false;
showItem3 = false;
showItem4 = false;
showItem5 = false;
showItem6 = false;
}
}
public void UpdateHotbar()
{
for (int i = 0; i < hotSlots.Length; i++)
{
if (i < inventory.items.Count)
{
hotSlots[i].AddItem(inventory.items[i]);
}
else
{
hotSlots[i].ClearSlot();
}
}
}
public void StopUpdating()
{
if (hotSlots.Length >= space)
{
hotbarFull = true;
Debug.Log("Not enough room in hotbar.");
inventory.onItemChangedCallback += GetComponent<InventoryUI>().UpdateUI;
}
}
}
Hotbar Slot Code:
using UnityEngine; using UnityEngine.UI;
public class HotSlot : MonoBehaviour {
public Image icon;
public Button removeButton;
private GameObject player;
Item item;
public void AddItem(Item newItem)
{
item = newItem;
icon.sprite = item.icon;
icon.enabled = true;
removeButton.interactable = true;
}
public void ClearSlot()
{
item = null;
icon.sprite = null;
icon.enabled = false;
removeButton.interactable = false;
}
public void OnRemoveButton()
{
ClearSlot();
Inventory.instance.Remove(item);
//Instantiate(item, player.transform.position + new Vector3(0, -3, 0), player.transform.rotation);
}
public void UseItem()
{
if (item != null)
{
item.Use();
}
}
}
Inventory:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Inventory : MonoBehaviour { private GameObject player;
#region Singleton
public static Inventory instance;
private void Awake()
{
if(instance != null)
{
Debug.LogWarning("More than one instance of Inventory found!");
return;
}
instance = this;
}
#endregion
public delegate void OnItemChanged();
public OnItemChanged onItemChangedCallback;
public int space;
public List<Item> items = new List<Item>();
public bool Add (Item item)
{
if(!item.isDefaultItem)
{
if(items.Count >= space)
{
Debug.Log("Not enough room.");
return false;
}
items.Add(item);
if(onItemChangedCallback != null)
onItemChangedCallback.Invoke();
}
return true;
}
public void Remove(Item item)
{
items.Remove(item);
if (onItemChangedCallback != null)
onItemChangedCallback.Invoke();
print(item.name);
}
}
Comment