Setting Up a Networked AR Session

Enable nearby players to join the same AR experience.

Anatomy of an ARNetworking Session

For multiple players to interact in the same AR world, they have to share information from the separate ARSessions running on each of their devices. Connecting players together so they can send and receive that information is handled by a MultipeerNetworking instance on each device. Processing that information is in turn handled by an ARNetworking instance on each device.

ARNetworking sessions are constructed using the ARNetworkingFactory class.

using Niantic.ARDK.AR.Networking;

// Create an ARNetworking session.
// This also creates new ARSession and MultipeerNetworking objects,
// since they are required components of ARNetworking.
var arNetworking = ARNetworkingFactory.Create();

An ARNetworking session can also be constructed from existing ARSession or MultipeerNetworking instances.

For example, your application could have a lobby, where players who are connected to a MulitpeerNetworking session can send messages to each other while waiting for a shared AR experience to start. The players could be labeled by colors based on their peer Identifier, so when those players hop into the shared AR experience, you want those identifiers to stay the same. Using that same MulitpeerNetworking session to power the ARNetworking session enables that, because it means players wouldn’t have to disconnect and join a new session.

using Niantic.ARDK.Networking;
using System.Text;

var networking = MultipeerNetworkingFactory.Create();

// Can join either before or after creating an ARNetworking
var sessionIdentifier = Encoding.UTF8.GetBytes("Example");
networking.Join(sessionIdentifier);

// The ARSession and MultipeerNetworking objects used to create an ARNetworking
// need to have the same stage identifier, so specify the stage identifier for
// the ARSession constructed here.
var arSession = ARSessionFactory.Create(networking.StageIdentifier);

var arNetworking = ARNetworkingFactory.Create(arSession, networking);

Starting a Shared Experience

To enable a shared AR experience, both the underlying ARSession and MultipeerNetworking components of the ARNetworking object need to be started.

using Niantic.ARDK.AR.Configuration;

// Create a configuration.
var configuration = ARWorldTrackingConfigurationFactory.Create();

// Enable shared experiences.
configuration.IsSharedExperienceEnabled = true;

// Run the AR session (if you haven't already).
arNetworking.ARSession.Run(configuration);

// Join the networking session (if you haven't already).
var sessionIdentifier = Encoding.UTF8.GetBytes("Example");
arNetworking.Networking.Join(sessionIdentifier);

See the page on Creating AR Experiences for a comprehensive explanation on how to set up an AR experience, and the page on Create a Multiplayer Experience for how to network players together. In short though:

  1. Add an ARSceneCamera prefab to your scene.

  2. Use the same sessionIdentifier value for all players joining the same session.

Using ARNetworkingManager

To simplify the process of setting up an AR networked session, we provide a Manager you can add to your scene. The API reference and in-code comments/tool tips for ARNetworkingManager explains how to use it.

Alternatively, you can use the ARNetworkingSceneManager prefab, which includes a ARNetworkingManager component. See Getting Started with Shared AR for details on using this prefab.

Hosts and Peers

Networked sessions use the concept of host and peer clients. When a client sends a join request with a specified session identifier, the server will do one of the following:

  • Add the client to the session, if the session exists. The client is now a peer in the session.

  • Create the session and designate the client as the host.

All subsequent clients that join the same session will know which client is the host.

Each client that wants to join a shared ARDK network session must also be configured to use the same API token.