浏览代码

Use Multipeer instead of TCP for collaborative session sample

/3.1
Tim Mowrer 6 年前
当前提交
933a862d
共有 3 个文件被更改,包括 176 次插入4 次删除
  1. 53
      Assets/Scenes/ARCollaborationData/ARCollaborationDataExample.unity
  2. 116
      Assets/Scenes/ARCollaborationData/CollaborativeSession.cs
  3. 11
      Assets/Scenes/ARCollaborationData/CollaborativeSession.cs.meta

53
Assets/Scenes/ARCollaborationData/ARCollaborationDataExample.unity


m_Name:
m_EditorClassIdentifier:
m_Session: {fileID: 1297001902}
--- !u!1 &488781145
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 488781147}
- component: {fileID: 488781146}
m_Layer: 0
m_Name: CollaborativeSession
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &488781146
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 488781145}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a7c1d01ad3d754ef788a30c12b724743, type: 3}
m_Name:
m_EditorClassIdentifier:
m_ARSession: {fileID: 1297001902}
m_SessionName: arf-collab-demo
--- !u!4 &488781147
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 488781145}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 7
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &498795603
GameObject:
m_ObjectHideFlags: 0

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &1342067404
RectTransform:
m_ObjectHideFlags: 0

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &1550501584
RectTransform:
m_ObjectHideFlags: 0

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &1615366624
MonoBehaviour:
m_ObjectHideFlags: 0

m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &2093015781
RectTransform:
m_ObjectHideFlags: 0

116
Assets/Scenes/ARCollaborationData/CollaborativeSession.cs


using UnityEngine;
using Unity.iOS.Multipeer;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARKit;
public class CollaborativeSession : MonoBehaviour
{
[SerializeField]
ARSession m_ARSession;
public ARSession arSession
{
get { return m_ARSession; }
set { m_ARSession = value; }
}
[SerializeField]
string m_SessionName;
public string sessionName
{
get { return m_SessionName; }
set { m_SessionName = value; }
}
MCSession m_MCSession;
ARKitSessionSubsystem GetSubsystem()
{
if (m_ARSession == null)
return null;
return m_ARSession.subsystem as ARKitSessionSubsystem;
}
void Awake()
{
m_MCSession = new MCSession(SystemInfo.deviceName, m_SessionName);
}
void OnEnable()
{
var subsystem = GetSubsystem();
if (!ARKitSessionSubsystem.supportsCollaboration || subsystem == null)
{
enabled = false;
return;
}
subsystem.collaborationEnabled = true;
m_MCSession.Enabled = true;
}
void OnDisable()
{
m_MCSession.Enabled = false;
var subsystem = GetSubsystem();
if (subsystem != null)
subsystem.collaborationEnabled = false;
}
void Update()
{
var subsystem = GetSubsystem();
if (subsystem == null)
return;
// Check for new collaboration data
while (subsystem.collaborationDataCount > 0)
{
using (var collaborationData = subsystem.DequeueCollaborationData())
{
if (collaborationData.priority == ARCollaborationDataPriority.Critical)
{
CollaborationNetworkingIndicator.NotifyHasCollaborationData();
if (m_MCSession.ConnectedPeerCount == 0)
continue;
using (var serializedData = collaborationData.ToSerialized())
using (var data = new NSData(serializedData.bytes, false))
{
m_MCSession.SendToAllPeers(data, MCSessionSendDataMode.Reliable);
Logger.Log($"Sent {data.Length} bytes of collaboration data.");
}
}
}
}
// Check for incoming data
while (m_MCSession.ReceivedDataQueueSize > 0)
{
CollaborationNetworkingIndicator.NotifyIncomingDataReceived();
using (var data = m_MCSession.DequeueReceivedData())
using (var collaborationData = new ARCollaborationData(data.Bytes))
{
if (collaborationData.valid)
{
subsystem.UpdateWithCollaborationData(collaborationData);
Logger.Log($"Received {data.Bytes.Length} bytes of collaboration data.");
}
else
{
Logger.Log($"Received {data.Bytes.Length} bytes from remote, but the collaboration data was not valid.");
}
}
}
}
void OnDestroy()
{
m_MCSession.Dispose();
}
}

11
Assets/Scenes/ARCollaborationData/CollaborativeSession.cs.meta


fileFormatVersion: 2
guid: a7c1d01ad3d754ef788a30c12b724743
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存