Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Interaction Between UIButton and stored data

Discussion in 'Scripting' started by Sylon87, Jan 18, 2020.

  1. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196
    hello.

    i'm creating an inventory system for my game.

    i have a Slot Class that is a class where every single slot information are stored.

    as follow
    Code (CSharp):
    1. public class Slots
    2. {
    3.     public string ItemID  {get; private set;}
    4.  
    5.     public EBagCategory Category { get; private set;}
    6.  
    7.     public Sprite Icon { get; private set;}
    8.    
    9.  
    10.     public GameObject ItemModel { get; private set;}
    11.  
    12.     public string ItemDescription {get; private set;}
    13.  
    14.     public Slots(
    15.         string in_itemID,
    16.         EBagCategory in_bagCategory,
    17.         Sprite in_sprite,
    18.         GameObject in_modelObject,
    19.         string in_description
    20.     )
    21.     {
    22.         ItemID = in_itemID;
    23.         Category = in_bagCategory;
    24.         Icon = in_sprite;
    25.         ItemModel = in_modelObject;
    26.         ItemDescription = in_description;
    27.     }
    28.  
    29. }

    what i'm wondering is... when for example slot one is highlighted, i need to get the item description to show on the item description box, i'm wondering on which is the best way to achieve this.

    i'm doing this because if i create a List<Slot> i can also sort by category, name.. etc... but i'm wondering in which way is the better way to create the interaction with the UIButton.

    any advice?
     
  2. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    Create a function with and
    int
    parameter, then attach this function to every button in the inventory.
    make an array to collect the items description.
    when you attach the function to a button, an integer parameter will appear, type the number that represents the array index.
    your fuction should be like this:

    Code (CSharp):
    1. public string[] itemDescription;
    2. public text descriptionBox;
    3.  
    4. public void showDescription(int index) {
    5. //write your function here, maybe something like this
    6. descriptionBox.text = itemDescription[index];
    7. }
    *I assumed that your description is a text.
     
  3. Sylon87

    Sylon87

    Joined:
    Aug 20, 2018
    Posts:
    196

    thank's you for your reply!

    is similar to something that i saw in some tutorial...
    i should create an Monobehaviour script to attach to every single Slot then call a function from there when a button is trigged, is this the best way?

    and in the Event System input module there is only "Submit" and "Cancel" but on my inventory i need also that when a (for Example) Y button on the xbox controller is pressed, the item detail popup will showed,.

    i thought that to achieve this i should get if a button is trigget, then check which button is selected (Event system will help on it with the focused object ) and get the Item from there, is this the only waiy?