by Pivec Labs
TutorialTurnBased + Video Chat

TurnBased + Video Chat

⏱ 10–15 min Uses the shipped TurnBasedVideoChatDemo scene

Prerequisites: NetCrafter Relay 2.0 with com.unity.webrtc 3.0.0+ installed (one click from Settings → Live (Voice & Video)); a server running the 2.0 rendezvous service; two machines, builds or Editor instances, each with a webcam and microphone. It helps to have done the TurnBased tutorial first — this is the same game.

What you'll see: The TurnBased game, unchanged, with a video chat panel. The host and the first player to join see and hear each other while they play. Nobody types a second code: joining the game is joining the call.

01

Open the demo scene#

text
Assets/PivecLabs/Examples/LiveDemos/TurnBasedVideoChat/TurnBasedVideoChatDemo.unity

Or click Open TurnBased + Video Chat in Settings → Live (Voice & Video). The scene is a copy of the TurnBased demo with three things added:

  • LiveVideoChat — a NetCrafterLiveConnection (webcam + microphone) and a NetCrafterLiveRelayLink.
  • VideoChatCanvas — the panel at the top of the game's left column (shown during gameplay; the score list moves down below it): the other player large, your own camera as a small inset, a status line and a Hang up / Start video button. Driven by TurnBasedVideoChatPanel.cs.
  • Nothing else. The game scripts and the TurnBased Definition are the originals.
02

Server and settings#

Both demos need the 2.0 rendezvous service on your server — it's what lets the call or broadcast reuse the game's join code. New server: run the Server Setup Wizard (Tools → Pivec Labs → NetCrafter Relay → Settings). Server set up with 1.x: run the wizard against it once and choose Update / redeploy (same port, credentials and data).

The video call uses the TurnBasedDemo Definition's Relay Settings — the same ones the game uses — so there is nothing extra to fill in. Select TurnBasedDemo in the NetCrafter Relay window and check Settings → Relay Settings shows your Relay Host and Rendezvous URL.

03

Play it#

  1. Player 1 (host): enter a name, click Host. The call starts straight away; once you start the game, the video panel shows "Waiting for player 1 to join the call..." and your own camera in the inset.
  2. Player 2: enter a name and the host's join code, click Join. A moment after joining the game, the call connects — no second code.
  3. Start the game and play. The call stays up across lobby, gameplay and game-over.
  4. Hang up ends the call without leaving the game; Start video brings it back. If one player hangs up, or the connection drops, the call re-establishes itself as soon as both want it: the host re-opens it automatically and the other player rejoins.

3 or 4 players? The game still works for everyone, but NetCrafterLiveConnection is 1-to-1: the call is between the host and the first player to join (Relay player id 1). Other players see a note saying so. Video between every pair of players would need a connection per pair.

04

How it works#

NetCrafterLiveRelayLink listens to Relay's NetCrafterEventDispatcher:

  • RelayHosting (the host got its game join code) → call.Host(gameCode). The 2.0 rendezvous service accepts the game's code for the call because that game session is active.
  • RelayJoined (new in 2.0, payload RelayJoinInfo with the code and your player id) → if your id is callPartnerUserId (1), call.Join(gameCode).
  • ConnectionLost → hang up.

The link never references the TurnBased game's generated code, which is why it drops into any Relay game.

05

Add it to your own game#

  1. Install com.unity.webrtc (Settings → Live tab).
  2. Add a GameObject with NetCrafterLiveConnection. In its Inspector, pick your game's Definition under Use Definition and click Assign Relay Settings. Leave videoSource on Webcam and captureAudio on; give it an AudioSource for remoteAudioOutput.
  3. Add NetCrafterLiveRelayLink to the same GameObject and drag the connection into its call field.
  4. Show the video: subscribe to RemoteVideoTextureAvailable / LocalVideoTextureAvailable and put the textures on RawImages — TurnBasedVideoChatPanel.cs is a ready-made example.

That's it — no code changes to your game. See the API Reference for the link's options (manual start, partner id).

06

Troubleshooting#

  • Host shows "Failed to get a join code from the rendezvous service" / the call never starts: the server is still running the 1.x rendezvous service. Re-run the wizard with Update / redeploy.
  • Joiner stays on "Joining video call...": check both machines use the same Definition's Relay Settings, and see the Player Hosting Guide for force-relay and connection checks.
  • Black video, call connected: camera in use by another app, or camera permission denied (Windows privacy settings / Android permission prompt).
  • Panel overlaps the board on very wide or narrow screens: it's a separate overlay canvas (VideoChatCanvas) — move or resize VideoChatPanel freely.