BenchmarkNetworkManager.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using UnityEngine;
  2. namespace Mirror.Examples.Benchmark
  3. {
  4. public class BenchmarkNetworkManager : NetworkManager
  5. {
  6. [Header("Spawns")]
  7. public GameObject spawnPrefab;
  8. public int spawnAmount = 5000;
  9. public float interleave = 1;
  10. void SpawnAll()
  11. {
  12. // calculate sqrt so we can spawn N * N = Amount
  13. float sqrt = Mathf.Sqrt(spawnAmount);
  14. // calculate spawn xz start positions
  15. // based on spawnAmount * distance
  16. float offset = -sqrt / 2 * interleave;
  17. // spawn exactly the amount, not one more.
  18. int spawned = 0;
  19. for (int spawnX = 0; spawnX < sqrt; ++spawnX)
  20. {
  21. for (int spawnZ = 0; spawnZ < sqrt; ++spawnZ)
  22. {
  23. // spawn exactly the amount, not any more
  24. // (our sqrt method isn't 100% precise)
  25. if (spawned < spawnAmount)
  26. {
  27. // instantiate & position
  28. GameObject go = Instantiate(spawnPrefab);
  29. float x = offset + spawnX * interleave;
  30. float z = offset + spawnZ * interleave;
  31. go.transform.position = new Vector3(x, 0, z);
  32. // spawn
  33. NetworkServer.Spawn(go);
  34. ++spawned;
  35. }
  36. }
  37. }
  38. }
  39. public override void OnStartServer()
  40. {
  41. base.OnStartServer();
  42. SpawnAll();
  43. }
  44. }
  45. }