RigidbodyBenchmarkNetworkManager.cs 1.6 KB

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