Files
nexus/wiki/concepts/UnityRelay.md
2026-04-26 12:02:53 +08:00

39 lines
1.4 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 Relay"
type: concept
tags: [networking, unity, ugs, nat-traversal]
sources: [unity-multiplayer-engineer]
last_updated: 2026-04-26
---
## Aliases
- Unity Gaming Services Relay
- UGS Relay
## Definition
Unity Relay 是 Unity Gaming ServicesUGS提供的**流量中继服务**,用于解决 NAT 穿透问题,使不同网络环境下的玩家能够建立连接。玩家托管游戏必须使用 Relay直接 P2P 连接会暴露主机 IP 地址。
## Key Features
- **无需专用服务器**:玩家之一作为 Host其他玩家通过 Relay 连接
- **IP 保护**Relay 隐藏了 Host 的真实 IP 地址
- **多平台兼容**:支持 UDP/TCP DTLS 加密传输
## Workflow
1. Host 初始化 Unity Services + 匿名认证
2. Host 创建 Relay Allocation获取 Join Code
3. Host 将 Join Code 分享给其他玩家
4. Clients 通过 Join Code 加入 Relay
5. UnityTransport 自动将流量路由至 Relay 服务器
## Security Note
> "Always use Relay for player-hosted games — direct P2P exposes host IP addresses"
## Related Concepts
- [[UnityLobby]]: 与 Lobby 配合实现完整匹配流程
- [[ServerAuthority]]: Relay 使服务器权威模型在玩家托管游戏中可行
- [[UnityGamingServices]]: Relay 是 UGS 的核心组件之一
## Related Entities
- [[UnityGamingServices]]: Relay 所属的服务平台
- [[UnityMultiplayerEngineer]]: 使用 Relay 构建多人游戏的专家