Search Unity

Best way populate a UI list or panel with game objects?

Discussion in 'Scripting' started by zeroBudget, Sep 29, 2021.

  1. zeroBudget

    zeroBudget

    Joined:
    Nov 14, 2019
    Posts:
    62
    I'm trying to create a booster panel, sort of like what you see in Candy Crush on the left of this image:



    My question is: Say I have a panel called "boosters". If I want to add items to it to show in the game like above, am I better off creating them programmatically by instantiating them in a script like in this video:



    But I was also thinking of simply making those items as GameObjects in the hierarchy, and then drag them into the panel in the editor.

    I was going to create them in the script, but I realize that I made need access to the objects at a later time. What's the best option, pros and cons? Or is there another solution?
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Creating with script is useful mostly if the set of objects you want to display changes depending on the circumstances, or if there's so many objects that creating them by hand is unwieldy. If you're always going to display the same 10 objects all the time, it's probably easier to create them by hand.
     
    Joe-Censored likes this.
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Instantiating them via script doesn't preclude you from accessing them elsewhere later. You just save a reference to the object when you instantiate it and then access it the same way you would otherwise.

    If the set of buttons isn't going to change, though, it probably makes more sense to just build it right into the UI element for that menu. It makes designing and debugging easier, at any rate. If you're just doing a limited set for now with the idea of having them be dynamic later, I'd recommend doing it the way that'll get you moving along faster at this time and only refactoring as needed.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It depends. Do you need the list to be Dynamic? Do you need to populate it externally? If you just have "boosters" or "power ups" that are always there, then I wouldn't see a reason to dynamically populate the gui. But if you need a count of each, that would be saved out somewhere and then update just a number count on the gui itself.
     
    zeroBudget likes this.
  5. zeroBudget

    zeroBudget

    Joined:
    Nov 14, 2019
    Posts:
    62
    Kinda both: It's the same 5-10 boosters. But if the count for one of the items is zero, it can be removed. And if the count is greater than 0, then obviously it will be added back.

    Yup just the same few ones. If the count for one of the items is zero, it can be removed, or added back if it is above zero.
    Gotcha, thanks for the info.

    Thanks everyone, I'll just make'em seperately.
     
    Last edited: Sep 29, 2021
    Schneider21 likes this.
  6. zeroBudget

    zeroBudget

    Joined:
    Nov 14, 2019
    Posts:
    62
    bump:

    This is actually kind of tricky: is there any alternative?

    The thing is, I want it to be that if the item count is zero, it won't show on the booster.

    The programmatic/serializable solution means I'd have to create and fill in the objects in the inspector... how would that work with keeping count of each one in game? (sorry if my question isn't clear)

    And making them separate game objects confuses me, because wouldn't it require them to have a specific position onscreen? I would prefer if it was "dynamic".(again, sorry if it isn't clear).
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    You can potentially put them into some sort of layout group (e.g. horizontal layout group, grid layout group, etc.) and use SetActive() to make them appear and disappear. (You might need to specifically tell the layout group to update afterwards in order for it to reposition everything; I seem to recall getting inconsistent results about whether that was required.)
     
    zeroBudget and Schneider21 like this.