t1k:unity:networking:netcode
| Field | Value |
|---|---|
| Module | networking |
| Version | 2.1.7 |
| Effort | high |
| Tools | — |
Keywords: multiplayer, netcode, networking, NGO
How to invoke
Section titled “How to invoke”/t1k:unity:networking:netcodeUnity Netcode for GameObjects — Multiplayer Networking
Section titled “Unity Netcode for GameObjects — Multiplayer Networking”NGO reference for Unity 6. Server-authoritative networking.
- Install
com.unity.netcode.gameobjectsvia Package Manager - Add
NetworkManagerto scene, set Unity Transport - Mark spawnable prefabs with
NetworkObject, register in NetworkManager
NetworkManager
Section titled “NetworkManager”NetworkManager.Singleton.StartHost(); // Server + clientNetworkManager.Singleton.StartServer(); // Dedicated serverNetworkManager.Singleton.StartClient(); // Client onlyNetworkManager.Singleton.Shutdown();
NetworkManager.Singleton.OnClientConnectedCallback += clientId => { };NetworkManager.Singleton.OnClientDisconnectCallback += clientId => { };NetworkBehaviour
Section titled “NetworkBehaviour”public class PlayerController : NetworkBehaviour { public override void OnNetworkSpawn() { if (IsOwner) EnableInput(); if (IsServer) InitServerState(); } // IsOwner, IsServer, IsClient, IsHost, IsLocalPlayer}NetworkVariable (State Sync)
Section titled “NetworkVariable (State Sync)”public NetworkVariable<int> Health = new(100, NetworkVariableReadPermission.Everyone, NetworkVariableWritePermission.Server);
public override void OnNetworkSpawn() { Health.OnValueChanged += (old, val) => UpdateHealthBar(val);}// Client → Server:[ServerRpc]void ShootServerRpc(Vector3 dir, ServerRpcParams rpcParams = default) { ulong sender = rpcParams.Receive.SenderClientId; SpawnProjectile(dir, sender); OnShootClientRpc(dir);}
// Server → All Clients:[ClientRpc]void OnShootClientRpc(Vector3 dir) { PlayShootVFX(dir); }
// Server → Specific Client:var rpcParams = new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIds = new[] { targetId } }};ShowMessageClientRpc("You win!", rpcParams);Spawning
Section titled “Spawning”GameObject obj = Instantiate(prefab, pos, rot);obj.GetComponent<NetworkObject>().Spawn(); // Server-ownedobj.GetComponent<NetworkObject>().SpawnWithOwnership(clientId); // Client-ownedobj.GetComponent<NetworkObject>().Despawn(); // RemoveNetworkTransform
Section titled “NetworkTransform”Add component to sync position/rotation/scale. Options: Interpolate, Authority (Server/Owner), Threshold.
Common Gotchas
Section titled “Common Gotchas”- RPC naming: Must end with
ServerRpcorClientRpcsuffix exactly - Serialization: RPC params must be primitives or
INetworkSerializable - IsOwner in Awake: Not set — use
OnNetworkSpawn()instead - NetworkVariable init: Field declaration only, not constructor
- Prefab registration: All spawnable prefabs must be in NetworkManager list
- Single NetworkManager: Only one per scene (singleton)
Related Skills & Agents
Section titled “Related Skills & Agents”unity-scene-management— Networked scene loadingunity-input-system— Client-side input predictionunity-animation— NetworkAnimator syncunity-profiling— Network perf analysis