Search Unity

How to best create UI buttons from data at runtime

Discussion in 'Scripting' started by csbailey1984, Feb 21, 2017.

  1. csbailey1984

    csbailey1984

    Joined:
    Feb 20, 2017
    Posts:
    3
    So this is what I would like to accomplish. I have a set of data that includes a few strings and integers and I wanted to use that data to populate a series of clickable objects. I figured using a UI button would be easiest since it already has all the benefits of organizing neatly into other UI elements. I created a button as a prefab but I haven't been able to figure out a a solid way of creating all of these items. Additionally, I need a variety of them excluded from the list until such time as certain prerequisites are met. I attempted to use gameObject.SetActive but that completely disables the object and as such I can no longer reference its data to see if the perquisites are met. Any ideas? I appreciate it in advance.
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    no, it just stops you using "find" to get at it... really going to need to see how you are attempting to implement this to comment on that further.

    anything can be made a "clickable object" by implementing the IPointerClick interface...
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Check out this tutorial.



    The basic idea is to create a prefab. As you scan through the list you can Instantiate new buttons. Each button can have its own code added to its OnClick event.
     
  4. csbailey1984

    csbailey1984

    Joined:
    Feb 20, 2017
    Posts:
    3
    How would it be best for me to show you?

    It seems to disable scripts that are attached to the disabled object as well. Am I mistaken?
     
  5. csbailey1984

    csbailey1984

    Joined:
    Feb 20, 2017
    Posts:
    3
    Thanks I will see how I can use this!