public IEnumerator SetupHost() => UniTask.ToCoroutine(async() =>
        {
            networkManagerGo = new GameObject();
            networkManagerGo.AddComponent <MockTransport>();
            sceneManager        = networkManagerGo.AddComponent <NetworkSceneManager>();
            serverObjectManager = networkManagerGo.AddComponent <ServerObjectManager>();
            clientObjectManager = networkManagerGo.AddComponent <ClientObjectManager>();
            manager             = networkManagerGo.AddComponent <NetworkManager>();
            manager.client      = networkManagerGo.GetComponent <NetworkClient>();
            manager.server      = networkManagerGo.GetComponent <NetworkServer>();
            server = manager.server;
            client = manager.client;
            sceneManager.client        = client;
            sceneManager.server        = server;
            serverObjectManager.server = server;
            serverObjectManager.networkSceneManager = sceneManager;
            clientObjectManager.client = client;
            clientObjectManager.networkSceneManager = sceneManager;

            ExtraSetup();

            // wait for all Start() methods to get invoked
            await UniTask.DelayFrame(1);

            await StartHost();

            playerGO  = new GameObject("playerGO", typeof(Rigidbody));
            identity  = playerGO.AddComponent <NetworkIdentity>();
            component = playerGO.AddComponent <T>();

            serverObjectManager.AddPlayerForConnection(server.LocalConnection, playerGO);

            client.Update();
        });
Exemple #2
0
        private void OnCreatePlayer(INetworkConnection connection, CreatePlayerMessage createPlayerMessage)
        {
            // create a gameobject using the name supplied by client
            GameObject playergo = Instantiate(playerPrefab).gameObject;

            playergo.GetComponent <Player>().playerName = createPlayerMessage.name;

            // set it as the player
            ServerObjectManager.AddPlayerForConnection(connection, playergo);

            chatWindow.gameObject.SetActive(true);
        }
Exemple #3
0
        public override void OnServerAddPlayer(INetworkConnection conn)
        {
            // add player at correct spawn position
            Transform       start  = Server.NumPlayers == 0 ? leftRacketSpawn : rightRacketSpawn;
            NetworkIdentity player = Instantiate(PlayerPrefab, start.position, start.rotation);

            ServerObjectManager.AddPlayerForConnection(conn, player.gameObject);

            // spawn ball if two players
            if (Server.NumPlayers == 2)
            {
                ball = Instantiate(ballPrefab);
                ServerObjectManager.Spawn(ball);
            }
        }
Exemple #4
0
        public IEnumerator SetupHost() => UniTask.ToCoroutine(async() =>
        {
            networkManagerGo = new GameObject();
            // set gameobject name to test name (helps with debugging)
            networkManagerGo.name = TestContext.CurrentContext.Test.MethodName;

            networkManagerGo.AddComponent <MockTransport>();
            sceneManager        = networkManagerGo.AddComponent <NetworkSceneManager>();
            serverObjectManager = networkManagerGo.AddComponent <ServerObjectManager>();
            clientObjectManager = networkManagerGo.AddComponent <ClientObjectManager>();
            manager             = networkManagerGo.AddComponent <NetworkManager>();
            manager.Client      = networkManagerGo.GetComponent <NetworkClient>();
            manager.Server      = networkManagerGo.GetComponent <NetworkServer>();
            server = manager.Server;
            client = manager.Client;
            sceneManager.Client        = client;
            sceneManager.Server        = server;
            serverObjectManager.Server = server;
            serverObjectManager.NetworkSceneManager = sceneManager;
            clientObjectManager.Client = client;
            clientObjectManager.NetworkSceneManager = sceneManager;

            ExtraSetup();

            // wait for all Start() methods to get invoked
            await UniTask.DelayFrame(1);

            await StartHost();

            playerGO  = new GameObject("playerGO", typeof(Rigidbody));
            identity  = playerGO.AddComponent <NetworkIdentity>();
            component = playerGO.AddComponent <T>();

            serverObjectManager.AddPlayerForConnection(server.LocalConnection, playerGO);

            // wait for client to spawn it
            await AsyncUtil.WaitUntilWithTimeout(() => client.Connection.Identity != null);
        });
Exemple #5
0
        public IEnumerator Setup() => UniTask.ToCoroutine(async() =>
        {
            serverGo      = new GameObject("server", typeof(NetworkSceneManager), typeof(ServerObjectManager), typeof(NetworkServer));
            clientGo      = new GameObject("client", typeof(NetworkSceneManager), typeof(ClientObjectManager), typeof(NetworkClient));
            testTransport = serverGo.AddComponent <LoopbackTransport>();

            await UniTask.Delay(1);

            server = serverGo.GetComponent <NetworkServer>();
            client = clientGo.GetComponent <NetworkClient>();

            server.Transport = testTransport;
            client.Transport = testTransport;

            serverSceneManager        = serverGo.GetComponent <NetworkSceneManager>();
            clientSceneManager        = clientGo.GetComponent <NetworkSceneManager>();
            serverSceneManager.Server = server;
            clientSceneManager.Client = client;
            serverSceneManager.Start();
            clientSceneManager.Start();

            serverObjectManager        = serverGo.GetComponent <ServerObjectManager>();
            serverObjectManager.Server = server;
            serverObjectManager.NetworkSceneManager = serverSceneManager;
            serverObjectManager.Start();

            clientObjectManager        = clientGo.GetComponent <ClientObjectManager>();
            clientObjectManager.Client = client;
            clientObjectManager.NetworkSceneManager = clientSceneManager;
            clientObjectManager.Start();

            ExtraSetup();

            // create and register a prefab
            playerPrefab             = new GameObject("serverPlayer", typeof(NetworkIdentity), typeof(T));
            NetworkIdentity identity = playerPrefab.GetComponent <NetworkIdentity>();
            identity.AssetId         = Guid.NewGuid();
            clientObjectManager.RegisterPrefab(identity);

            // wait for client and server to initialize themselves
            await UniTask.Delay(1);

            // start the server
            var started = new UniTaskCompletionSource();
            server.Started.AddListener(() => started.TrySetResult());
            server.ListenAsync().Forget();

            await started.Task;

            // now start the client
            await client.ConnectAsync("localhost");

            await AsyncUtil.WaitUntilWithTimeout(() => server.connections.Count > 0);

            // get the connections so that we can spawn players
            connectionToClient = server.connections.First();
            connectionToServer = client.Connection;

            // create a player object in the server
            serverPlayerGO  = Object.Instantiate(playerPrefab);
            serverIdentity  = serverPlayerGO.GetComponent <NetworkIdentity>();
            serverComponent = serverPlayerGO.GetComponent <T>();
            serverObjectManager.AddPlayerForConnection(connectionToClient, serverPlayerGO);

            // wait for client to spawn it
            await AsyncUtil.WaitUntilWithTimeout(() => connectionToServer.Identity != null);

            clientIdentity  = connectionToServer.Identity;
            clientPlayerGO  = clientIdentity.gameObject;
            clientComponent = clientPlayerGO.GetComponent <T>();
        });