Hi,
I’m trying to make a cycling item bar like Pokémon Legends Arceus, ( the right bottom one:
) where I press the shoulder buttons and it cycles through/ scrolls through the items:
using System.Collections.Generic;
using UnityEngine;
public class ActionMenu : MonoBehaviour
{
public Transform itemContainer; // The container holding all the items
private List<Transform> items = new List<Transform>(); // List of item game objects
private int selectedItemIndex = 0; // Index of the currently selected item
void Start()
{
// Populate the items list with the child objects of itemContainer
foreach (Transform item in itemContainer)
{
items.Add(item);
}
// Ensure there's at least one item
if (items.Count > 0)
{
// Select the first item by default
UpdateSelectedItem();
}
}
void Update()
{
// Toggle through items using input
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
SelectPreviousItem();
}
else if (Input.GetKeyDown(KeyCode.RightArrow))
{
SelectNextItem();
}
}
// Update the appearance of the selected item and its neighbors
void UpdateSelectedItem()
{
for (int i = 0; i < items.Count; i++)
{
CanvasGroup itemGroup = items[i].GetComponent<CanvasGroup>();
if (itemGroup != null)
{
// Set the alpha of the selected item to 1 and its neighbors to 0.7
float alpha = (i == selectedItemIndex) ? 1f : 0.5f;
itemGroup.alpha = alpha;
}
}
}
// Select the previous item in the list
void SelectPreviousItem()
{
if (items.Count > 0)
{
selectedItemIndex = (selectedItemIndex - 1 + items.Count) % items.Count;
UpdateSelectedItem();
}
}
// Select the next item in the list
void SelectNextItem()
{
if (items.Count > 0)
{
selectedItemIndex = (selectedItemIndex + 1) % items.Count;
UpdateSelectedItem();
}
}
}
My idea was to just make the items move places in the list, and then the position of the UI objects would change automatically, but I don’t know how to do this.
Another idea I tried was to just populate a Canvas with the items, move the items transform when pressing the shoulder buttons, but the problem is that the menu should start over, so the item on first place from the left needs to jump to last place on the right, and it was kinda tricky and didn’t work.
You guys have better ideas on how to do this?
Thanks!