aha
This commit is contained in:
@ -0,0 +1,52 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Benchmark
|
||||
{
|
||||
[AddComponentMenu("")]
|
||||
public class BenchmarkNetworkManager : NetworkManager
|
||||
{
|
||||
[Header("Spawns")]
|
||||
public GameObject spawnPrefab;
|
||||
public int spawnAmount = 5000;
|
||||
public float interleave = 1;
|
||||
|
||||
void SpawnAll()
|
||||
{
|
||||
// calculate sqrt so we can spawn N * N = Amount
|
||||
float sqrt = Mathf.Sqrt(spawnAmount);
|
||||
|
||||
// calculate spawn xz start positions
|
||||
// based on spawnAmount * distance
|
||||
float offset = -sqrt / 2 * interleave;
|
||||
|
||||
// spawn exactly the amount, not one more.
|
||||
int spawned = 0;
|
||||
for (int spawnX = 0; spawnX < sqrt; ++spawnX)
|
||||
{
|
||||
for (int spawnZ = 0; spawnZ < sqrt; ++spawnZ)
|
||||
{
|
||||
// spawn exactly the amount, not any more
|
||||
// (our sqrt method isn't 100% precise)
|
||||
if (spawned < spawnAmount)
|
||||
{
|
||||
// instantiate & position
|
||||
GameObject go = Instantiate(spawnPrefab);
|
||||
float x = offset + spawnX * interleave;
|
||||
float z = offset + spawnZ * interleave;
|
||||
go.transform.position = new Vector3(x, 0, z);
|
||||
|
||||
// spawn
|
||||
NetworkServer.Spawn(go);
|
||||
++spawned;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
base.OnStartServer();
|
||||
SpawnAll();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f0f6e2c4566084948a433ee5285d3fb7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/Benchmark/Scripts/BenchmarkNetworkManager.cs
|
||||
uploadId: 736421
|
57
Assets/Mirror/Examples/Benchmark/Scripts/MonsterMovement.cs
Normal file
57
Assets/Mirror/Examples/Benchmark/Scripts/MonsterMovement.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Benchmark
|
||||
{
|
||||
public class MonsterMovement : NetworkBehaviour
|
||||
{
|
||||
public float speed = 1;
|
||||
|
||||
// movement probability:
|
||||
// 0.5 is too high, monsters are moving almost all the time.
|
||||
// only-sync-on-change shows no difference with 0.5 at all.
|
||||
// in other words: broken change detection would be too easy to miss!
|
||||
[Header("Note: use 0.1 to test change detection, 0.5 is too high!")]
|
||||
public float movementProbability = 0.1f;
|
||||
public float movementDistance = 20;
|
||||
|
||||
bool moving;
|
||||
Vector3 start;
|
||||
Vector3 destination;
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
start = transform.position;
|
||||
}
|
||||
|
||||
[ServerCallback]
|
||||
void Update()
|
||||
{
|
||||
if (moving)
|
||||
{
|
||||
if (Vector3.Distance(transform.position, destination) <= 0.01f)
|
||||
{
|
||||
transform.position = destination;
|
||||
moving = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
float r = Random.value;
|
||||
if (r < movementProbability * Time.deltaTime)
|
||||
{
|
||||
Vector2 circlePos = Random.insideUnitCircle;
|
||||
Vector3 dir = new Vector3(circlePos.x, 0, circlePos.y);
|
||||
|
||||
// set destination on random pos in a circle around start.
|
||||
// (don't want to wander off)
|
||||
destination = start + dir * movementDistance;
|
||||
moving = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9cddc2e496c474e538a494465be0192a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/Benchmark/Scripts/MonsterMovement.cs
|
||||
uploadId: 736421
|
31
Assets/Mirror/Examples/Benchmark/Scripts/PlayerMovement.cs
Normal file
31
Assets/Mirror/Examples/Benchmark/Scripts/PlayerMovement.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Mirror.Examples.Benchmark
|
||||
{
|
||||
public class PlayerMovement : NetworkBehaviour
|
||||
{
|
||||
public float speed = 5;
|
||||
|
||||
// naming for easier debugging
|
||||
public override void OnStartClient()
|
||||
{
|
||||
name = $"Player[{netId}|{(isLocalPlayer ? "local" : "remote")}]";
|
||||
}
|
||||
|
||||
public override void OnStartServer()
|
||||
{
|
||||
name = $"Player[{netId}|server]";
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!isLocalPlayer) return;
|
||||
|
||||
float h = Input.GetAxis("Horizontal");
|
||||
float v = Input.GetAxis("Vertical");
|
||||
|
||||
Vector3 dir = new Vector3(h, 0, v);
|
||||
transform.position += dir.normalized * (Time.deltaTime * speed);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c482338c8cc6d4a3cba81934c0151972
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
AssetOrigin:
|
||||
serializedVersion: 1
|
||||
productId: 129321
|
||||
packageName: Mirror
|
||||
packageVersion: 96.0.1
|
||||
assetPath: Assets/Mirror/Examples/Benchmark/Scripts/PlayerMovement.cs
|
||||
uploadId: 736421
|
Reference in New Issue
Block a user