1

Trying to build the multiplayer experience with RealityKit. But the synchronization component of the all entities is always nil even if I set it explicitly.

dump(entity.synchronization)  // nil
entity.synchronization = SynchronizationComponent()
dump(entity.synchronization)  // nil

As a result, the virtual content is not shared. What I'm doing wrong?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220

1 Answers1

0

ViewController.swift

At first, you have to setup a service MultipeerConnectivityService that provides scene synchronization among all peers in a multipeer connectivity session MCSession.

import RealityKit
import MultipeerConnectivity

var arView = ARView(frame: .zero)
private let serviceName = "service"

override init() {

    let mcPeerID = MCPeerID(displayName: UIDevice.current.name)

    self.serviceAdvertiser.delegate = self
    self.serviceAdvertiser.startAdvertisingPeer()

    self.serviceBrowser.delegate = self
    self.serviceBrowser.startBrowsingForPeers()

    let session = MCSession(peer: mcPeerID, securityIdentity: nil, 
                                        encryptionPreference: .required)

    arView.scene.synchronizationService = 
                         try? MultipeerConnectivityService(session: session)
}


If you implemented ARWorldTrackinConfig in ARKit, don't forget to turn isCollaborationEnabled instance property On that opts you in to a peer-to-peer multiuser AR experience.

import ARKit

let collabConfig = ARWorldTrackingConfiguration()
collabConfig.isCollaborationEnabled = true
arView.session.run(collabConfig)

info.plist

Secondly you have to setup NSLNUD and NSBonjourServices in property list file:

<key>NSLocalNetworkUsageDescription</key>
    <string>LNUD</string>

<key>NSBonjourServices</key>
    <array>
        <string>_service._tcp</string>     // Transmission Control Protocol 
        <string>_service._udp</string>     // User Datagram Protocol
    </array>

enter image description here

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    It doesn't work. I have a MultipeerConnectivityService and isCollaborationEnabled flag is on but synchronization component is nil – Alexander Gaidukov Apr 06 '20 at 19:12
  • 1
    I'm having the same problem. For further information try using a catch instead of "try?". For mine I just get the following uninformative error. "The operation couldn’t be completed. (RealityKit.MultipeerConnectivityService.SynchronizationError error 0.)" – Owen Godfrey Sep 18 '20 at 11:04
  • 1
    I seem to have resolved the problem, but I’m not sure what I did. I think part of the problem was that SwiftUI was accidentally setting up synchronisation in previews at the same time… or it might be that I increased the versions to iOS 14. For whatever reason it appears that the code itself was correct. – Owen Godfrey Sep 19 '20 at 12:06