Exemple #1
0
        public IEnumerator SetUp() => UniTask.ToCoroutine(async() =>
        {
#if UNITY_EDITOR
            await EditorSceneManager.LoadSceneAsyncInPlayMode(ScenePath, new LoadSceneParameters {
                loadSceneMode = LoadSceneMode.Additive
            });
#else
            throw new System.NotSupportedException("Test not supported in player");
#endif
            var scene = SceneManager.GetSceneByPath(ScenePath);
            SceneManager.SetActiveScene(scene);

#if UNITY_EDITOR
            MonsterPrefab = AssetDatabase.LoadAssetAtPath <NetworkIdentity>(MonsterPath);
#else
            throw new System.NotSupportedException("Test not supported in player");
#endif

            // load host
            Server = Object.FindObjectOfType <NetworkServer>();
            ServerObjectManager = Object.FindObjectOfType <ServerObjectManager>();

            Server.Authenticated.AddListener(conn => ServerObjectManager.SpawnVisibleObjects(conn, true));

            var started = new UniTaskCompletionSource();
            Server.Started.AddListener(() => started.TrySetResult());

            // wait 1 frame before Starting server to give time for Unity to call "Start"
            await UniTask.Yield();
            Server.StartServer();

            await started.Task;

            socketFactory = Server.GetComponent <SocketFactory>();
            Debug.Assert(socketFactory != null, "Could not find socket factory for test");

            // connect from a bunch of clients
            for (var i = 0; i < ClientCount; i++)
            {
                await StartClient(i, socketFactory);
            }

            // spawn a bunch of monsters
            for (var i = 0; i < MonsterCount; i++)
            {
                SpawnMonster(i);
            }

            while (Object.FindObjectsOfType <MonsterBehavior>().Count() < MonsterCount * (ClientCount + 1))
            {
                await UniTask.Delay(10);
            }
        });