#How should I got about making a hotbar/toolbar?

1 messages · Page 1 of 1 (latest)

soft solar
#

I've tried making a toolbar with UI in the past, it did not work and imo poorly optimized. Now I'm developing a new game with limited knowledge on good designs of hotbars. I'm not looking for extremely detailed ways and all the small stuff, I'm just looking for a script that can get scroll input reasonably and deactivate/activate children objects of the hotbar object (or any way that works good, just preferred). I think I could handle the UI and toolbar visuals myself, any help would be nice to figure out a good way or design of a scrollable hotbar.

New input system wanted if possible

jovial rampart
#

The Scroll View already provides scroll detection functionality through the size of the child "Content" object's RectTransform, you could use that, it sounds like this is largely the functionality your looking for? (Game Object > UI > Scroll View): https://ouzaniabdraouf.medium.com/in-unityhow-to-use-the-scroll-view-ui-element-in-unity-71492e051618#:~:text=A Scroll view is a,to scroll over this content.

Medium

A Scroll view is a Unity UI element that contains a Scroll Rect, a mask and scrollbar components. It can be used when content that takes…

soft solar
#

I ended up not using scroll view and instead went for a refurbished version of my original design. UI is working, but the disabling/enabling of items is not working properly. Basically, player receives any item that they can interact with will be placed as a child gameobject under "Tool Handler." So all the items you see on the hotbar are inside the Tool Handler gameobject. The script I have is supposed to control what gets deactivated and activated when scrolling. Here is the script used ```cs
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.InputSystem;

public class SwitchItems : MonoBehaviour
{
//This script disables items that are not being held by the player, and enables the ones that are. (both UI and functonality)

[SerializeField] List<GameObject> hotbarImageObjects = new List<GameObject>(5); //These are the images on the hotbar
[SerializeField] List<GameObject> currentItems = new List<GameObject>(1); //any gameobject under "Tool Handler" which can be found as a child in player.
[SerializeField] RectTransform highlight; //the outline on the hotbar that goes over the selected item
private int hotbarSelectIndex = 0;
public int HotbarSelectIndex
{
    get
    {
        return hotbarSelectIndex;
    }
    set
    {
        //This code prevents the index used in hotbarImageObjects to go out of bounds
        if (value > hotbarImageObjects.Count-1) value = 0;
        else if (value < 0) value = hotbarImageObjects.Count-1;

        hotbarSelectIndex = value;
    }
}

void OnMouseScroll(InputValue value)
{
    if (value.Get<float>() == 0) return; 

    if (value.Get<float>() > 0)
    HotbarSelectIndex--;
    else if (value.Get<float>() < 0)
    HotbarSelectIndex++;

    //outlines the selected item
    highlight.position = hotbarImageObjects[hotbarSelectIndex].GetComponent<RectTransform>().position;
    SelectedItem();
}

void SelectedItem()
{
    //The functionality behind switching items
    foreach (var item in currentItems)
    {
        item.SetActive(false);
    }
   
    currentItems[hotbarSelectIndex].SetActive(true);
}

public void NewItem(GameObject item)
{
    //called by other scripts when a new item wants to be added to the player and its hotbar
    currentItems.Add(item);
}

}

The problem lies within the SelectedItem method, Specifically **line 52** with the `hotbarSelectIndex` integer.  the integer can have a possible value between 0, the start of the hotbar, and 5, the end of the hotbar. If the values inputted go over, it will wrap, EX: if you put a value that goes over 5 in the integer, the integer will set to 0 and vice versa. Problem is, there is always five slots in the hotbar and no less, but the player doesn't constantly have five items, in fact they start out with only one item. Because of this, the `hotbarSelectIndex` sometimes has a index value too large for the `currentItems` list and causes an out of bounds error. **I need to find a solution to where not having five items still allows me to properly deactivate/activate items correctly.** I tried thinking of possible solutions by myself but can't even think one possible idea, I can send more information and pictures if needed.
#

(These item images on the hotbar are exmaples, still haven't coded the part where image changes to look like the corresponding item as it is not needed currently)

jovial rampart
#

If your going with your own system, and you know the slot size will always be fixed at 5, then you should maintain a list of 5 slots that get disabled/enabled, and update the index of those slots with relevant child data (for example, a blank icon if there is nothing in that slot, a relevant icon from your item data otherwise)