This commit is contained in:
2026-08-04 13:47:07 +03:00
parent 48bc19d79f
commit bca1cbb323
241 changed files with 51945 additions and 0 deletions
@@ -0,0 +1,7 @@
{
"name": "Bootstrap",
"rootNamespace": "Unity.Netcode.Samples",
"references": [
"Unity.Netcode.Runtime"
]
}
@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 219b171db79434558b442780da267dab
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,58 @@
using UnityEngine;
namespace Unity.Netcode.Samples
{
/// <summary>
/// Class to display helper buttons and status labels on the GUI, as well as buttons to start host/client/server.
/// Once a connection has been established to the server, the local player can be teleported to random positions via a GUI button.
/// </summary>
public class BootstrapManager : MonoBehaviour
{
private void OnGUI()
{
GUILayout.BeginArea(new Rect(10, 10, 300, 300));
var networkManager = NetworkManager.Singleton;
if (!networkManager.IsClient && !networkManager.IsServer)
{
if (GUILayout.Button("Host"))
{
networkManager.StartHost();
}
if (GUILayout.Button("Client"))
{
networkManager.StartClient();
}
if (GUILayout.Button("Server"))
{
networkManager.StartServer();
}
}
else
{
GUILayout.Label($"Mode: {(networkManager.IsHost ? "Host" : networkManager.IsServer ? "Server" : "Client")}");
// "Random Teleport" button will only be shown to clients
if (networkManager.IsClient)
{
if (GUILayout.Button("Random Teleport"))
{
if (networkManager.LocalClient != null)
{
// Get `BootstrapPlayer` component from the player's `PlayerObject`
if (networkManager.LocalClient.PlayerObject.TryGetComponent(out BootstrapPlayer bootstrapPlayer))
{
// Invoke a `ServerRpc` from client-side to teleport player to a random position on the server-side
bootstrapPlayer.RandomTeleportServerRpc();
}
}
}
}
}
GUILayout.EndArea();
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5fed568ebf6c14b11928f16219b5675b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,32 @@
using UnityEngine;
namespace Unity.Netcode.Samples
{
/// <summary>
/// Component attached to the "Player Prefab" on the NetworkManager.
/// </summary>
public class BootstrapPlayer : NetworkBehaviour
{
/// <summary>
/// If this method is invoked on the client instance of this player, it will invoke a `ServerRpc` on the server-side.
/// If this method is invoked on the server instance of this player, it will teleport player to a random position.
/// </summary>
/// <remarks>
/// Since a NetworkTransform component is attached to this player, and the authority on that component is set to "Server",
/// this transform's position modification can only be performed on the server, where it will then be replicated down to all clients through NetworkTransform.
/// </remarks>
[ServerRpc]
public void RandomTeleportServerRpc()
{
var oldPosition = transform.position;
transform.position = GetRandomPositionOnXYPlane();
var newPosition = transform.position;
print($"{nameof(RandomTeleportServerRpc)}() -> {nameof(OwnerClientId)}: {OwnerClientId} --- {nameof(oldPosition)}: {oldPosition} --- {nameof(newPosition)}: {newPosition}");
}
private static Vector3 GetRandomPositionOnXYPlane()
{
return new Vector3(Random.Range(-3f, 3f), Random.Range(-3f, 3f), 0f);
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1aaab5fe31dc3423e89747f088d7bcaf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: