Files
nexus/wiki/sources/unity-multiplayer-engineer.md

65 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Unity Multiplayer Engineer"
type: source
tags: []
date: 2026-04-26
---
## Source File
- [[raw/Agent/agency-agents/game-development/unity/unity-multiplayer-engineer.md]]
## Summary用中文描述
- 核心主题Unity 多人游戏网络编程专家 Agent人格定义涵盖 Netcode for GameObjectsNGO、Unity Gaming ServicesRelay/Lobby集成、服务器权威模型、延迟补偿和状态同步等技术规范。
- 问题域:如何为 Unity 游戏构建安全、抗作弊、延迟容忍的多人联网系统。
- 方法/机制:服务器权威架构 + 客户端预测与调和 + RPC/NetworkVariable 分层设计 + UGS Relay/Lobby 匹配服务。
- 结论/价值:提供完整的 Unity 多人游戏开发技术规范,包括网络代码模板、带宽管理策略、反作弊架构和高级性能优化方案。
## Key Claims用中文描述
- 服务器必须拥有所有游戏状态的权威真相——位置、生命值、分数、物品所有权,客户端仅发送输入。
- NetworkVariable 用于持久化复制的状态RPC 用于一次性事件,二者不可混用。
- 客户端预测移动必须与服务器状态进行调和,当偏差超过阈值时强制回正。
- Unity Relay 应用于所有玩家托管游戏,禁止直接 P2P 连接以保护主机 IP 安全。
- 带宽管理:非关键状态更新(如血条、分数)应限流至最高 10Hz避免每帧同步。
## Key Quotes
> "The client doesn't own this — the server does. The client sends a request." — 服务器权威原则
> "That NetworkVariable fires every frame — it needs a dirty check or it's 60 updates/sec per client" — 带宽控制原则
> "Design for 200ms — not LAN. What does this mechanic feel like with real latency?" — 延迟设计原则
> "If it persists, it's a NetworkVariable. If it's a one-time event, it's an RPC. Never mix them." — RPC vs Variable 原则
## Key Concepts
- [[ServerAuthority]]:服务器权威模型——服务器拥有所有游戏状态的最终真相,客户端仅发送输入请求。
- [[ClientPrediction]]:客户端预测——客户端立即执行输入预测移动,再与服务器权威状态调和校正。
- [[NetworkVariable]]: NGO 中用于持久化复制状态的类型,仅在值变化时触发网络同步。
- [[ServerRpc]]: 客户端调用、服务器执行的 RPC用于提交输入请求服务器端必须验证所有输入。
- [[ClientRpc]]: 服务器调用、所有客户端执行的 RPC用于广播确认的游戏事件如命中确认、技能激活
- [[UnityRelay]]: Unity Gaming Services 的中继服务,解决 NAT 穿透问题,保护主机 IP 不暴露。
- [[UnityLobby]]: Unity Gaming Services 的匹配大厅服务,仅存储元数据(玩家名、地图选择),不存储游戏状态。
- [[LagCompensation]]: 延迟补偿机制——服务器在处理客户端输入时考虑网络延迟影响。
- [[BandwidthManagement]]: 带宽管理——通过 dirty check、差分序列化、限流等策略控制网络流量。
- [[AntiCheatArchitecture]]: 反作弊架构——所有游戏关键值必须经过服务器验证,客户端不可信。
## Key Entities
- [[UnityMultiplayerEngineer]]:本 Agent 角色定义——构建确定性、抗作弊、延迟容忍的 Unity 多人游戏系统的专家。
- [[NetcodeForGameObjects]]Unity 官方网络代码框架,提供 NetworkObject、NetworkVariable、ServerRpc、ClientRpc 等核心网络原语。
- [[UnityGamingServices]]: Unity 云服务集合,包含 Relay流量中继和 Lobby匹配大厅两个多人游戏核心服务。
- [[UnityTransport]]: Unity Netcode 的传输层,支持直连和 Relay 两种连接方式。
## Connections
- [[UnityMultiplayerEngineer]] ← extends ← [[NetcodeForGameObjects]]
- [[UnityMultiplayerEngineer]] ← depends_on ← [[UnityGamingServices]]
- [[ServerAuthority]] ← implements ← [[ClientPrediction]]
- [[ClientPrediction]] ← reconciles_with ← [[ServerAuthority]]
- [[UnityRelay]] ← enables ← [[ServerAuthority]]
- [[UnityLobby]] ← provides ← [[AntiCheatArchitecture]]
## Contradictions
- 与 [[UnrealMultiplayerArchitect]] 潜在差异:
- 冲突点:客户端预测实现细节。
- 当前观点Unity使用 NetworkVariable + 自定义调和逻辑,在 LateUpdate 中检测并纠正客户端与服务端位置偏差。
- 对方观点Unreal使用更细粒度的帧缓冲和回滚机制。
- 说明:两者均遵循服务器权威原则,差异源于底层网络框架设计哲学不同,两者可互补学习。