using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MainMenuController : MonoBehaviour { public GameObject mainMenu; public GameObject serverMenu; public GameObject lobbyMenu; public InputField username; public InputField ip; public InputField port; void Start() { // Simulate Lobbies in a Server for (int i = 0; i < 10; i++) { var lobby = Instantiate(serverMenu.transform.GetChild(2).GetChild(0).GetChild(0), serverMenu.transform.GetChild(2).GetChild(0)); lobby.GetComponentInChildren().text = "LBY" + Random.Range(0, 10) + Random.Range(0, 10) + Random.Range(0, 10); lobby.gameObject.SetActive(true); } } void Update() {} public void ConnectServer() { var conn = Client.GetConnection(); if (conn == null) { if (ip.text != "" && port.text != "") Client.Connect(username.text, ip.text + ":" + port.text); else Client.Connect(username.text); conn = Client.GetConnection(); } if (conn != null) { mainMenu.SetActive(false); serverMenu.SetActive(true); lobbyMenu.SetActive(false); } } public void LeaveServer() { Client.CloseConnection(); mainMenu.SetActive(true); serverMenu.SetActive(false); lobbyMenu.SetActive(false); } public void ConnectLobby(Text codeText) { var code = codeText.text; Debug.Log(code); var conn = Client.GetConnection(); if (conn != null) { var lobby = conn.GetLobby(); if (lobby == null) { conn.JoinLobby(code); mainMenu.SetActive(false); serverMenu.SetActive(false); lobbyMenu.SetActive(true); } } } public void CreateLobby() { var conn = Client.GetConnection(); if (conn != null) { var lobby = conn.GetLobby(); if (lobby == null) { conn.CreateLobby(); mainMenu.SetActive(false); serverMenu.SetActive(false); lobbyMenu.SetActive(true); } } } public void LeaveLobby() { var conn = Client.GetConnection(); if (conn == null) { var lobby = conn.GetLobby(); if (lobby != null) { //conn.LeaveLobby(); mainMenu.SetActive(false); serverMenu.SetActive(true); lobbyMenu.SetActive(false); } } } }