Recently, I got quite a few projects to do involving multiplayer. One thing that is making my life much easier is the new Unity Multiplayer Play Mode. Let me tell you what it is, why you should use it, and some tips that may be helpful for you, like, for instance, how to use it with SteamVR!
Unity Multiplayer Play Mode: What is it and why is it great?
Unity has recently upgraded all its multiplayer services. Among these services, it also released some utilities that are platform-agnostic and can be used with other multiplayer plugins like Mirror or Photon. One of these utilities is the Multiplayer Play Mode, which lets you simulate multiple players in your Unity session, so that you can test a multiplayer game locally, alone, without the need to ask other people to help you, and even without the need to build your application.
Let me explain this better. If you make a multiplayer game, you must have multiple players playing it to test its functionalities. So the standard way of performing a test is usually one of the following two:
- You and some other devs run the project from the editor in play mode, so as to perform a match with multiple players
- You make a build of the project, and then perform a local test alone on your PC using both the build and the Unity editor to simulate two players.
The problems of these approaches are that the first requires multiple people to waste time on the tests, while the second requires making a build, which is a very long and time-consuming operation. In any case, it is a lot of wasted time.
That’s why, with Unity 6, Unity came out with a great idea, which is Unity Multiplayer Play Mode. It basically simulates the existence of multiple Unity instances on your PC, all linked to the same project, so that you can just press play and test the application with multiple simulated players, in your editor alone. This is simply amazing. As a developer, I can make a lot of tests simulating that I’m multiple players, and involve the team only when I’ve already ironed out most of the bugs! Plus, it works in play mode, so I can make the modifications on the fly without having to build. This saves everyone a lot of time. And it also works with VR projects…
How to use it
Unity already has a very generous guide about Unity Multiplayer Play Mode, and it makes no sense for me to replicate it. You can access it here: https://docs-multiplayer.unity3d.com/mppm/current/about/
The short version of it, just so that you can get a quick grasp of it, is:
- You use the Package Manager to install the Multiplayer Play Mode package (it’s in the Unity registry)
- You open the window related to Multiplayer Play Mode from the Unity menu: Window > Multiplayer > Multiplayer Play Mode
- You activate the virtual players
- You wait for Unity to activate the players (it may take some time)
And that’s it! You press Play, and the application is run on all the virtual players at the same time!
VR runtimes and compatibility
I’m currently always using OpenXR as the XR plugin for my applications, as it is suggested by Unity itself, so I’m going to comment only about Unity Multiplayer Play Mode (from now on, I’ll refer to it also as “MPPM” for brevity) with this XR plugin.
Oculus/Meta
The best VR runtime to use with MPPM is the Oculus/Meta one. If you use Quest+Link, MPPM just works out of the box: you run the application, and it is seen in VR by one of the virtual players. Then you click the window of one of the other players, and it immediately gets control of the VR headset and controllers, and you can play as the other player. Sometimes, when you click on the window of a player, the click is not immediately recognized (you see it because if you rotate your head, the visuals are not rotating in your window of interest, but in another one). If the click is not recognized, grab the application bar of the window, and move the window a bit: usually, at that point, the window recognizes the input.
SteamVR
I’ve mostly used Meta Quest for my multiplayer work, and things have always been fine. But recently, an update bricked my Quest (thanks, Meta) during my trip to China, so I had to revert to using my Pico 4 to test my application. Pico 4 must be used with Steam VR. Steam VR can work fairly well with Unity, but it has a serious problem with MPPM: only one player gets to use VR, and the others see a black screen. This is a bummer because it means you can not use Multiplayer Play Mode with Steam VR. Or better, actually, you could not, until Tony “the Skarredghost” found a solution yesterday evening. It’s kinda hacky, but it saved my life.
So what I realized reading in the forums is that Steam VR does not like that multiple players get control of it, so only the last one who asks for it gets the control forever, and the others remain without VR support. So what I did was create a script that makes the players get control of SteamVR when they get the focus, but lose it when they lose the focus. This way, only one player at a time controls SteamVR, but you can still have multiple players running at the same time, with everyone getting VR only when it is needed. This makes Steam VR happy and you happy at the same time.
To use MPPM with Steam VR, you can save the following script in your project and then attach it to an empty game object in your start scene. Remember also to set the “Manual Initialization for MPPM” flag to true and to save the scene.
Thanks to this script, you can click on the various players and activate VR on them, and do your tests. I’ve noticed that there may be some recentering issues (like the player may float above the floor), but you can use the recentering feature of SteamVR to fix this problem.
If you still have errors…
If you did not manage to make your application work with your XR runtime, you still have a last resort option: using the XR Device Simulator. Unity has a prefab that you can put in your scene, and in play mode, it lets you control a simulated headset and two simulated controllers using your mouse and keyboard. The simulator also works with MPPM, so you can control the simulated headset and controllers of multiple players.
You can find everything about it here: https://docs.unity3d.com/Packages/com.unity.xr.interaction.toolkit@2.3/manual/xr-device-simulator-overview.html.
This solution kinda works, but it is very clunky, because you have to move your emulated headset and emulated controllers one by one, so keep it only as your last resort.
VPNs and compatibility
I’ve noticed that some features of Unity multiplayer may have problems if you have a VPN on. I was testing an application featuring Unity Netcode using MPPM, and I’ve noticed that when the host of the instance changed the current scene, the other simulated players did not. After banging my head on the wall for more than half an hour, I realized that it was the VPN creating issues. So be careful that some malfunctions may be caused by your VPN, if you have one on.
Tags and authentication
I strongly suggest that you add a tag to every one of your virtual players. The reason is that, for instance, if you use Unity Authentication, it can work fairly well with MPPM, but it must be able to disambiguate the players, so they must have different tags.
Debug on the main player
If you have issues and you have to run the debugger in Visual Studio, the easiest approach is to make the main player have the bug and run the debugger as usual in the Visual Studio instance associated with the main Unity instance. Theoretically, you could also debug the projects related to the other players, but I’ve found it to be more clunky.
Ignore the Unity XR Core exceptions
I’ve seen that my virtual players in some projects using the XR Interaction Tookit and XR Hands have a couple of random errors related to Unity XR Core. These errors actually are causing no problems to me, so I would suggest that you ignore them and just press Play and do your tests.
Use the Unity windows, if needed
The virtual players show only the Game window, but actually, upon request, they can also show the other Unity windows, like the Inspector or the Hierarchy. This is disabled by default for performance reasons, but in case you absolutely need to show them, you can use the Layout button on the top right of the window of the virtual players. I suggest you show these extra windows only for the time that you need them, to avoid consuming too many computational resources.
Fix the bug for mouse usage with tags
Currently, if a player has a tag associated, the mouse input does not work if you are using the XR Interaction Toolkit. To fix the problem of the mouse not working on the players with a tag inside Unity Multiplayer Play Mode, you can attach this script to the same gameobject holding the XRUIInputModule:
Mind the used resources
Unity Multiplayer Play Mode runs multiple Unity instances at the same time, which is pretty heavy on the computer. VR is also pretty heavy on the computer. Using these two technologies together puts your PC under serious stress. I had to do some PC reboots while testing my applications with MPPM, because everything was blocked. It has been rare, but it happened. You have been warned: do not use MPPM with VR while you are doing some other heavy operations on your PC.
Keep doing real multiplayer tests
I can tell by experience that testing locally in Play mode is not the same as testing with remote players running a build. MPPM can help you in detecting most of the bugs, but there will still be a 5-10% of issues that will show up only in real tests. This is why you should still keep doing real tests and can not rely only on MPPM. But you will require fewer tests than before!
And that’s it, I hope this post has been useful for you, and if it is the case, reshare it with your Unity peers, subscribe to my newsletter not to miss my next article about VR, or donate to my Patreon! I wish you a great time with Unity Multiplayer Play Mode!