using foreach and generating buttons

if there any difference how i would translate this with the new UI?

GUILayout.BeginArea(new Rect(Screen.width - 350, 0, 350, Screen.height), “Available Games”, “Box”);
GUILayout.Space(20);
foreach (HostData match in MasterServer.PollHostList())
{
GUILayout.BeginHorizontal(“Box”);

GUILayout.Label(match.gameName);

if (GUILayout.Button(“Connect”))
{
Network.Connect(match);
}

GUILayout.EndHorizontal();
}

GUILayout.EndArea();
}

basically for every server available it create a button and clicking that button will allow user to connect

The most straight-forward way would probably be to instantiate a button prefab for each one, then position it and add an onClick callback through script. I’ve posted a couple of snippets already that do exactly this; just check my post history if you want to see some code. =)

EDIT: Post history isn’t as easy to scan as I thought; here it is:

for(int i=0; i<5; i++){
    Button btn = ((GameObject)Object.Instantiate(btnPrefab)).GetComponent<Button>();
    btn.onClick.AddListener(delegate{Callback(btn);});
}

void Callback(Button btnPressed){
    Debug.Log(btnPressed.gameObject.name + " pressed!");
}

alright thanks ill give this a go …

This confuses me a bit…i need to pull from server list all servers that are currently created my previous code allowed to pull the server’s name along with a button to connect to that server but that was with the old GUI

Ok, so the new uGUI works quite different than the old OnGUI() stuff. I would strongly advice you to watch Unity’s video tutorials on the topic before moving on. After that you’ll probably want to keep track of which rooms already have a button, and add and remove buttons as needed using Instantiate() and Destroy().

1 Like

well i watched all the tuts on the new GUI none specify or go over rather, what im looking to do but thanks Senshi im going to try a few things out i know uGUI works differently just got to figure out my switch from old to new in a more complete form

Alright, good luck and feel free to ask if you need any further/ more specific help!