Ejemplo n.º 1
0
        async UniTask StartClient(int i, string networkAddress)
        {
            var                 clientGo            = new GameObject($"Client {i}", typeof(NetworkClient), typeof(ClientObjectManager), typeof(PlayerSpawner), typeof(NetworkSceneManager));
            NetworkClient       client              = clientGo.GetComponent <NetworkClient>();
            ClientObjectManager objectManager       = clientGo.GetComponent <ClientObjectManager>();
            PlayerSpawner       spawner             = clientGo.GetComponent <PlayerSpawner>();
            NetworkSceneManager networkSceneManager = clientGo.GetComponent <NetworkSceneManager>();

            networkSceneManager.Client = client;

            objectManager.Client = client;
            objectManager.NetworkSceneManager = networkSceneManager;
            objectManager.Start();
            objectManager.RegisterPrefab(MonsterPrefab.GetComponent <NetworkIdentity>());
            objectManager.RegisterPrefab(PlayerPrefab.GetComponent <NetworkIdentity>());

            spawner.Client              = client;
            spawner.PlayerPrefab        = PlayerPrefab.GetComponent <NetworkIdentity>();
            spawner.ClientObjectManager = objectManager;
            spawner.SceneManager        = networkSceneManager;

            client.Transport = transport;

            try
            {
                await client.ConnectAsync(networkAddress);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }
Ejemplo n.º 2
0
        void StartClient(int i, string networkAddress)
        {
            GameObject clientGo = Instantiate(ClientPrefab);

            clientGo.name = $"Client {i}";
            NetworkClient       client              = clientGo.GetComponent <NetworkClient>();
            ClientObjectManager objectManager       = clientGo.GetComponent <ClientObjectManager>();
            CharacterSpawner    spawner             = clientGo.GetComponent <CharacterSpawner>();
            NetworkSceneManager networkSceneManager = clientGo.GetComponent <NetworkSceneManager>();

            networkSceneManager.Client = client;

            objectManager.Client = client;
            objectManager.NetworkSceneManager = networkSceneManager;
            objectManager.Start();
            objectManager.RegisterPrefab(MonsterPrefab.GetComponent <NetworkIdentity>());
            objectManager.RegisterPrefab(PlayerPrefab.GetComponent <NetworkIdentity>());

            spawner.Client              = client;
            spawner.PlayerPrefab        = PlayerPrefab.GetComponent <NetworkIdentity>();
            spawner.ClientObjectManager = objectManager;
            spawner.SceneManager        = networkSceneManager;

            client.SocketFactory = socketFactory;

            try
            {
                client.Connect(networkAddress);
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }
Ejemplo n.º 3
0
        private IEnumerator StartClient(int i, SocketFactory socketFactory)
        {
            var                 clientGo      = new GameObject($"Client {i}", typeof(NetworkClient), typeof(ClientObjectManager));
            NetworkClient       client        = clientGo.GetComponent <NetworkClient>();
            ClientObjectManager objectManager = clientGo.GetComponent <ClientObjectManager>();

            objectManager.Client = client;
            objectManager.Start();
            client.SocketFactory = socketFactory;

            objectManager.RegisterPrefab(MonsterPrefab);
            client.Connect("localhost");
            while (!client.IsConnected)
            {
                yield return(null);
            }
        }
Ejemplo n.º 4
0
        private IEnumerator StartClient(int i, Transport transport)
        {
            var                 clientGo      = new GameObject($"Client {i}", typeof(NetworkClient), typeof(ClientObjectManager));
            NetworkClient       client        = clientGo.GetComponent <NetworkClient>();
            ClientObjectManager objectManager = clientGo.GetComponent <ClientObjectManager>();

            objectManager.client = client;
            objectManager.Start();
            client.Transport = transport;

            objectManager.RegisterPrefab(monsterPrefab);
            client.ConnectAsync("localhost");
            while (!client.IsConnected)
            {
                yield return(null);
            }
        }
Ejemplo n.º 5
0
        async UniTask StartClient(int i, Transport transport, string networkAddress)
        {
            var                 clientGo      = new GameObject($"Client {i}", typeof(NetworkClient), typeof(ClientObjectManager));
            NetworkClient       client        = clientGo.GetComponent <NetworkClient>();
            ClientObjectManager objectManager = clientGo.GetComponent <ClientObjectManager>();

            objectManager.client = client;
            objectManager.Start();
            client.Transport = transport;

            objectManager.RegisterPrefab(MonsterPrefab.GetComponent <NetworkIdentity>());
            objectManager.RegisterPrefab(PlayerPrefab.GetComponent <NetworkIdentity>());

            try
            {
                await client.ConnectAsync(networkAddress);

                client.Send(new AddPlayerMessage());
            }
            catch (Exception ex)
            {
                Debug.LogException(ex);
            }
        }
Ejemplo n.º 6
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.Players.Count > 0);

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

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

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

            clientIdentity  = connectionToServer.Identity;
            clientPlayerGO  = clientIdentity.gameObject;
            clientComponent = clientPlayerGO.GetComponent <T>();
        });
Ejemplo n.º 7
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));
            socketFactory = serverGo.AddComponent <TestSocketFactory>();

            await UniTask.Delay(1);

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

            if (ServerConfig != null)
            {
                server.PeerConfig = ServerConfig;
            }
            if (ClientConfig != null)
            {
                client.PeerConfig = ClientConfig;
            }

            server.SocketFactory = socketFactory;
            client.SocketFactory = socketFactory;

            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("player (unspawned)", typeof(NetworkIdentity), typeof(T));
            NetworkIdentity identity = playerPrefab.GetComponent <NetworkIdentity>();
            identity.PrefabHash      = Guid.NewGuid().GetHashCode();
            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.StartServer();

            await started.Task;

            if (AutoConnectClient)
            {
                // now start the client
                client.Connect("localhost");

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

                // get the connections so that we can spawn players
                serverPlayer = server.Players.First();
                clientPlayer = client.Player;

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

                // wait for client to spawn it
                await AsyncUtil.WaitUntilWithTimeout(() => clientPlayer.HasCharacter);

                clientIdentity      = clientPlayer.Identity;
                clientPlayerGO      = clientIdentity.gameObject;
                clientPlayerGO.name = "player (client)";
                clientComponent     = clientPlayerGO.GetComponent <T>();
            }

            await LateSetup();
        });