-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathCollaborativeSession.cs
144 lines (122 loc) · 4.96 KB
/
CollaborativeSession.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using UnityEngine;
using UnityEngine.XR.ARFoundation;
#if UNITY_IOS && !UNITY_EDITOR
using Unity.iOS.Multipeer;
using UnityEngine.XR.ARKit;
#endif
namespace UnityEngine.XR.ARFoundation.Samples
{
[RequireComponent(typeof(ARSession))]
public class CollaborativeSession : MonoBehaviour
{
[SerializeField]
[Tooltip("The name for this network service. It should be 15 characters or less and can contain ASCII, lowercase letters, numbers, and hyphens.")]
string m_ServiceType;
/// <summary>
/// The name for this network service.
/// See <a href="https://developer.apple.com/documentation/multipeerconnectivity/mcnearbyserviceadvertiser">MCNearbyServiceAdvertiser</a>
/// for the purpose of and restrictions on this name.
/// </summary>
public string serviceType
{
get => m_ServiceType;
set => m_ServiceType = value;
}
ARSession m_ARSession;
void DisableNotSupported(string reason)
{
enabled = false;
Logger.Log(reason);
}
void OnEnable()
{
#if UNITY_IOS && !UNITY_EDITOR
var subsystem = GetSubsystem();
if (!ARKitSessionSubsystem.supportsCollaboration || subsystem == null)
{
DisableNotSupported("Collaborative sessions require iOS 13.");
return;
}
subsystem.collaborationRequested = true;
m_MCSession.Enabled = true;
#else
DisableNotSupported("Collaborative sessions are an ARKit 3 feature; This platform does not support them.");
#endif
}
#if UNITY_IOS && !UNITY_EDITOR
MCSession m_MCSession;
ARKitSessionSubsystem GetSubsystem()
{
if (m_ARSession == null)
return null;
return m_ARSession.subsystem as ARKitSessionSubsystem;
}
void Awake()
{
m_ARSession = GetComponent<ARSession>();
m_MCSession = new MCSession(SystemInfo.deviceName, m_ServiceType);
}
void OnDisable()
{
m_MCSession.Enabled = false;
var subsystem = GetSubsystem();
if (subsystem != null)
subsystem.collaborationRequested = false;
}
void Update()
{
var subsystem = GetSubsystem();
if (subsystem == null)
return;
// Check for new collaboration data
while (subsystem.collaborationDataCount > 0)
{
using (var collaborationData = subsystem.DequeueCollaborationData())
{
CollaborationNetworkingIndicator.NotifyHasCollaborationData();
if (m_MCSession.ConnectedPeerCount == 0)
continue;
using (var serializedData = collaborationData.ToSerialized())
using (var data = NSData.CreateWithBytesNoCopy(serializedData.bytes))
{
m_MCSession.SendToAllPeers(data, collaborationData.priority == ARCollaborationDataPriority.Critical
? MCSessionSendDataMode.Reliable
: MCSessionSendDataMode.Unreliable);
CollaborationNetworkingIndicator.NotifyOutgoingDataSent();
// Only log 'critical' data as 'optional' data tends to come every frame
if (collaborationData.priority == ARCollaborationDataPriority.Critical)
{
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);
if (collaborationData.priority == ARCollaborationDataPriority.Critical)
{
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();
}
#endif
}
}