示例#1
0
    void OnStartServer()
    {
#if UNITY_WEBPLAYER
        mMessage = "Can't host from the Web Player due to Unity's security restrictions";
#else
        // Start a local server, loading the saved data if possible
        // The UDP port of the server doesn't matter much as it's optional,
        // and the clients get notified of it via Packet.ResponseSetUDP.
        int         udpPort       = Random.Range(10000, 40000);
        LobbyClient lobby         = GetComponent <LobbyClient>();
        var         serverStarted = false;
        if (lobby == null)
        {
            serverStarted = TNServerInstance.Start(serverTcpPort, udpPort, serverFileName);
        }
        else
        {
            TNServerInstance.Type type = (lobby is TNUdpLobbyClient) ?
                                         TNServerInstance.Type.Udp : TNServerInstance.Type.Tcp;
            serverStarted = TNServerInstance.Start(serverTcpPort, udpPort, serverFileName, type, Tools.ResolveEndPoint(lobby.remoteAddress, lobby.remotePort));
//            TNServerInstance.Start(serverTcpPort, udpPort, lobby.remotePort, serverFileName, type);

            var servername = InputDialog.instance.GetValueString(INPUT_SERVERNAME);
            if (!string.IsNullOrEmpty(servername))
            {
                TNServerInstance.serverName = servername;
            }
        }
        if (serverStarted)
        {
            mMessage = "Server started";

            InputDialog.instance.CloseDialog();
        }
#endif
    }
示例#2
0
    /// <summary>
    /// This menu is shown if the client has not yet connected to the server.
    /// </summary>

    void DrawConnectMenu()
    {
        Rect rect = new Rect(Screen.width * 0.5f - 200f * 0.5f - mAlpha * 120f,
                             Screen.height * 0.5f - 100f, 200f, 220f);

        // Show a half-transparent box around the upcoming UI
        GUI.color = new Color(1f, 1f, 1f, 0.5f);
        GUI.Box(UnityTools.PadRect(rect, 8f), "");
        GUI.color = Color.white;

        GUILayout.BeginArea(rect);
        {
            GUILayout.Label("Server Address", text);
            mAddress = GUILayout.TextField(mAddress, input, GUILayout.Width(200f));

            if (GUILayout.Button("Connect", button))
            {
                // We want to connect to the specified destination when the button is clicked on.
                // "OnConnect" function will be called sometime later with the result.
                TNManager.Connect(mAddress);
                mMessage = "Connecting...";
            }

            if (TNServerInstance.isActive)
            {
                GUI.backgroundColor = Color.red;

                if (GUILayout.Button("Stop the Server", button))
                {
                    // Stop the server, saving all the data
                    TNServerInstance.Stop();
                    mMessage = "Server stopped";
                }
            }
            else
            {
                GUI.backgroundColor = Color.green;

                if (GUILayout.Button("Start a LAN Server", button))
                {
#if UNITY_WEBPLAYER
                    mMessage = "Can't host from the Web Player due to Unity's security restrictions";
#else
                    // Start a local server, loading the saved data if possible
                    // The UDP port of the server doesn't matter much as it's optional,
                    // and the clients get notified of it via Packet.ResponseSetUDP.
                    int           udpPort = Random.Range(10000, 40000);
                    TNLobbyClient lobby   = GetComponent <TNLobbyClient>();

                    if (lobby == null)
                    {
                        if (TNServerInstance.Start(serverTcpPort, udpPort, "server.dat"))
                        {
                            TNManager.Connect();
                        }
                    }
                    else
                    {
                        TNServerInstance.Type type = (lobby is TNUdpLobbyClient) ?
                                                     TNServerInstance.Type.Udp : TNServerInstance.Type.Tcp;

                        if (TNServerInstance.Start(serverTcpPort, udpPort, lobby.remotePort, "server.dat", type))
                        {
                            TNManager.Connect();
                        }
                    }
                    mMessage = "Server started";
#endif
                }

                // Start a local server that doesn't use sockets. It's ideal for testing and for single player gameplay.
                if (GUILayout.Button("Start a Virtual Server", button))
                {
                    mMessage = "Server started";
                    TNServerInstance.Start("server.dat");
                    TNManager.Connect();
                }
            }
            GUI.backgroundColor = Color.white;

            if (!string.IsNullOrEmpty(mMessage))
            {
                GUILayout.Label(mMessage, text);
            }
        }
        GUILayout.EndArea();

        if (mAlpha > 0.01f)
        {
            rect.x = rect.x + (Screen.width - rect.xMin - rect.xMax) * mAlpha;
            DrawServerList(rect);
        }
    }