Closed tmmychn closed 3 years ago
ARCollaborationData is a specific data type in ARKit and cannot be extended to include custom data.
I think you are asking a more general networking question. If you want to share other types of data, you'll need to serialize it to a byte array and transmit it over the network. There are a number of ways to do this (see https://docs-multiplayer.unity3d.com/ for example).
If you want to extend the collaboration data sample in this repo (and your app is iOS only), then you can use the MCSession iOS framework which is used by that sample. See https://github.com/Unity-Technologies/arfoundation-samples/issues/790 for more discussion on this topic.
@tdmowrer Thank you Tim for the prompt reply. I did actually follow along with the discussion on #790, I manage to serialise the data into NSData and send the data with the MCSession function SendToAllPeers but I couldn't understand how to retrieve the sent NSData, convert it back to bytes and deserialise them.
//CLASS
[System.Serializable]
public class PrefabSender
{
public static PrefabSender instance;
public int prefabID;
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
public byte[] Serialize()
{
using (MemoryStream m = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(m))
{
writer.Write(prefabID);
}
return m.ToArray();
}
}
public static PrefabSender Deserialize(byte[] data)
{
Debug.Log("Deserialized");
PrefabSender result = new PrefabSender();
using (MemoryStream m = new MemoryStream(data))
{
using (BinaryReader reader = new BinaryReader(m))
{
result.prefabID = reader.ReadInt32();
}
}
return result;
}
}
//Send Prefeb Data
PrefabSender NewSender = new PrefabSender();
byte[] CollabPrefabByte = NewSender.Serialize();
unsafe
{
fixed (byte* ptr = CollabPrefabByte)
{
var slice = NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<byte>(ptr, 1, CollabPrefabByte.Length);
using (var data = NSData.CreateWithBytesNoCopy(slice))
{
m_MCSession.SendToAllPeers(data, 0);
Logger.Log($"Sent {data.Length} bytes of Prefab data.");
}
}
}
@tdmowrer Hi, I am still stuck on this. Not sure how to get the received NSData and convert the data back to bytes to be deserialised. Would really appreciate it if you could steer me in the right direction, thank you !
Have you seen what we do in CollaborativeSession.cs?
That snippet gets the bytes that were transmitted and then interprets them as ARCollaborationData. You would need to run the bytes through your deserialization.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi,
Extending the ARCollaborationData sample, I understand that the collaborative session only shares each participant's pose and all reference points, I would like to know how to share data such as the prefab instantiated by each participant ? Also, is it possible to interact with the instantiated prefabs and update the position and rotation ?
Any solutions or suggestions are appreciated. Thank you !