# IGP Unity Multiplayer

`cn.indiegp.sdk.unity.multiplayer` is the public Core-dependent Unity package
for the Playing Game data plane: Realtime, KCP/TCP/UDP Game transport, and
optional Mirror integration.

Core is the sole Google.Protobuf/Unsafe runtime DLL provider for both local source
and release carriers. Multiplayer does not bundle a second copy. Upgrade Core and
Multiplayer together; older Multiplayer releases embed their own copies.

Mirror accepts original reliable messages up to 256 KiB into one shared
1 MiB / 1024-message queue. It forwards whole payloads to Game without batching,
compression or Mirror fragmentation. WouldBlock retains the queue head; full
queues reject new messages. TryClientSend/TryServerSend return local acceptance.
Both peers must support RawReliable256KiB. See [Mirror integration](Documentation~/MIRROR-QUICKSTART.md).

## Binary UPM Contents

The offline UPM tarball contains the Multiplayer runtime in
`Plugins/IGP.Multiplayer.dll`, package documentation, and opt-in samples. Runtime
implementation source and PDB files are intentionally omitted. The Mirror
adapter remains as source under `Scripts/Integrations/Mirror` so projects can
compile it only when Mirror is present. Install the same-version Core package
before Multiplayer. Do not install this UPM carrier and the source
`.unitypackage` carrier for the same module together.

Core includes the required Google.Protobuf and System.Runtime.CompilerServices.Unsafe
binaries and their licenses. Multiplayer's `link.xml` preserves its generated
protocol types during managed stripping. No separate Protobuf package is needed.

Unity keeps package-owned `Samples~/` content hidden until the developer imports
a sample. The shared `IGPSDKIntegration` examples retain the inert `.cs.txt`
suffix and must be copied or renamed explicitly before use.

Core resolves the current Unity runtime platform in Desktop-then-Mobile order
before constructing exactly one matching Host Session runtime. Attach failure
does not switch platforms. There is no user-facing or serialized platform
setting. Multiplayer does not expose, inspect, or repeat this policy. Its
parameterless creation API obtains a complete `IGPMultiplayerBootstrap` from
Core's active Host provider; the explicit overload accepts the same caller-owned
Core DTO. Both entry points validate the immutable Gateway room/current-player
snapshot and descriptor before connecting to the same reliable KCP/TCP and
optional unreliable UDP lanes.

If the active Host path is unavailable, Core initialization still succeeds,
the failure is logged as a warning, and `IsRuntimeCreated` remains false. Call
the parameterless `TryCreateRuntimeAsync()` to request Host bootstrap again.
An established Reliable interruption emits ConnectionInterrupted and automatically
attempts recovery once using retained credentials. Manual ReconnectDataPlaneAsync
shares an ongoing attempt; success emits ConnectionRecovered and failure emits
MULTIPLAYER_RECONNECT_FAILED without a retry loop. DisconnectAsync requests only
this player's Game departure; FinishGameAsync requests whole-Match completion.

CurrentRoomSnapshot is immutable. RoomChanged reports Joined/Updated/Left and
GameStarted/GameEnded with complete previous/current snapshots and combined Changes.
Host and explicit Lobby handoffs use internal descriptor matchId for deduplication
and replacement; Game handshakes and snapshots project player ConnectionState.
Multiplayer never connects to Gateway or exposes room commands.
See [room events](Documentation~/EVENT-REFERENCE.md) for timing and payloads.

`Robots` exposes a read-only list of the startup snapshot's players marked
`IsBot`. `TryGetRobot(id, out robot)` searches that list by exact ID without
network requests. Each item is the existing immutable
`IGPMultiplayerPlayerSnapshot`; `RobotData` is opaque game data, not interpreted
by the SDK. Robots do not require RoomNode peer slots or routes. The list is
empty before bootstrap installation and after creation failure, explicit
disconnect, or runtime disposal. A transient Reliable interruption retains the
snapshot; a fresh bootstrap installed during creation/reconnect replaces it.

The host ends the active Game runtime with `FinishGameAsync()`. This directly
writes the reserved `game_finish` message to the connected reliable Game lane
before the call completes; it is not placed in the normal Game send queue and
is not a Gateway/Lobby command. The call requires a Ready reliable transport
and the RoomNode rejects non-host senders.

Desktop launch-ticket redemption and Hosted RoomSnapshot/descriptor collection
are implemented by Core's Desktop platform runtime. Multiplayer contains no
Desktop bootstrap/session client. Core's Mobile platform runtime does not yet
implement the bootstrap provider, so parameterless creation returns false with a stable
Host capability error. The descriptor request does not switch to Desktop; Core
already finalized the platform before constructing its single Host runtime.

RoomNode supplies peer slots during the Reliable handshake. Multiplayer commits
`CurrentPeerContext` before Ready and clears it on Reliable disconnect. These
fields are routing context only; the SDK does not audit robot flags, team rules,
membership policy, or RoomNode slot allocation.

Host communication events are owned by the optional GameKit package. Multiplayer
does not subscribe to Host Session Service Messages and exposes no voice, text,
or emotion event API.

Bootstrap objects are connection inputs, not saved state. Descriptor credentials
remain opaque to game code, are redacted from `ToString()`, and must not be
serialized or logged. Invalid bootstrap inputs fail before socket creation with
`MULTIPLAYER_CONTEXT_INVALID`; unusable Reliable peer routing fails with
`DATA_PLANE_PEER_CONTEXT_INVALID`.

For offline UPM installation, add `core-igp-<version>.tgz` and then
`multiplayer-igp-<version>.tgz`.

See [Documentation](Documentation~/README.md),
[Events](Documentation~/EVENT-REFERENCE.md),
[断线重连接入流程与示例](Documentation~/CONNECTION-RECONNECT.md), and
[Mirror](Documentation~/MIRROR-QUICKSTART.md).

游戏进程退出后重新进入当前对局，见[手动重返指南](Documentation~/MANUAL-REENTRY.zh-CN.md)。
