Hey guys,
I’m new to Unity and C# in general, and for my first “real” project I wanted to play around with as much functionality as possible.
Now I’m trying to get my game to be multiplayer and it’s working out fine but I want the script to automatically populate a server list with buttons that have the name of the game on it (and possibly some other info later on) that will send you to the game when you click it.
Now I’ve been searching the forums and google for a solution for every day for almost a week now trying to find someone with a similar problem, but none of the solutions to their problems helped me.
So I’ve given up the search for a readily available fix and hope that one of you could possibly help me get back on track with this little project.
using UnityEngine;
using System.Collections;
public class MainMenu : MonoBehaviour {
public GameObject selfy;
private bool mainMenu;
private bool networkMenu;
private bool createNGame;
private bool exGameList;
public static string gameName;
public static string password;
public static string description;
private Vector2 scrollViewVector = Vector2.zero;
private HostData[] hostData;
// initializing variables
void Start () {
mainMenu = true;
networkMenu = false;
createNGame = false;
exGameList = false;
gameName = "-";
password = "-";
description = "-";
MasterServer.ClearHostList();
MasterServer.RequestHostList("VoetbalGameTest");
hostData = MasterServer.PollHostList();
}
void FixedUpdate () {
ServerList();
}
// Drawing the Starting Menu
void OnGUI(){
GUI.enabled = true;
if(mainMenu == true){
if(GUI.Button(new Rect(470,350,100,60), "Start Game")){
Debug.Log("Clicked ze button");
selfy.active = false;
}
if(GUI.Button(new Rect(470,450,100,60), "Network Play")){
mainMenu = false;
networkMenu = true;
}
}
// Networking menu
if(networkMenu == true){
if(GUI.Button(new Rect(470,350,100,60), "Create new")){
Debug.Log("Creating new network game");
networkMenu = false;
createNGame = true;
}
if(GUI.Button(new Rect(470,450,100,60), "Join Existing")){
Debug.Log("Joining existing game");
networkMenu = false;
exGameList = true;
}
if(GUI.Button(new Rect(650,600,100,60), "Back to Main")){
networkMenu = false;
mainMenu = true;
}
}
// Create Game Menu
if(createNGame == true){
GUI.Label(new Rect(470,320,300,20), "Name your new Game");
gameName = GUI.TextField(new Rect(470,350,100,20), gameName);
GUI.Label(new Rect(470,390,300,20), "A short description for your game");
description = GUI.TextField(new Rect(470,420,100,20), description);
GUI.Label(new Rect(470,460,300,20), "Enter a password if you want");
password = GUI.TextField(new Rect(470,490,100,20), password);
if(GUI.Button(new Rect(470,520,100,60), "Create")){
LaunchServer();
}
if(GUI.Button(new Rect(650,600,100,60), "Back to Main")){
createNGame = false;
mainMenu = true;
}
}
// Create table with existing games
if(exGameList == true){
GUILayout.BeginArea(new Rect(Screen.width/3, Screen.height/3, 600,300));
GUILayout.BeginVertical("Box");
scrollViewVector = GUILayout.BeginScrollView(scrollViewVector, GUILayout.Width(600), GUILayout.Height(300));
if(GUILayout.Button("Server 1")){
Debug.Log("Joining Server 1");
}
//Draw Server buttons here!
foreach(HostData element in hostData){
Debug.Log("Game Name: " + element.gameName);
if(GUILayout.Button("Game Name: " + element.gameName)){
Debug.Log("Clicked it sir");
}
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
void LaunchServer(){
Network.incomingPassword = password;
bool useNat = !Network.HavePublicAddress();
Network.InitializeServer(32, 25000, useNat);
MasterServer.RegisterHost("VoetbalGameTest", gameName, description);
}
IEnumerator ServerList(){
while(true){
yield return new WaitForSeconds(10.0f); //wait 10 seconds
Debug.Log("Refreshing serverlist");
MasterServer.ClearHostList();
hostData = MasterServer.PollHostList();
}
}
}
Sinnce I started I’ve pretty much had this document in any thinkable configuration, even had the networking bits in a different script and whatnot…
But couldn’t get it to work, so I am really hoping one of you can help me.
The real problem is with this part of the code:
// Create table with existing games
if(exGameList == true){
GUILayout.BeginArea(new Rect(Screen.width/3, Screen.height/3, 600,300));
GUILayout.BeginVertical("Box");
scrollViewVector = GUILayout.BeginScrollView(scrollViewVector, GUILayout.Width(600), GUILayout.Height(300));
if(GUILayout.Button("Server 1")){
Debug.Log("Joining Server 1");
}
//Draw Server buttons here!
foreach(HostData element in hostData){
Debug.Log("Game Name: " + element.gameName);
if(GUILayout.Button("Game Name: " + element.gameName)){
Debug.Log("Clicked it sir");
}
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
GUILayout.EndArea();
}
}
Regards,
Leonardi