Shark Deng

Interactive Designer @Shanju Design in Shanghai

touched caffe / mxnet / python / wordpress / bootstrap / Arduino / Raspberry Pi / webgl / three.js / PIXI / 3dMax

Email

Github

ARKit创建多玩家(翻译)

2019-11-13 00:24:01

传输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{

 

}

 

 

 

 

 

 



——By SharkDeng
如果你喜欢我的文章,欢迎红包赞赏

Leave a Reply

Your email address will not be published. Required fields are marked *