Пример #1
0
    // Draw the GUI
    private void OnGUI()
    {
        if (!isHost && !isClient)
        {
            // Host button
            if (GUI.Button(new Rect(10, 10, 100, 30), "Host"))
            {
                isHost   = true;
                isClient = false;

                networkManager.StartHost();
            }

            // Client button
            if (GUI.Button(new Rect(10, 50, 100, 30), "Client"))
            {
                networkManager.InitClient();
                isHost   = false;
                isClient = true;
            }
        }
        else
        {
            // Host or client GUI
            if (isHost)
            {
                GUIHost();
            }
            else if (isClient)
            {
                GUIClient();
            }
        }
    }
Пример #2
0
 public async Task CreateMatch(string matchName, int maxPlayers)
 {
     _createMatchTask = new TaskCompletionSource();
     _matchName       = matchName;
     _maxPlayers      = maxPlayers;
     _networkManager.StartHost();
     await _createMatchTask.Task;
 }
Пример #3
0
    /// <summary>
    /// Tries to start a server.
    /// </summary>
    /// <param name="lanOnly">Wheter the server should be lan only.</param>
    public static bool TryStartServer(bool lanOnly)
    {
        NobleNetworkManager networkManager = (NobleNetworkManager)NetworkManager.singleton;

        // Try to start server. If the port is already in use, catch the error and try a different one.
        int startPort = networkManager.networkPort;

        for (int i = 0; i < 5; i++)
        {
            try
            {
                networkManager.networkPort = startPort + i;

                if (lanOnly)
                {
                    networkManager.StartHostLANOnly();
                }
                else
                {
                    networkManager.StartHost();
                }

                return(true);
            }
            catch (SocketException se)
            {
                if (se.SocketErrorCode == SocketError.AddressAlreadyInUse)
                {
                    continue;
                }

                // unknown error
                Debug.LogException(se);
            }
        }

        return(false);
    }
Пример #4
0
 public void HostGame()
 {
     networkManager.StartHost();
     isHost = true;
 }