各Unityインスタンスが自分のUDPポートで待ち受けながら相互に状態を同期するミニマムなメッシュデモとして再構成しました。移動状態は低遅延なシーケンス付きUDPで配信し、演出イベントはACK/再送付きの信頼配送で同期します。プロダクション運用を想定し、ポート衝突、疎通監視、重複パケット抑制、コマンドライン起動、ランタイムUIを備えています。
Every Unity mesh instance listens on its own UDP port and synchronizes live avatar state with its peers. Motion snapshots use sequenced low-latency UDP while pulse events use ACK-backed reliable delivery with retry and duplicate suppression. The implementation includes production-facing guardrails: port validation, heartbeat/stale detection, bounded packet processing, command-line launch hooks, and an in-game operator panel.
- The demo targets Unity 6.3 URP and builds the scene at runtime, so an empty template scene becomes an immediate visual networking testbed.
- Peer avatars use procedurally composed mesh primitives with per-peer material instances, trail renderers, and color hashing from peer identity to keep remote state legible during multi-instance testing.
- The visual layer is intentionally lightweight: no imported assets, no scene prefab dependency, and no heavy post-processing, keeping the frame budget reserved for synchronization behavior.
- Local motion uses keyboard input mapped into a normalized 2D movement vector, converted into XZ-plane velocity, clamped to a bounded arena, and smoothed with exponential interpolation.
- Remote avatars interpolate toward sequenced position snapshots while rotating toward received velocity vectors, giving stable motion even when UDP packets arrive with jitter or packet loss.
- Space/Pulse sends a reliable event to all known peers; the receiver applies a radial scale pulse while suppressing duplicate reliable sequence IDs.
- UDP datagrams are capped at 1200 bytes to stay MTU-friendly and avoid fragmentation in normal LAN conditions.
- Receive work is bounded per frame (
96packets by default), preventing a packet burst from monopolizing the main thread. - Reliable event delivery uses per-peer pending queues, ACK packets, retry intervals, retry caps, RTT smoothing, and stale-peer detection, mirroring production network discipline without requiring external packages.
Keyboard / UI / CLI
|
v
P2PDemoApp
- local movement vector
- runtime scene + UI
- command-line port/peer setup
|
v
P2PPeerTransport
- UDP socket bound per instance
- heartbeat + stale detection
- reliable ACK/retry for events
- sequenced snapshots for state
|
v
P2PPacketCodec / P2PMessagePayloads
- binary packet header
- bounded payloads
- duplicate + stale sequence rejection
|
v
Remote Unity Instance(s)
|
v
Avatar interpolation / pulse visualization / operator status UI
Open Assets/Scenes/SampleScene.unity and press Play. The bootstrapper creates the demo scene and UI automatically.
For two local builds or two editor clones:
UnityPlayer.exe -p2p-name PeerA -p2p-port 7777 -p2p-peer 127.0.0.1:7778 -p2p-autostart
UnityPlayer.exe -p2p-name PeerB -p2p-port 7778 -p2p-peer 127.0.0.1:7777 -p2p-autostartControls:
WASD/ arrow keys: move the local peer.SpaceorPulse: send a reliable pulse event.Start,Stop,Add Peer: operate the mesh from the runtime UI.