Building a Multiplayer for Magic Leap 2

I am currently considering developing a multiplayer feature in Unity using several Magic Leap 2 devices.

Magic Leap 2 I would like to synchronize the wearer's position, hand tracking position, and animations of several models with the multi-player feature.

Photon Fusion is expensive and requires a monthly subscription. I would like to adopt a system that realizes a multiplayer function that only charges for the amount used.

I would like to know the best way to achieve multiplayer functionality at a lower cost than Photon. By the way, Photon costs $800 per month for the cheapest plan.

By the way, I would like to handle it without using AR Cloud, and I am thinking of using marker tracking or importing mapping data.


Hi @tokufxug,

One low cost option to creating shared experiences on the Magic Leap 2 device is to create a Peer-2-Peer connection between the headsets with WiFi Direct Service Discovery.

With a WiFi Direct connection between the devices I have been able to send messages over a basic sockets layer to share and synchronize the experience between the different users.


Hi @dlamb,
Thank you for your prompt reply.
When you implemented using WiFi Direct, did you develop it in the Unity Editor?
Have you developed using the following Asset? Or did you develop native Android libraries?

I also noticed that Wi-Fi Direct is listed in the Know Issue in the release notes from a year ago.

I apologize for being so selfish,If you could share some sample code or a sample project with us, it would be greatly appreciated.

Best regards.

Hi @tokufxug we are working on a sample that demonstrates how to create a multi-user experiences using Unity's Netcode, which supports a wide range of transports such as Mirror and Pun2.

1 Like

The approach I took was to create a Unity Android plugin that leverages the Android WiFi Direct Service Discovery APIs Usar o Wi-Fi Direct (P2P) para descoberta de serviços  |  Desenvolvedores Android  |  Android Developers.
I have not tried that Asset from the Unity store.

I believe the WiFi Direct issue mentioned in the Aug 2022 release notes may have been an older settings related issue, but I have found that WiFi Direct is working for me in the latest release build.

What I have working at the moment is not ready for public release, however knowing that others may be interested I will look into the option of possibly sharing a sample of WiFi Direct of some sort.


@kbabilinski I apologize for the late response. It's been quite a few days. Are the samples available?

Hi @tokufxug,

I am happy to share that the sample Wi-Fi Direct Service Discovery - Unity plug-in just went live:

And there is also a sample Unity project to demonstrate it in use:

Please let me know if you have any questions.


@tokufxug your original mentioned:

I am currently considering developing a multiplayer feature in Unity using several Magic Leap 2 devices.

but you were asking elsewhere if you required at least two Magic Leap 2 devices to to try this sample shared experience.

Ideally yes, the Unity Shared Experience sample has only been tested on Magic Leap 2 devices so far.

However, the sample Android native Wi-Fi Direct plug-in has nothing Magic Leap 2 specific in it. Some of the early tests for this Wi-Fi Direct work were done between a Android phone and an ML2, so I have no reason to believe that it will not also work on other Android based headsets or devices that have the required support for Wi-Fi Direct.

The Unity sample app also leverages MRTK3 which should also help with the cross platform potential for the provided sample app. It might require tweaking the MR Rig to build for other Android headsets, not sure. The sample source code is pretty straight forward and should provide a good example of how to work with the plug-in.

Alternatively another option would be to build a compatible or completely different app by just using the plug-in. That app could be for a shared immersive AR experience or any other kind of shared experience that someone might dream up between Wi-Fi Direct capable devices.

If you are limited to a single ML2, it might also be possible to create some sort of simulator to connect to which could send and received messages and commands with the ML2.

Please let me know if you have other questions.

I am currently verifying with over 20 devices and non-Magic Leap 2 devices. Before this release, we were using a sample of Photon Fusion provided by Magic Leap, but now there is a problem on the PC side and we have passed the error log to Magic Leap, but they have not responded.Can I connect more than 20 devices with Wifi Direct?

Hello @dlamb!

First of all, thank you for this very useful sample!

I am trying the sample with two ML2 devices.
The connection works fine (on the menu, the "WiFi Device Service" section becomes green), and we can even see when one of the users stops the rotation of the car.
However, as soon as one user moves the car (via drag or by pressing the full size button), the second user no longer sees the car. The second user will only be able to see the car again once one of the two users presses the "Reset" button.

To install the sample, I sent the given APK through the ML Hub as instructed. I also tried to update the Magic Leaps' OS to 1.4.1 but it still won't work.

Is there an additional setup required?

Based on what I have read, Wi-Fi direct is capable of connecting in theory up to potentially 254 devices per group. However, we have not verified large numbers of connections.

The JavaSockets communication layer in the sample plug-in currently uses a thread pool that is set to be up to 10 threads. The thread pool size would need to be adjusted, however that might also introduce performance issues so it might be necessary to find another way to manage the JavaSockets for a larger number of devices.

Ultimately Wi-Fi Direct is just a Peer-to-peer networking protocol for the connection layer. Depending on what you are trying to accomplish, there may be other challenges to overcome when trying to get over 20 devices of any sort connected and communicating together at the same time.

Are you able to share more information about the scenario?

Hi @maria.princi,

Welcome to the Magic Leap developer forums and thank you for taking the time to look at these new developer samples!

It sounds like the connection is working correctly and that the two devices are communicating with each other if the Reset, Rotate and Windows Open operations are working correctly. That is a very good sign that the basics are working.

The transformations of the 3D model also rely on a shared origin.

First question I have is: did both users first move, place and rotate the round stage (that is initially underneath the car) to an agreed common location in the room so that the sample application can establish a common origin?

Hi @dlamb,

Thank you for the quick answer! Yes, everything seems to be working fine except for the position...

Yes, we have both placed the round stage at the same position and rotation

Hi @maria.princi,

After some digging and experimentation I believe I was able to reproduce the results that you described.

I found that when I changed the Language settings on the ML2 to a language with a locale that uses a comma "," character for the numeric decimal separator, that the transform based message commands like Move, Scale and Orientation in the developer sample were failing to parse correctly when sent to another device.

I have updated both the source code and the build of the apk in the Shared Experience dev sample repository with what I believe is a fix to the issue.

After installing and running this updated version on to your devices, please let me know if that corrects the issue for you as well.

Hello @dlamb,

I set the language to English on both devices and everything is working perfectly now.
If I have enough time I will test the updated sample.
Thank you so much!!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.