RealityKit Entity synchronization is always nil
Asked Answered
D

1

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?

Dichromatism answered 23/11, 2019 at 13:4 Comment(0)
C
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

Catchup answered 2/3, 2020 at 18:9 Comment(3)
It doesn't work. I have a MultipeerConnectivityService and isCollaborationEnabled flag is on but synchronization component is nilDichromatism
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.)"Etter
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.Etter

© 2022 - 2024 — McMap. All rights reserved.