Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Scrolling Item Bar like Legends Arceus?

Discussion in 'Scripting' started by henhiro, May 7, 2024.

  1. henhiro

    henhiro

    Joined:
    Dec 11, 2019
    Posts:
    4
    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:

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3.  
    4. public class ActionMenu : MonoBehaviour
    5. {
    6.     public Transform itemContainer; // The container holding all the items
    7.     private List<Transform> items = new List<Transform>(); // List of item game objects
    8.     private int selectedItemIndex = 0; // Index of the currently selected item
    9.  
    10.     void Start()
    11.     {
    12.         // Populate the items list with the child objects of itemContainer
    13.         foreach (Transform item in itemContainer)
    14.         {
    15.             items.Add(item);
    16.         }
    17.  
    18.         // Ensure there's at least one item
    19.         if (items.Count > 0)
    20.         {
    21.             // Select the first item by default
    22.             UpdateSelectedItem();
    23.         }
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         // Toggle through items using input
    29.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    30.         {
    31.             SelectPreviousItem();
    32.         }
    33.         else if (Input.GetKeyDown(KeyCode.RightArrow))
    34.         {
    35.             SelectNextItem();
    36.         }
    37.     }
    38.  
    39.     // Update the appearance of the selected item and its neighbors
    40.     void UpdateSelectedItem()
    41.     {
    42.         for (int i = 0; i < items.Count; i++)
    43.         {
    44.             CanvasGroup itemGroup = items[i].GetComponent<CanvasGroup>();
    45.             if (itemGroup != null)
    46.             {
    47.                 // Set the alpha of the selected item to 1 and its neighbors to 0.7
    48.                 float alpha = (i == selectedItemIndex) ? 1f : 0.5f;
    49.                 itemGroup.alpha = alpha;
    50.             }
    51.         }
    52.     }
    53.  
    54.     // Select the previous item in the list
    55.     void SelectPreviousItem()
    56.     {
    57.         if (items.Count > 0)
    58.         {
    59.             selectedItemIndex = (selectedItemIndex - 1 + items.Count) % items.Count;
    60.             UpdateSelectedItem();
    61.         }
    62.     }
    63.  
    64.     // Select the next item in the list
    65.     void SelectNextItem()
    66.     {
    67.         if (items.Count > 0)
    68.         {
    69.             selectedItemIndex = (selectedItemIndex + 1) % items.Count;
    70.             UpdateSelectedItem();
    71.         }
    72.     }
    73. }
    74.  
    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!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,008
    Inventories are complex. To do it "properly" you need to divorce the notion of the list of all inventory items from those you possess, as well as have something that knows the order of them to cycle, as well as which one is currently selected.

    Note: nothing I listed above touches Unity in any way. Everything above is just pure C# data structures. You should keep Unity only for presenting the items to the user based on what the data is.

    I would suggest just starting from any tutorial that shows how to make an inventory system that is close to the one you contemplate. If they store state in Unity objects, well, that's not good because then you need to extract that state so you can serialize it and restore it the next time the menu comes up.

    So you can see how quickly the complexity grows... :)

    Imphenzia: How Did I Learn To Make Games:




    Here's my standard inventory cribsheet:

    These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.

    The following applies to ALL types of code listed above, but for simplicity I will call it "inventory."

    Inventory code never lives "all by itself." All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.

    If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.

    Inventories / shop systems / character selectors all contain elements of:

    - a database of items that you may possibly possess / equip
    - a database of the items that you actually possess / equip currently
    - perhaps another database of your "storage" area at home base?
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
    - interaction with items in the inventory or on the character or in the home base storage area
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    Just the design choices of such a system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - if there is an item limit, what is it? Total count? Weight? Size? Something else?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items? How? Limits? Results? Messages of success/failure?
    - can users substantially modify items with other things like spells, gems, sockets, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    If you want to see most of the steps involved, make a "micro inventory" in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).

    Everything you learn doing that "micro inventory" of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.

    Breaking down large problems in general:

    https://forum.unity.com/threads/opt...n-an-asteroid-belt-game.1395319/#post-8781697

    The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.
     
  3. samana1407

    samana1407

    Joined:
    Aug 23, 2015
    Posts:
    268
    If we're just talking about looping through items without animation, it's not complicated at all. I have three UIImage on the canvas. I added the following script to a convenient object, filled its inspector with references to those three Images in an array of slots, and filled the Items array with sprites from the project folder. By pressing the left and right arrow keys on the keyboard, the images in the slots cycle through, and three indexes of the current set are displayed in the GUI menu (just for visualization). But in this version, it's assumed that the number of Items is not less than the number of Slots. And since I don't know how such a menu should behave if it contains only one or two Items, I didn't implement it in the code.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class CyclingItemBar : MonoBehaviour
    6. {
    7.     public Image[] Slots; // 3 slots
    8.  
    9.     public List<Sprite> Items;
    10.  
    11.     private int _itemsOffset = 0;
    12.  
    13.     private void Start()
    14.     {
    15.         RefreshSlots();
    16.     }
    17.  
    18.     private void Update()
    19.     {
    20.         if (Input.GetKeyDown(KeyCode.RightArrow))
    21.         {
    22.             _itemsOffset++;
    23.             RefreshSlots();
    24.         }
    25.  
    26.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    27.         {
    28.             _itemsOffset--;
    29.             RefreshSlots();
    30.         }
    31.     }
    32.  
    33.     private void RefreshSlots()
    34.     {
    35.         for (int i = 0; i < Slots.Length; i++)
    36.         {
    37.             int index = ClampCycleZeroMax(_itemsOffset + i, Items.Count);
    38.  
    39.             Slots[i].sprite = Items[index];
    40.         }
    41.     }
    42.  
    43.     private int ClampCycleZeroMax(int value, int max)
    44.     {
    45.         value %= max;
    46.  
    47.         if (value < 0)
    48.             value = max - Mathf.Abs(value);
    49.  
    50.         return value;
    51.     }
    52.  
    53.     private void OnGUI()
    54.     {
    55.         GUILayout.Label("ITEMS");
    56.  
    57.         int firstItem = ClampCycleZeroMax(_itemsOffset + 0, Items.Count);
    58.         int secondItem = ClampCycleZeroMax(_itemsOffset + 1, Items.Count);
    59.         int thirdItem = ClampCycleZeroMax(_itemsOffset + 2, Items.Count);
    60.         GUILayout.Label($"[{firstItem}]  [{secondItem}]  [{thirdItem}]");
    61.     }
    62.  
    63. }