New to Unity and followed a tutorial on how to setup a inventory UI. I created a Canvas with UI elements and a bunch of Slots. I can add items to the inventory I get from the ground, but have no idea how to drop, use, or rearrange the items I pickup. I have a script for Inventory_UI, Slot_UI, and inventory. I am trying to say on Left mouse click use the item and on right click drop the item. I just don’t know how to determine which slot I clicked on in the inventory to return the item im clicking.
I am not sure which script my code should go into or the best way to go about this. I am trying to use the new Input system and am unsure if this is the correct way to go about this. Any guidance would be greatly appreciated.
Slot_UI code I am trying UseItem to determine which item i clicked on.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using UnityEngine.InputSystem;
public class Slot_UI : MonoBehaviour
{
private PlayerActionControls playerActionControls;
public Image itemIcon;
public PlayerController player;
public TextMeshProUGUI quantityText;
private Slot_UI slot;
private void Awake(){
playerActionControls = new PlayerActionControls();
}
private void OnEnable() {
playerActionControls.Enable();
}
private void OnDisable() {
playerActionControls.Disable();
}
void Start(){
playerActionControls.UI.Click.performed += cxt => UseItem(cxt);
}
private void UseItem(InputAction.CallbackContext cxt){
print(slot.itemIcon);
Debug.Log("use item");
}
public void SetItem(Inventory.Slot slot){
if(slot != null){
itemIcon.sprite = slot.icon;
itemIcon.color = new Color(1, 1, 1, 1);
quantityText.text = slot.count.ToString();
}
}
public void SetEmpty(){
itemIcon.sprite = null;
itemIcon.color= new Color(1, 1, 1, 0);
quantityText.text = "";
}
}
Inventory_ui script
using System.Collections;
[code=CSharp]using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Inventory_UI : MonoBehaviour
{
public GameObject inventoryPanel;
public Inventory inv;
public PlayerController player;
private PlayerActionControls playerActionControls;
public List<Slot_UI> slots = new List<Slot_UI>();
void Update()
{
if(Input.GetKeyDown(KeyCode.Tab)){
ToggleInventory();
}
}
public void ToggleInventory(){
if(!inventoryPanel.activeSelf){
inventoryPanel.SetActive(true);
Setup();
}
else{
inventoryPanel.SetActive(false);
}
}
void Setup(){
if(slots.Count == player.inventory.slots.Count){
for(int i = 0; i < slots.Count; i++){
if(player.inventory.slots[i].type != CollectableType.NONE){
slots[i].SetItem(player.inventory.slots[i]);
}
else{
slots[i].SetEmpty();
}
}
}
}
}
Inventory.cs Script
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
[System.Serializable]
public class Inventory
{
private PlayerActionControls playerActionControls;
private Slot_UI slot;
private void Awake(){
playerActionControls = new PlayerActionControls();
}
private void OnEnable() {
playerActionControls.Enable();
}
private void OnDisable() {
playerActionControls.Disable();
}
[System.Serializable]
public class Slot{
public CollectableType type;
public int count;
public int maxAllowed;
public Sprite icon;
public Slot(){
type = CollectableType.NONE;
count = 0;
maxAllowed = 99;
}
public bool CanAddItem(){
if(count < maxAllowed){
return true;
}
return false;
}
public void AddItem(Collectable item){
this.type = item.type;
this.icon = item.icon;
count++;
}
}
public List<Slot> slots = new List<Slot>();
public Inventory(int numSlots){
for(int i = 0; i < numSlots; i++){
Slot slot = new Slot();
slots.Add(slot);
}
}
public void Add(Collectable item){
Debug.Log("ADD ITEM");
foreach(Slot slot in slots){
if(slot.type == item.type && slot.CanAddItem()){
Debug.Log("Adding "+item.type);
slot.AddItem(item);
return;
}
}
foreach(Slot slot in slots){
if(slot.type == CollectableType.NONE){
slot.AddItem(item);
return;
}
}
}
}
