This commit is contained in:
2025-06-16 15:14:23 +02:00
committed by devbeni
parent 60fe4620ff
commit 4ff561284f
3174 changed files with 428263 additions and 0 deletions

View File

@ -0,0 +1,86 @@
using System;
using UnityEngine;
namespace Mirror.Examples.Hex2D
{
[AddComponentMenu("")]
[RequireComponent(typeof(HexSpatialHash2DInterestManagement))]
public class Hex2DNetworkManager : NetworkManager
{
// Overrides the base singleton so we dont have to cast to this type everywhere.
public static new Hex2DNetworkManager singleton => (Hex2DNetworkManager)NetworkManager.singleton;
[Header("Spawns")]
public GameObject spawnPrefab;
[Range(1, 3000), Tooltip("Number of prefabs to spawn in a flat 2D grid across the scene.")]
public ushort spawnPrefabsCount = 1000;
[Range(1, 10), Tooltip("Spacing between grid points in meters.")]
public byte spawnPrefabSpacing = 3;
[Header("Diagnostics")]
[ReadOnly, SerializeField] HexSpatialHash2DInterestManagement hexSpatialHash2DInterestManagement;
public override void OnValidate()
{
if (Application.isPlaying) return;
base.OnValidate();
if (hexSpatialHash2DInterestManagement == null)
hexSpatialHash2DInterestManagement = GetComponent<HexSpatialHash2DInterestManagement>();
}
public override void OnStartClient()
{
NetworkClient.RegisterPrefab(spawnPrefab);
}
public override void OnStartServer()
{
// Instantiate an empty GameObject to parent spawns
GameObject spawns = new GameObject("Spawns");
Transform spawnsTransform = spawns.transform;
int spawned = 0;
// Spawn prefabs in a 2D grid centered around origin (0,0,0)
int gridSize = (int)Mathf.Sqrt(spawnPrefabsCount); // Square grid size based on count
// Calculate the starting position to center the grid at (0,0,0)
float halfGrid = (gridSize - 1) * spawnPrefabSpacing * 0.5f;
float startX = -halfGrid;
float startZorY = -halfGrid; // Z for XZ, Y for XY
//Debug.Log($"Start Positions: X={startX}, Z/Y={startZorY}, gridSize={gridSize}");
// Use a 2D loop for a flat grid
for (int x = 0; x < gridSize && spawned < spawnPrefabsCount; ++x)
{
for (int zOrY = 0; zOrY < gridSize && spawned < spawnPrefabsCount; ++zOrY)
{
Vector3 position = Vector3.zero;
if (hexSpatialHash2DInterestManagement.checkMethod == HexSpatialHash2DInterestManagement.CheckMethod.XZ_FOR_3D)
{
float xPos = startX + x * spawnPrefabSpacing;
float zPos = startZorY + zOrY * spawnPrefabSpacing;
position = new Vector3(xPos, 0.5f, zPos);
}
else // XY_FOR_2D
{
float xPos = startX + x * spawnPrefabSpacing;
float yPos = startZorY + zOrY * spawnPrefabSpacing;
position = new Vector3(xPos, yPos, -0.5f);
}
GameObject instance = Instantiate(spawnPrefab, position, Quaternion.identity, spawnsTransform);
NetworkServer.Spawn(instance);
++spawned;
}
}
//Debug.Log($"Spawned {spawned} objects in a {gridSize}x{gridSize} 2D grid.");
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 43d8e3a03523cba438aea0b8e793b390
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/HexSpatialHash/Scripts/Hex2DNetworkManager.cs
uploadId: 736421

View File

@ -0,0 +1,49 @@
using UnityEngine;
namespace Mirror.Examples.Hex2D
{
[AddComponentMenu("")]
public class Hex2DPlayer : NetworkBehaviour
{
[Range(1, 20)]
public float speed = 15f;
[Header("Diagnostics")]
[ReadOnly, SerializeField] HexSpatialHash2DInterestManagement.CheckMethod checkMethod;
void Awake()
{
#if UNITY_2022_2_OR_NEWER
checkMethod = FindAnyObjectByType<HexSpatialHash2DInterestManagement>().checkMethod;
#else
checkMethod = FindObjectOfType<HexSpatialHash2DInterestManagement>().checkMethod;
#endif
}
void Update()
{
if (!isLocalPlayer) return;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 dir;
if (checkMethod == HexSpatialHash2DInterestManagement.CheckMethod.XY_FOR_2D)
dir = new Vector3(h, v, 0);
else
dir = new Vector3(h, 0, v);
transform.position += dir.normalized * (Time.deltaTime * speed);
}
void OnGUI()
{
if (isLocalPlayer)
{
GUILayout.BeginArea(new Rect(10, Screen.height - 25, 300, 300));
GUILayout.Label("Use WASD to move");
GUILayout.EndArea();
}
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: c71d1d9ad0bdf3d498e9caac7d172f56
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/HexSpatialHash/Scripts/Hex2DPlayer.cs
uploadId: 736421

View File

@ -0,0 +1,95 @@
using UnityEngine;
using UnityEngine.SceneManagement;
// This sets up the scene camera for the local player
namespace Mirror.Examples.Hex2D
{
[AddComponentMenu("")]
[DisallowMultipleComponent]
public class Hex2DPlayerCamera : NetworkBehaviour
{
Camera mainCam;
public Vector3 offset = new Vector3(0f, 40f, -65f);
public Vector3 rotation = new Vector3(35f, 0f, 0f);
[Header("Diagnostics")]
[ReadOnly, SerializeField] HexSpatialHash2DInterestManagement.CheckMethod checkMethod;
void Awake()
{
mainCam = Camera.main;
#if UNITY_2022_2_OR_NEWER
checkMethod = FindAnyObjectByType<HexSpatialHash2DInterestManagement>().checkMethod;
#else
checkMethod = FindObjectOfType<HexSpatialHash2DInterestManagement>().checkMethod;
#endif
}
public override void OnStartLocalPlayer()
{
if (mainCam != null)
{
// configure and make camera a child of player with 3rd person offset
mainCam.transform.SetParent(transform);
if (checkMethod == HexSpatialHash2DInterestManagement.CheckMethod.XY_FOR_2D)
{
mainCam.orthographic = true;
mainCam.transform.localPosition = new Vector3(0, 0, -5f);
mainCam.transform.localEulerAngles = Vector3.zero;
}
else
{
mainCam.orthographic = false;
mainCam.transform.localPosition = offset;
mainCam.transform.localEulerAngles = rotation;
}
}
else
Debug.LogWarning("PlayerCamera: Could not find a camera in scene with 'MainCamera' tag.");
}
void OnApplicationQuit()
{
//Debug.Log("PlayerCamera.OnApplicationQuit");
ReleaseCamera();
}
public override void OnStopLocalPlayer()
{
//Debug.Log("PlayerCamera.OnStopLocalPlayer");
ReleaseCamera();
}
void OnDisable()
{
//Debug.Log("PlayerCamera.OnDisable");
ReleaseCamera();
}
void OnDestroy()
{
//Debug.Log("PlayerCamera.OnDestroy");
ReleaseCamera();
}
void ReleaseCamera()
{
if (mainCam != null && mainCam.transform.parent == transform)
{
//Debug.Log("PlayerCamera.ReleaseCamera");
mainCam.transform.SetParent(null);
mainCam.orthographic = true;
mainCam.orthographicSize = 15f;
mainCam.transform.localPosition = new Vector3(0f, 70f, 0f);
mainCam.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
if (mainCam.gameObject.scene != SceneManager.GetActiveScene())
SceneManager.MoveGameObjectToScene(mainCam.gameObject, SceneManager.GetActiveScene());
}
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 8bd7ffa7c966c0c47be237edb68c98c8
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/HexSpatialHash/Scripts/Hex2DPlayerCamera.cs
uploadId: 736421

View File

@ -0,0 +1,70 @@
using System;
using UnityEngine;
namespace Mirror.Examples.Hex3D
{
[AddComponentMenu("")]
public class Hex3DNetworkManager : NetworkManager
{
// Overrides the base singleton so we don't have to cast to this type everywhere.
public static new Hex3DNetworkManager singleton => (Hex3DNetworkManager)NetworkManager.singleton;
[Header("Spawns")]
public GameObject spawnPrefab;
[Range(1, 8000)]
public ushort spawnPrefabsCount = 1000;
[Range(1, 10)]
public byte spawnPrefabSpacing = 3;
public override void OnValidate()
{
if (Application.isPlaying) return;
base.OnValidate();
// Adjust spawnPrefabsCount to have an even cube root
ushort cubeRoot = (ushort)Mathf.Pow(spawnPrefabsCount, 1f / 3f);
spawnPrefabsCount = (ushort)(Mathf.Pow(cubeRoot, 3f));
}
public override void OnStartClient()
{
NetworkClient.RegisterPrefab(spawnPrefab);
}
public override void OnStartServer()
{
// instantiate an empty GameObject
GameObject Spawns = new GameObject("Spawns");
Transform SpawnsTransform = Spawns.transform;
int spawned = 0;
// Spawn prefabs in a cube grid centered around origin (0,0,0)
float cubeRoot = Mathf.Pow(spawnPrefabsCount, 1f / 3f);
int gridSize = Mathf.RoundToInt(cubeRoot);
// Calculate the starting position to center the grid
float startX = -(gridSize - 1) * spawnPrefabSpacing * 0.5f;
float startY = -(gridSize - 1) * spawnPrefabSpacing * 0.5f;
float startZ = -(gridSize - 1) * spawnPrefabSpacing * 0.5f;
//Debug.Log($"Start Positions: X={startX}, Y={startY}, Z={startZ}, gridSize={gridSize}");
for (int x = 0; x < gridSize; ++x)
for (int y = 0; y < gridSize; ++y)
for (int z = 0; z < gridSize; ++z)
if (spawned < spawnPrefabsCount)
{
float x1 = startX + x * spawnPrefabSpacing;
float y1 = startY + y * spawnPrefabSpacing;
float z1 = startZ + z * spawnPrefabSpacing;
Vector3 position = new Vector3(x1, y1, z1);
NetworkServer.Spawn(Instantiate(spawnPrefab, position, Quaternion.identity, SpawnsTransform));
++spawned;
}
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: 3291bd2e9ac86c046bb768d598cd0a3f
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/HexSpatialHash/Scripts/Hex3DNetworkManager.cs
uploadId: 736421

View File

@ -0,0 +1,47 @@
using UnityEngine;
using Mirror;
namespace Mirror.Examples.Hex3D
{
[AddComponentMenu("")]
public class Hex3DPlayer : NetworkBehaviour
{
[Range(1, 20)]
public float speed = 10;
void Update()
{
if (!isLocalPlayer) return;
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
// if left shift is held, apply v to y instead of z
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
Vector3 dir = new Vector3(h, v, 0);
transform.position += dir.normalized * (Time.deltaTime * speed);
}
else
{
Vector3 dir = new Vector3(h, 0, v);
transform.position += dir.normalized * (Time.deltaTime * speed);
}
if (Input.GetKey(KeyCode.Q))
transform.Rotate(Vector3.up, -90 * Time.deltaTime);
if (Input.GetKey(KeyCode.E))
transform.Rotate(Vector3.up, 90 * Time.deltaTime);
}
void OnGUI()
{
if (isLocalPlayer)
{
GUILayout.BeginArea(new Rect(10, Screen.height - 50, 300, 300));
GUILayout.Label("Use WASD+QE to move and rotate\nHold Shift with W/S to move up/down");
GUILayout.EndArea();
}
}
}
}

View File

@ -0,0 +1,18 @@
fileFormatVersion: 2
guid: df2e991210804be44b9ab6b7d6de4d52
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/HexSpatialHash/Scripts/Hex3DPlayer.cs
uploadId: 736421