<!-- skill: Unity Netcode | version: 1.0.0 | description: Netcode for GameObjects RPCs lobbies relay and ParrelSync testing | category: Games -->
## Instructions
- Use Netcode for GameObjects v1.8+ on Unity 2022.3 LTS or newer for all new multiplayer work.
- Always place exactly one NetworkManager + UnityTransport + NetworkSceneManager in a bootstrapped DontDestroyOnLoad scene.
- Make player prefabs with NetworkObject + ClientNetworkTransform; never use legacy NetworkTransform without reason.
- Default to ServerAuthoritative movement; use ownership only for cosmetic / input intent, validate on server.
- Use ServerRpc for client->server input, ClientRpc for server->broadcast, and NetworkVariable with read/write perms for state.
- Require ownership for ServerRpcs that move a player: `[ServerRpc(RequireOwnership=true)]`; set false only for global interactions.
- Integrate Unity Gaming Services Lobby + Relay for NAT traversal; never expose direct IP in production builds.
- Test with ParrelSync clones (2-3 clients + host) before any commit; cover host-migration failure message.
- Handle OnClientDisconnectCallback and scene-event cleanup to avoid ghost NetworkObjects on rejoin.
- Profile with Network Profiler and keep tick rate 30-60 Hz; compress vec3 and use delta updates.
- Version network prefabs via GlobalObjectIdHash; warn on prefab list mismatch between builds.
- Secure builds with connection approval JSON + rate limits; kick on malformed RPC payload.

## Examples
- Example 1: User asks "add sprint sync" -> add NetworkVariable<bool> IsSprinting, ServerRpc RequestSprint, ClientRpc animation trigger.
- Example 2: User asks "lobby then start" -> create UGS Lobby (max 4), allocate Relay, join code to clients, LoadScene on all.
- Example 3: User asks "test locally" -> clone project with ParrelSync, run Host in editor, Client in clone, check ownership log.

## Workflows
- New project workflow: Install NGO + UGS packages -> configure NetworkManager prefab list -> add Transport -> write PlayerBehaviour -> ParrelSync test.
- RPC workflow: Define intent (who calls, who executes) -> choose ServerRpc/ClientRpc -> add validation -> test with latency simulation 100/200ms.
- Lobby+Relay workflow: Authenticate anonymously -> Create Lobby -> Allocate Relay -> Update Lobby with join code -> Clients JoinLobby then JoinRelay -> NetworkManager.StartClient.
- Debug workflow: Enable NGO logs -> reproduce desync -> inspect NetworkVariable deltas -> fix authority -> retest with 3 clones.

## Templates
- Player NetworkBehaviour checklist:
  - [ ] NetworkObject checked, prefab in NetworkPrefabs list
  - [ ] ClientNetworkTransform added, authority set to Server
  - [ ] Input read in Update only if IsOwner
  - [ ] Movement applied in ServerRpc or NetworkVariable
  - [ ] OnNetworkSpawn registers, OnNetworkDespawn unregisters
- ServerRpc template:
```csharp
[ServerRpc(RequireOwnership = true)]
void SubmitMoveServerRpc(Vector3 dir, ServerRpcParams rpc = default) {
  if (dir.sqrMagnitude > 1.1f) return; // validation
  transform.position += dir * speed * Time.deltaTime;
}
```
- Connection approval snippet:
```csharp
NetworkManager.Singleton.ConnectionApprovalCallback = (req, resp) => {
  resp.Approved = req.Payload.Length < 512;
  resp.CreatePlayerObject = true;
};
```
- ParrelSync test matrix: Host+1 client walk, Host+2 clients RPC spam, Host drop + rejoin.

## Tools
- Packages: com.unity.netcode.gameobjects, com.unity.transport, com.unity.services.lobbies, com.unity.services.relay, ParrelSync (editor).
- Editor: Network Profiler, Multiplayer Play Mode (Unity 6), Transport debug simulator.
- Services: Unity Gaming Services dashboard for Lobby/Relay quotas and analytics.
- External: Wireshark for packet check, Unity Cloud Build for synced client/host builds.

## Advanced
- Use NetworkList and custom INetworkSerializable structs for inventories; avoid spamming ClientRpc per item.
- Implement interest management via NetworkObject.CheckObjectVisibility for 16+ player scenes.
- Add client prediction + reconciliation for fast shooters; fallback to server reconcile every 100ms.
- Encrypt Relay join codes out-of-band; expire lobbies after 30s without heartbeat.
- Handle scene switching with NetworkSceneManager.OnSceneEvent to respawn players deterministically.

## Documentation
- Unity Netcode docs: https://docs-multiplayer.unity3d.com/netcode/current/about/
- UGS Lobby: https://docs.unity.com/ugs/en-us/manual/lobby-manual
- Relay manual: https://docs.unity.com/ugs/en-us/manual/relay-manual
- ParrelSync repo: https://github.com/VeriorPies/ParrelSync
- Boss Room sample: https://github.com/Unity-Technologies/com.unity.multiplayer.samples.coop
