I’m having an extremely difficult time trying to implement a basic cursor controlled by the keyboard or game controller that operates an inventory. I looked up and down the web for inventory tutorials but almost all of them use the mouse to navigate the inventory. The tutorials on pause menus don’t really work for me because the inventory system I want to implement does not have a fixed number of buttons, that is, items. That setup requires me to load a first “selected item” which may or may not be available. I also can’t simply make a placeholder “item” when no items exist because I have the code set up to clear all of the contents whenever the inventory is updated. I need help! Thanks!
Code for Inventory Manager:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class InventoryManager : MonoBehaviour
{
[Header("Inventory information")]
public PlayerInventory playerInventory;
[SerializeField] private GameObject blankInventorySlot;
[SerializeField] private GameObject inventoryPanel;
// The "Content" part of the Viewport in the Inventory Menu
[SerializeField] private TextMeshProUGUI descriptionText;
[SerializeField] private GameObject equipButton;
public InventoryItems currentItem;
public Animator inventoryAnimator;
public bool InventoryOpen;
private bool StartInit;
public InventoryCursor cursor;
[SerializeField] private GameObject noItems;
//'InventoryOpen' will help prevent the Inventory menu from automatically sliding back
// up off screen to its inactive state right after being summoned. The same is true for the
// other way around; The bool also prevents the menu from re-opening automatically right
// after being closed.
public void PullDownInventory()
{
if (!InventoryOpen)
{
inventoryAnimator.SetTrigger("InventoryActive");
InventoryOpen = true;
}
}
public void CloseInventory()
{
if (InventoryOpen)
{
inventoryAnimator.SetTrigger("InventoryInactive");
InventoryOpen = false;
}
}
public void SetTextAndButton(string description, bool buttonActive)
{
descriptionText.text = description;
if (buttonActive)
{
equipButton.SetActive(true);
} else
{
equipButton.SetActive(false);
}
}
void MakeInventorySlot()
{
if (playerInventory)
// If the Player Inventory exists...
{
for (int i = 0; i < playerInventory.myInventory.Count; i++)
// Go through all existing slots starting at 0.
{
if (playerInventory.myInventory[i].numberHeld > 0)
{
GameObject temp = Instantiate(blankInventorySlot, inventoryPanel.transform.position, Quaternion.identity);
temp.transform.SetParent(inventoryPanel.transform);
InventorySlot newSlot = temp.GetComponent<InventorySlot>();
newSlot.transform.SetParent(inventoryPanel.transform);
if (newSlot)
{
newSlot.Setup(playerInventory.myInventory[i], this);
}
Debug.Log("I contain " + currentItem + " and my integer is " + i);
if (StartInit && i == 0)
{
cursor.InitCursor(inventoryPanel);
StartInit = false;
}
} if (playerInventory.myInventory.Count == 0)
{
GameObject other = Instantiate(noItems, inventoryPanel.transform.position, Quaternion.identity);
other.transform.SetParent(inventoryPanel.transform);
InventorySlot newSlot = other.GetComponent<InventorySlot>();
newSlot.transform.SetParent(inventoryPanel.transform);
}
}
}
}
private void Start()
{
StartInit = true;
InventoryOpen = false;
// The inventory, by default, should always be inactive, or at least off screen,
// when the game starts.
ClearInventorySlots();
MakeInventorySlot();
}
private void OnEnable()
{
SetTextAndButton("", false);
}
// Start is called before the first frame update
public void UpdateInventory()
{
ClearInventorySlots();
MakeInventorySlot();
}
public void Update()
{
if(GameObject.FindGameObjectWithTag("Player").GetComponent<Health>().dead)
{
Destroy(this.gameObject);
}
}
// Update is called once per frame
public void SetupDescriptionAndButton(string newDescriptionString, bool buttonAvailable, InventoryItems newItem)
{
currentItem = newItem;
descriptionText.text = newDescriptionString;
equipButton.SetActive(buttonAvailable);
}
public void ClearInventorySlots()
{
for(int i = 0; i < inventoryPanel.transform.childCount; i++)
{
Destroy(inventoryPanel.transform.GetChild(i).gameObject);
Debug.Log("I destroyed " + inventoryPanel.transform.GetChild(i).gameObject + "and its integer was "
+ i + " and its coordinates were " + inventoryPanel.transform.GetChild(i).gameObject.transform.position.x + " x and "
+ inventoryPanel.transform.GetChild(i).gameObject.transform.position.y);
}
}
public void EquipBoxMarked()
{
if (currentItem)
{
currentItem.Equip();
ClearInventorySlots();
MakeInventorySlot();
if (currentItem.numberHeld == 0)
{
SetTextAndButton("", false);
// Keep the description and equip checkbox button active until the item
// reaches zero.
}
}
}
}
For the sake of space, I’m not including all associated codes in this post, but if you need me to post them, let me know. Thanks again!