Ejemplo n.º 1
0
    // Use this for initialization
    void Start()
    {
        // setup reference to networkVariables
        nvs    = gameObject.GetComponent("networkVariables") as networkVariables;
        myInfo = nvs.myInfo;

        // add us to the player list
        nvs.players.Add(myInfo);

        // get server version
        serverVersion = nvs.serverVersion;              // maybe set the server version to the map name?
        // get server name
        string serverName = nvs.serverName + ": Lobby";

        // serverComment also holds extra info about the server
        serverComment         = new ServerComment();
        serverComment.NATmode = nvs.NATmode;
        serverComment.comment = "This is a comment about the server";
        serverComment.level   = "level_full";           // only use this one since it's the only one set up atm

        // Use NAT punchthrough if NATmode says to
        Network.InitializeServer(31, 11177, nvs.NATmode != 0);
        Debug.Log(serverComment.toString());
        //MasterServer.updateRate = 5; - not needed anymore since Unity doesn't understand networking
        MasterServer.RegisterHost(serverVersion, serverName, serverComment.toString());

        // get game state
        gameHasBegun = nvs.gameHasBegun;

        // go into the lobby
        gameObject.AddComponent <netLobby>();
    }
    // Use this for initialization
    void Start()
    {
        // setup reference to networkVariables
        nvs = gameObject.GetComponent("networkVariables") as networkVariables;
        myInfo = nvs.myInfo;

        // add us to the player list
        nvs.players.Add(myInfo);

        // get server version
        serverVersion = nvs.serverVersion;	// maybe set the server version to the map name?
        // get server name
        string serverName = nvs.serverName + ": Lobby";
        // serverComment also holds extra info about the server
        serverComment = new ServerComment();
        serverComment.NATmode = nvs.NATmode;
        serverComment.comment = "This is a comment about the server";
        serverComment.level = "level_full";	// only use this one since it's the only one set up atm

        // Use NAT punchthrough if NATmode says to
        Network.InitializeServer(31, 11177, nvs.NATmode!=0);
        Debug.Log(serverComment.toString());
        //MasterServer.updateRate = 5; - not needed anymore since Unity doesn't understand networking
        MasterServer.RegisterHost(serverVersion, serverName, serverComment.toString());

        // get game state
        gameHasBegun = nvs.gameHasBegun;

        // go into the lobby
        gameObject.AddComponent("netLobby");
    }
Ejemplo n.º 3
0
    // TODO: use something else instead of OnGUI
    void OnGUI()
    {
        // check connection type and screen printout
        if (testStatus != "")
        {
            GUILayout.Label(testStatus);
        }
        if (!doneTesting)
        {
            TestConnection();
        }
        else if (NATmode != -1)                 // only carry on if no error
        // if we are connecting to a server
        {
            if (connectingToServer)
            {
                GUILayout.Label("Connecting to server...");

                // if we aren't connecting to a server
            }
            else
            {
                // why does this cause an error? It seems to not like being inside an if inside an if inside an if! :S
                // it's now only an error when removing "Connecting to server..."
                if (GUILayout.Button("Host a server"))
                {
                    //disable menu level preview - "main" doesn't exist if debugin
                    if (GameObject.Find("main"))
                    {
                        GameControl gCtrl = GameObject.Find("main").GetComponent(typeof(GameControl)) as GameControl;
                        //gCtrl.ed_levelPreviewScreen.SetActive(false);
                        gCtrl.hideAllScreens();
                    }
                    else
                    {
                        InputManager.Setup();
                    }
                    // add the server script to the NetObj
                    GameObject.FindWithTag("NetObj").AddComponent <networkManagerServer>();

                    // enable the camera
                    nvs.myCam.gameObject.SetActive(true);

                    // disable this script
                    this.enabled = false;
                }

                if (GUILayout.Button("Refresh server list"))
                {
                    // get them servers
                    MasterServer.ClearHostList();
                    MasterServer.RequestHostList(serverVersion);
                }

                GUILayout.BeginHorizontal();
                GUILayout.Label("IP:");
                directConnectIP = GUILayout.TextField(directConnectIP, 21);
                if (GUILayout.Button("Direct connect") && directConnectIP != "")
                {
                    string remoteIP;
                    int    remotePort = 11177;
                    if (directConnectIP.Contains(":"))
                    {
                        remoteIP = directConnectIP.Split(':')[0];
                        string rip2 = directConnectIP.Split(':')[1];
                        try {
                            remotePort = int.Parse(rip2);
                        } catch {
                            remotePort = 11177;
                        }
                    }
                    else
                    {
                        remoteIP = directConnectIP;
                    }
                    Network.Connect(remoteIP, remotePort);
                    connectingToServer = true;
                    timeoutTime        = 0;
                    showFailMessage    = false;
                }
                GUILayout.EndHorizontal();


                // HACKY - REPLACE ME!
                GUI.Label(new Rect(Screen.width - 340, 20, 100, 20), "Player name:");
                nvs.myInfo.name = GUI.TextField(new Rect(Screen.width - 200, 20, 180, 20), nvs.myInfo.name, 32);
                GUI.Label(new Rect(Screen.width - 340, 40, 100, 20), "Server name:");
                nvs.serverName = GUI.TextField(new Rect(Screen.width - 200, 40, 180, 20), nvs.serverName, 32);



                // AND THIS BIT ASWELL
                HostData[] data = MasterServer.PollHostList();
                // Go through all the hosts in the host list
                foreach (HostData element in data)
                {
                    // get the server data from its comment
                    ServerComment hostParams = new ServerComment(element.comment);
                    // only show the server if it's possible to connect to it
                    if (hostParams.NATmode + nvs.NATmode <= 2)
                    {
                        GUILayout.BeginHorizontal();
                        if (hostParams.locked)                                  // don't use element.passwordProtected since it lies
                        {
                            GUILayout.Label("Locked");
                        }
                        else
                        {
                            GUILayout.Label("");
                        }
                        GUILayout.Space(5);
                        string name = element.gameName;
                        GUILayout.Label(name);
                        GUILayout.Space(5);
                        GUILayout.Label(element.connectedPlayers + " / " + element.playerLimit);
                        //string hostInfo = "[";
                        //foreach (var host in element.ip) hostInfo = hostInfo + host + ":" + element.port + " ";
                        //hostInfo = hostInfo + "]";
                        //GUILayout.Label(hostInfo);
                        GUILayout.Space(5);
                        GUILayout.Label(hostParams.comment);
                        GUILayout.Space(5);
                        GUILayout.FlexibleSpace();
                        if (!hostParams.locked && GUILayout.Button("Connect"))
                        {
                            // Connect to HostData struct, internally the correct method is used (GUID when using NAT).
                            Network.Connect(element);
                            connectingToServer = true;
                            timeoutTime        = 0;
                            showFailMessage    = false;
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                if (showFailMessage)
                {
                    GUILayout.Label("Failed to connect");
                }
            }
        }

        /*
         * if(GUILayout.Button ("Back")){
         *      //Go back to main menu
         *      string nameOfLevel = "main";
         *      Application.LoadLevel( nameOfLevel );
         * }
         */
    }
    void OnGUI()
    {
        // check connection type and screen printout
        if (testStatus!="") GUILayout.Label(testStatus);
        if (!doneTesting) {
            TestConnection();
        } else if (NATmode!=-1) {	// only carry on if no error
            // if we are connecting to a server
            if (connectingToServer) {
                GUILayout.Label("Connecting to server...");

            // if we aren't connecting to a server
            } else {
                // why does this cause an error? It seems to not like being inside an if inside an if inside an if! :S
                // it's now only an error when removing "Connecting to server..."
                if (GUILayout.Button ("Host a server"))
                {
                    //disable menu level preview - "main" doesn't exist if debugin
                    if(GameObject.Find("main"))
                    {
                        GameControl gCtrl = GameObject.Find("main").GetComponent(typeof(GameControl)) as GameControl;
                        //gCtrl.ed_levelPreviewScreen.SetActive(false);
                        gCtrl.hideAllScreens();
                    } else {
                        InputManager.Setup();
                    }
                    // add the server script to the NetObj
                    GameObject.FindWithTag("NetObj").AddComponent("networkManagerServer");

                    // enable the camera
                    nvs.myCam.gameObject.SetActive(true);

                    // disable this script
                    this.enabled = false;
                }
                if (GUILayout.Button ("Refresh server list"))
                {
                    // get them servers
                    MasterServer.ClearHostList();
                    MasterServer.RequestHostList(serverVersion);
                }

                // HACKY - REPLACE ME!
                GUI.Label(new Rect(Screen.width-340,20,100,20), "Player name:");
                nvs.myInfo.name = GUI.TextField(new Rect(Screen.width-200,20,180,20), nvs.myInfo.name, 32);
                GUI.Label(new Rect(Screen.width-340,40,100,20), "Server name:");
                nvs.serverName = GUI.TextField(new Rect(Screen.width-200,40,180,20), nvs.serverName, 32);

                // AND THIS BIT ASWELL
                HostData[] data = MasterServer.PollHostList();
                // Go through all the hosts in the host list
                foreach (HostData element in data)
                {
                    // get the server data from its comment
                    ServerComment hostParams = new ServerComment(element.comment);
                    // only show the server if it's possible to connect to it
                    if (hostParams.NATmode+nvs.NATmode<=2) {
                        GUILayout.BeginHorizontal();
                        if (hostParams.locked) {	// don't use element.passwordProtected since it lies
                            GUILayout.Label("Locked");
                        } else {
                            GUILayout.Label("");
                        }
                        GUILayout.Space(5);
                        string name = element.gameName;
                        GUILayout.Label(name);
                        GUILayout.Space(5);
                        GUILayout.Label(element.connectedPlayers + " / " + element.playerLimit);
                        //string hostInfo = "[";
                        //foreach (var host in element.ip) hostInfo = hostInfo + host + ":" + element.port + " ";
                        //hostInfo = hostInfo + "]";
                        //GUILayout.Label(hostInfo);
                        GUILayout.Space(5);
                        GUILayout.Label(hostParams.comment);
                        GUILayout.Space(5);
                        GUILayout.FlexibleSpace();
                        if (!hostParams.locked && GUILayout.Button("Connect"))
                        {
                            // Connect to HostData struct, internally the correct method is used (GUID when using NAT).
                            Network.Connect(element);
                            connectingToServer = true;
                            timeoutTime = 0;
                            showFailMessage = false;
                        }
                        GUILayout.EndHorizontal();
                    }
                }
                if (showFailMessage)
                    GUILayout.Label("Failed to connect");
            }
        }

        /*
        if(GUILayout.Button ("Back")){
            //Go back to main menu
            string nameOfLevel = "main";
            Application.LoadLevel( nameOfLevel );
        }
        */
    }