Home > AI > Uncategorized

ARKit创建多玩家(翻译)

传输ARKit世界映射数据在附近设备间,用MultiplayerConnectivity框架来构建共享基础。

Demo:CreatingAMultiuserARExperience

https://developer.apple.com/documentation/arkit/creating_a_multiuser_ar_experience

 

》MultipeerSession.swift

#import MultipeerConnectivity

 

class MultipeerSession: NSObject{

static let serviceType : “ar”

 

private var session : MCSession!

private var browser : MCNearbyServiceBrowser!

private var advertiser : MCNearbyServiceAdvertiser!

private var receiveDataHandle : ((Data, MCPeerID) -> Void)

 

init(receiveDataHandle: ((Data, MCPeerID) -> Void)?) {

self.receiveDataHandle = receiveDataHandle

super.init

 

//set session

session = MCSession()

session.delegate = self

 

//set advertiser

advertiser = MCNearbyServiceAdvertiser()

advertiser.delegate = self

session.startAdvertising()

 

//set browser

browser = MCNearbyServiceBrowser()

browser.delegate = self

session.startBrowsing()

}

 

func send(data: Data, peer: MCPeerID){

do{

try session.send(data: data, to: session.connectedDevice)

} catch {

print(“error”)

}

}

 

var connectedDevice: [MCPeerID] {

return session.connectedDevices

}

 

}

 

extension MultipeerSession: MCSessionDelegate {

func session(_ session: MCSession, peer: MCPeerID, state: MCSessionState){

//not used

}

func session(_ session: MCSession, disReceiveData){

self.receiveDataHandle()

}

 

func session(_ session: MCSession){

}

 

func session(_ session MCSession){

}

 

func session (_ session MCSession){

}

 

extension MultipeerSession: MCNearbyServiceAdvertiserDelegate{

 

 

}

 

extension MultipeerSession: MCNearbyServiceBrowserDelegate{

 

}

 

 

 

 

 

 

Related posts:

Leave a Reply