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

Create a list of buttons through code (For server browser)

Discussion in 'UGUI & TextMesh Pro' started by valax, Aug 22, 2014.

  1. valax

    valax

    Joined:
    Jul 8, 2012
    Posts:
    14
    Hey everyone!

    For my game, I need to be able to be able to populate a scroll-able area with buttons. Using the old GUI system, I would just do the following inside a GUILayout scrollview:

    Code (CSharp):
    1. foreach (RoomInfo room in PhotonNetwork.GetRoomList())
    2. {
    3.     if(GUILayout.Button(room.name))
    4.         PhotonNetwork.JoinRoom(room.name);
    5. }
    Even though it looked bloody hideous, it got the job done. Now - using the new GUI - I'm wondering how I can reproduce this. Thanks for any help you guys can provide!

    ps. I'm definitely a programmer, not an artist or UI designer so forgive my lack of knowledge in this area :p
     
  2. LuxUnity

    LuxUnity

    Joined:
    Sep 29, 2010
    Posts:
    717
    Code (csharp):
    1.  
    2.  
    3.    public Transform panel_target;//this must have Grid Layout Group
    4.    public GameObject mybutton;
    5.    GameObject temp;
    6.    // Use this for initialization
    7.    void Start () {
    8.    
    9.      foreach (RoomInfo room in PhotonNetwork.GetRoomList())
    10.      {
    11.        temp = (GameObject)Instantiate(mybutton);
    12.          temp.transform.parent = panel_target_target;
    13.      }
    14.    }
    15.  
     
  3. valax

    valax

    Joined:
    Jul 8, 2012
    Posts:
    14
    Thanks! I'm going to test this out now.

    Edit: Thanks so much for the help! It works perfectly. See the post below.
     
    Last edited: Aug 22, 2014
  4. valax

    valax

    Joined:
    Jul 8, 2012
    Posts:
    14
    There is some really odd behavior when I set the parent of the instantiated to the panel. It should look like this (What I get when I place it not at runtime in the editor)

    Instead (When I set the parent using transform.parent = x) it looks like this


    Sorry for the formatting on this post...
     
  5. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,221
    Hi, does using the transform.SetParent() function work for you?

    As said by Rune:

     
    Chri907 likes this.
  6. valax

    valax

    Joined:
    Jul 8, 2012
    Posts:
    14
    Aha, Thank you! This works like a charm.