Using the MultipeerConnectivity framework to share data between nearby devices. Works with iOS and tvOS.
- From Source
- Clone or download and copy
Assets/UnityMultipeerConnectivity
,Assets/UnitySwift
andAssets/Plugin
directories to your own project.
- Clone or download and copy
- Check Examples
- see exemple scene in
Assets/Scenes
and the scripts used inAssets/Scripts
.
- see exemple scene in
public class MultipeerSender : MonoBehaviour
{
public void SendData(string data)
{
UnityMCSessionNativeInterface.GetMcSessionNativeInterface()
.SendToAllPeers(SystemInfo.deviceName + "," + data);
}
}
public class MultipeerReceiver : MonoBehaviour
{
public Text text;
void Start()
{
UnityMCSessionNativeInterface.GetMcSessionNativeInterface()
.DataReceivedAsObservable()
.Subscribe(Received)
.AddTo(this);
}
void Received(byte[] bytes)
{
string dataReceived = System.Text.Encoding.UTF8.GetString(bytes);
string[] tokens = dataReceived.Split(',');
text.text += "\n"+ "Received " + tokens[1] + " from " + tokens[0];
}
}
See Example Sene.
Based on noir-neo/UnityARKitMultipeerConnectivity.
- Unity 2018.3+
- iOS 12.0+
- Xcode 10.0+