Tutorial: Unity Multi Apps Part 2: Networking
Now that you've built an XR Package with Unity, you're ready for the next step: implementing netwokring! Multi Apps are inherently social; every Multi App is inherently shareable. By loading it into the Pluto Multi App Launcher, every Pluto conversation member automatically loads an identical copy. That said, implementing a networking layer lets you design intricate interactive multi user experiences, and share data between your Multi Apps. It's something you'll typically want to do when designing your Multi Apps.
The project utilizes Photon Unity Networking (PUN), a popular networking library for Unity. Luckily, Photon has a free tier, so you can download the project, add your own Photon app ID (see step #0), build and witness XR Package networking in action!
It's important to mention you can use any networking solution you like, as long as it's supported in the WebGL runtime (which unfortunately, as of the time of writing, excludes the equally popular Normcore.)
You'll notice the project is very similar to the Pluto XRPK Exporter sample project you've encountered in the previous tutorial. In fact, the only difference is the inclusion and implementation of Photon (which also involves a minor refactor to the Cubes prefab - it is now instantiated when the first user has joined, or created, the room).
Before we start, we recommend you acquaint yourself with Photon/PUN fundamentals. It's important to note this is just an example networking implementation - the XR Package-critical step will be described in step 2.
Start by downloading the sample project and opening it in Unity.
As mentioned, you'll have to add your own App ID to Photon - this is a unique identifier for your app, and is used to identify your app when connecting to a specific Photon server.
Sign up for a Photon Account (free).
In your project, go to
Window/Photon Unity Networking/Highlight Server Settingsand paste or type the App ID you copied. You should be good to go - hitting
Playwill demonstrate the stages of connecting to a server in the console and instantiate the Cubes prefab.
1. General Scene Changes
A. Photon components
Note that the Left Controller and Right Controller scene objects now have additional components added to them: a Photon View and a Photon Transform View.
These are built-in compoenents that, without any code, let us sync the position and rotation of the controller with the position and rotation of each controller in the Unity scene (assuming we're networked - which we'll discuss in the next step).
It's important to note that even though were are not displaying the controllers graphically (since, when working with multi apps, we can see the controllers represented in our runtime environment like SteamVR), we are still able to sync the position and rotation of the controllers - furthermore this is critical to ensure that the cubes respond to each controller across every other networked instance of the scene, and not just in ours.
Aside from those Photon components, the controller object is identical to the example in the previous tutorial.
B. Multi App Manager / App ID retrieval
Next, take a look at the
Networking Manager script under the
Managers Scene object.
The script is responsible for handling the connection to Photon. It is a variation on the basic connection script from the Photon PUN Tutorial. The connection is handled automatically and straightforwardly, with one exception: the room ID is procured from the Pluto Multi App Manager - thus, ensuring a unique room is created for every Pluto conversation that the multi app is part of!
2. More on the Multi App Manager
Included in the Pluto Unity XRPK Exporter is a
MultiAppManager static object that provides an entry point to several endpoints available to you when you launch your XRPK (although not in the Unity editor!).
One of them is the unique XR Package ID generated by the Pluto Multi App Launcher when it loads your XR Package.
This is a very helpful piece of data, since without it we'd have to find another way to pass a room ID, create a lobby system, or have every user of this Multi App join the same room (regardless of Pluto conversation) - none of which are ideal solutions.
For our purposes, the relevant line is #16 in
// using the Multi App Manager (available thanks to the pluto XRPK exporter package)// to get the session-unique XRPK ID and use it as a room identifier_appId = MultiAppManager.GetAppId();
You can use
MultiAppManager.GetAppId() regardless of which networking stack you use, to get a reliably unique room number that will work per XRPK, per Pluto conversation. This is the most important line in this example, and the biggest takeaway.
There are no other special considerations for this example. Go ahead and build it, launch the resulting XRPK to the Multi App Launcher and - if you're in a Pluto conversation - you and all other participants should be able to see and interact with the cubes.