NetworkManagerPredictionBenchmark.cs 1.6 KB
Newer Older
cann-alberto's avatar
cann-alberto committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using UnityEngine;

namespace Mirror.Examples.PredictionBenchmark
{
    [AddComponentMenu("")]
    public class NetworkManagerPredictionBenchmark : NetworkManager
    {
        [Header("Spawns")]
        public int spawnAmount = 1000;
        public GameObject spawnPrefab;
        public Bounds spawnArea = new Bounds(new Vector3(0, 2.5f, 0), new Vector3(10f, 5f, 10f));

        public override void Awake()
        {
            base.Awake();

            // ensure vsync is disabled for the benchmark, otherwise results are capped
            QualitySettings.vSyncCount = 0;
        }

        void SpawnAll()
        {
            // spawn randomly inside the cage
            for (int i = 0; i < spawnAmount; ++i)
            {
                // choose a random point within the cage
                float x = Random.Range(spawnArea.min.x, spawnArea.max.x);
                float y = Random.Range(spawnArea.min.y, spawnArea.max.y);
                float z = Random.Range(spawnArea.min.z, spawnArea.max.z);
                Vector3 position = new Vector3(x, y, z);

                // spawn & position
                GameObject go = Instantiate(spawnPrefab);
                go.transform.position = position;
                NetworkServer.Spawn(go);
            }
        }

        public override void OnStartServer()
        {
            base.OnStartServer();
            SpawnAll();

            // disable rendering on server to reduce noise in profiling.
            // keep enabled in host mode though.
            if (mode == NetworkManagerMode.ServerOnly)
                Camera.main.enabled = false;
        }
    }
}