/* * This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl) * The original author of this code is Mike Hergaarden, even though some small parts * are copied from the Unity tutorials/manuals. * Feel free to use this code for your own projects, drop us a line if you made something exciting! */ #pragma strict var skin : GUISkin; var gameName = "Example3_Lobby"; var serverPort = 45671; private var timeoutHostList = 0.0; private var lastHostListRequest = -1000.0; private var hostListRefreshTimeout = 60.0; private var windowRect1; private var windowRect2; static var quickplayMode : boolean = false; static var quickplayModeStarted : float = 0.0; private var tryingToConnectquickplay : boolean = false; private var tryingToConnectquickplayNumber : int = 0; private var readyToConnect : boolean = false; private var remotePort : int[] = new int[3]; private var remoteIP : String[] = new String[3]; private var connectionInfo : String = ""; private var lastMSConnectionAttemptForcedNat : boolean= false; private var NAToptionWasSwitchedForTesting : boolean = false; private var officialNATstatus : boolean = Network.useNat; private var errorMessage : String = ""; private var lastquickplayConnectionTime : float; var customButton : GUIStyle; private var showMenu : boolean = false; private var mainMenuScript : MainMenu; ///////////////////////////// function Awake(){ windowRect1 = Rect (Screen.width/2-305,Screen.height/2-140,380,280); windowRect2 = Rect (Screen.width/2+110,Screen.height/2-140,220,100); } function Start(){ mainMenuScript = MainMenu.SP; } function EnableMenu(quickplay : boolean){ showMenu=true; tryingToConnectquickplayModeNumber=0; tryingToConnectquickplay=false; quickplayMode=quickplay; if(quickplay){ quickplayModeStarted=Time.time; } MasterServer.RequestHostList (gameName); lastHostListRequest = Time.realtimeSinceStartup; remoteIP[0]="127.0.0.1"; remotePort[0]=serverPort; } function OnFailedToConnectToMasterServer(info: NetworkConnectionError) { //Dont request the MS list the next 60 sec lastHostListRequest = Time.realtimeSinceStartup + 60; } function OnFailedToConnect(info: NetworkConnectionError) { Debug.Log("Failed To Connect info: "+info); var invalidPass : boolean = false; if(info==NetworkConnectionError.InvalidPassword){ invalidPass=true; } FailedConnRetry(invalidPass); } function FailedConnRetry(invalidPassword : boolean){ if(tryingToConnectquickplay){ //Try again without NAT if we used NAT if(Network.useNat && lastMSConnectionAttemptForcedNat){ Debug.Log("Failed 1A"); lastMSConnectionAttemptForcedNat=false; Network.useNat=false; remotePort[0]=serverPort;//Fall back to default server port Network.Connect(remoteIP, remotePort[0]); lastquickplayConnectionTime=Time.time; }else{ Debug.Log("Failed 1B"); //Reset NAT to org value Network.useNat=officialNATstatus; tryingToConnectquickplayNumber++; tryingToConnectquickplay=false; } }else{ //Direct connect or host list connectionInfo="Failed to connect!"; if(Network.useNat && lastMSConnectionAttemptForcedNat){ Debug.Log("Failed 2A"); Network.useNat=false; Network.Connect(remoteIP, remotePort[0]); lastquickplayConnectionTime=Time.time; }else{ Debug.Log("Failed 2B"); errorMessage="Failed to connect to "+remoteIP[0]+":"+remotePort[0]; if(invalidPassword){ errorMessage+="\nYou used the wrong password (or you didn't need to enter one!)."; } //reset default port remotePort[0]=serverPort; //Reset nat to tested value Network.useNat=officialNATstatus; } } } function OnConnectedToServer(){ Debug.Log("Connected to lobby!"); showMenu=false; var gameLobbyScript : GameLobby = GetComponent(GameLobby); gameLobbyScript.EnableLobby(); } function OnGUI () { if(!showMenu){ return; } //Back to main menu if(GUI.Button(Rect(40,10,150,20), "Back to main menu")){ showMenu=false; mainMenuScript.OpenMenu("multiplayer"); } if(errorMessage && errorMessage!=""){ GUI.Box(Rect(Screen.width/2-100,Screen.height/2-30,200,60), "Error"); GUI.Label(Rect(Screen.width/2-90,Screen.height/2-15,180,50), errorMessage); if(GUI.Button(Rect(Screen.width/2+40,Screen.height/2+5,50,20), "Close")){ errorMessage=""; } } if(quickplayMode){ quickplayFunction(); }else{ if(!errorMessage || errorMessage==""){ //Hide windows on error windowRect1 = GUILayout.Window (0, windowRect1, listGUI, "Join a game via the list"); windowRect2 = GUILayout.Window (1, windowRect2, directConnectGUIWindow, "Directly join a game via an IP"); //windowRect3 = GUILayout.Window (2, windowRect3, hostGUI, "Host a game"); } } } function quickplayFunction(){ GUI.Box(Rect(Screen.width/4+180,Screen.height/2-30,280,50), ""); var i : int=0; var data : HostData[] = MasterServer.PollHostList(); for (var element in data) { // Do not display NAT enabled games if we cannot do NAT punchthrough if ( !(natTester.filterNATHosts && element.useNat) && !element.passwordProtected ) { aHost=1; if(element.connectedPlayers lastHostListRequest + hostListRefreshTimeout) { if(Time.realtimeSinceStartup > lastHostListRequest + 5){//max once per 5 sec MasterServer.RequestHostList (gameName); lastHostListRequest = Time.realtimeSinceStartup; } } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); scrollPosition = GUILayout.BeginScrollView (scrollPosition); var aHost =0; var data : HostData[] = MasterServer.PollHostList(); for (var element in data) { GUILayout.BeginHorizontal(); // Do not display NAT enabled games if we cannot do NAT punchthrough if ( !(natTester.filterNATHosts && element.useNat) ) { aHost=1; var name = element.gameName + " "; GUILayout.Label(name); GUILayout.FlexibleSpace(); GUILayout.Label(element.connectedPlayers + "/" + element.playerLimit); if(element.useNat){ GUILayout.Label("."); } GUILayout.FlexibleSpace(); GUILayout.Label("[" + element.ip[0] + ":" + element.port + "]"); GUILayout.FlexibleSpace(); if(element.passwordProtected){ GUILayout.Label("PASSWORD"); if (GUILayout.Button("Connect")) { if(password==""){ errorMessage="You must enter a password if you want to join this game!"; return; }// Enable NAT functionality based on what the hosts if configured to do Network.useNat = element.useNat; lastMSConnectionAttemptForcedNat = element.useNat; if (Network.useNat){ print("Using Nat punchthrough to connect"); }else{ print("Connecting directly to host"); } remoteIP=element.ip; remotePort[0]=element.port; var connectPort2 : int=element.port; Network.Connect(element.ip, connectPort2, password); } }else{ if (GUILayout.Button("Connect")) { // Enable NAT functionality based on what the hosts if configured to do Network.useNat = element.useNat; lastMSConnectionAttemptForcedNat = element.useNat; if (Network.useNat){ print("Using Nat punchthrough to connect"); }else{ print("Connecting directly to host"); } remoteIP=element.ip; remotePort[0]=element.port; var connectPort : int=element.port; Network.Connect(element.ip, connectPort); } } GUILayout.Space(15); } GUILayout.EndHorizontal(); } if(aHost==0){ GUILayout.Label("No games hosted at the moment.."); } GUILayout.EndScrollView (); }