BenchmarkIdleNetworkManager.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using UnityEngine;
  2. namespace Mirror.Examples.BenchmarkIdle
  3. {
  4. [AddComponentMenu("")]
  5. public class BenchmarkIdleNetworkManager : NetworkManager
  6. {
  7. [Header("Spawns")]
  8. public int spawnAmount = 10_000;
  9. public float interleave = 1;
  10. public GameObject spawnPrefab;
  11. // player spawn positions should be spread across the world.
  12. // not all at one place.
  13. // but _some_ at the same place.
  14. // => deterministic random is ideal
  15. [Range(0, 1)] public float spawnPositionRatio = 0.01f;
  16. System.Random random = new System.Random(42);
  17. void SpawnAll()
  18. {
  19. // clear previous player spawn positions in case we start twice
  20. foreach (Transform position in startPositions)
  21. Destroy(position.gameObject);
  22. startPositions.Clear();
  23. // calculate sqrt so we can spawn N * N = Amount
  24. float sqrt = Mathf.Sqrt(spawnAmount);
  25. // calculate spawn xz start positions
  26. // based on spawnAmount * distance
  27. float offset = -sqrt / 2 * interleave;
  28. // spawn exactly the amount, not one more.
  29. int spawned = 0;
  30. for (int spawnX = 0; spawnX < sqrt; ++spawnX)
  31. {
  32. for (int spawnZ = 0; spawnZ < sqrt; ++spawnZ)
  33. {
  34. // spawn exactly the amount, not any more
  35. // (our sqrt method isn't 100% precise)
  36. if (spawned < spawnAmount)
  37. {
  38. // spawn & position
  39. GameObject go = Instantiate(spawnPrefab);
  40. float x = offset + spawnX * interleave;
  41. float z = offset + spawnZ * interleave;
  42. Vector3 position = new Vector3(x, 0, z);
  43. go.transform.position = position;
  44. // spawn
  45. NetworkServer.Spawn(go);
  46. ++spawned;
  47. // add random spawn position for players.
  48. // don't have them all in the same place.
  49. if (random.NextDouble() <= spawnPositionRatio)
  50. {
  51. GameObject spawnGO = new GameObject("Spawn");
  52. spawnGO.transform.position = position;
  53. spawnGO.AddComponent<NetworkStartPosition>();
  54. }
  55. }
  56. }
  57. }
  58. }
  59. // overwrite random spawn position selection:
  60. // - needs to be deterministic so every CCU test results in the same
  61. // - needs to be random so not only are the spawn positions spread out
  62. // randomly, we also have a random amount of players per spawn position
  63. public override Transform GetStartPosition()
  64. {
  65. // first remove any dead transforms
  66. startPositions.RemoveAll(t => t == null);
  67. if (startPositions.Count == 0)
  68. return null;
  69. // pick a random one
  70. int index = random.Next(0, startPositions.Count); // DETERMINISTIC
  71. return startPositions[index];
  72. }
  73. public override void OnStartServer()
  74. {
  75. base.OnStartServer();
  76. SpawnAll();
  77. // disable rendering on server to reduce noise in profiling.
  78. // keep enabled in host mode though.
  79. if (mode == NetworkManagerMode.ServerOnly)
  80. Camera.main.enabled = false;
  81. }
  82. }
  83. }