浏览代码

Add MultipeerConnectivity support to samples

/3.1
Tim Mowrer 5 年前
当前提交
eb60b20a
共有 38 个文件被更改,包括 1201 次插入0 次删除
  1. 8
      Assets/Scripts/Multipeer.meta
  2. 100
      Assets/Scripts/Multipeer/MCSession.cs
  3. 11
      Assets/Scripts/Multipeer/MCSession.cs.meta
  4. 18
      Assets/Scripts/Multipeer/MCSessionSendDataMode.cs
  5. 11
      Assets/Scripts/Multipeer/MCSessionSendDataMode.cs.meta
  6. 62
      Assets/Scripts/Multipeer/NSData.cs
  7. 11
      Assets/Scripts/Multipeer/NSData.cs.meta
  8. 56
      Assets/Scripts/Multipeer/NSError.cs
  9. 11
      Assets/Scripts/Multipeer/NSError.cs.meta
  10. 18
      Assets/Scripts/Multipeer/NSErrorException.cs
  11. 11
      Assets/Scripts/Multipeer/NSErrorException.cs.meta
  12. 53
      Assets/Scripts/Multipeer/NSMutableData.cs
  13. 11
      Assets/Scripts/Multipeer/NSMutableData.cs.meta
  14. 15
      Assets/Scripts/Multipeer/NSMutableDataExtensions.cs
  15. 11
      Assets/Scripts/Multipeer/NSMutableDataExtensions.cs.meta
  16. 79
      Assets/Scripts/Multipeer/NSString.cs
  17. 11
      Assets/Scripts/Multipeer/NSString.cs.meta
  18. 17
      Assets/Scripts/Multipeer/NativeApi.cs
  19. 11
      Assets/Scripts/Multipeer/NativeApi.cs.meta
  20. 8
      Assets/Scripts/Multipeer/NativeCode.meta
  21. 62
      Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate-C-Bridge.m
  22. 37
      Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate-C-Bridge.m.meta
  23. 13
      Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.h
  24. 27
      Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.h.meta
  25. 132
      Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.m
  26. 105
      Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.m.meta
  27. 30
      Assets/Scripts/Multipeer/NativeCode/NSData-C-Bridge.m
  28. 37
      Assets/Scripts/Multipeer/NativeCode/NSData-C-Bridge.m.meta
  29. 12
      Assets/Scripts/Multipeer/NativeCode/NSError-C-Bridge.m
  30. 37
      Assets/Scripts/Multipeer/NativeCode/NSError-C-Bridge.m.meta
  31. 22
      Assets/Scripts/Multipeer/NativeCode/NSMutableData-C-Bridge.m
  32. 37
      Assets/Scripts/Multipeer/NativeCode/NSMutableData-C-Bridge.m.meta
  33. 52
      Assets/Scripts/Multipeer/NativeCode/NSString-C-Bridge.m
  34. 37
      Assets/Scripts/Multipeer/NativeCode/NSString-C-Bridge.m.meta
  35. 17
      Assets/Scripts/Multipeer/SerializedARCollaborationDataExtensions.cs
  36. 11
      Assets/Scripts/Multipeer/SerializedARCollaborationDataExtensions.cs.meta

8
Assets/Scripts/Multipeer.meta


fileFormatVersion: 2
guid: ab5ca8171efd6403fa3e0f422eccde1e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

100
Assets/Scripts/Multipeer/MCSession.cs


using System;
using System.Runtime.InteropServices;
namespace Unity.iOS.Multipeer
{
[StructLayout(LayoutKind.Sequential)]
public struct MCSession : IDisposable, IEquatable<MCSession>
{
IntPtr m_Ptr;
public bool Created => m_Ptr != IntPtr.Zero;
public bool Enabled
{
get
{
if (!Created)
return false;
return GetEnabled(this);
}
set
{
if (!Created && value)
throw new InvalidOperationException();
SetEnabled(this, value);
}
}
public MCSession(string peerName, string serviceType)
{
if (peerName == null)
throw new ArgumentNullException(nameof(peerName));
if (serviceType == null)
throw new ArgumentNullException(nameof(serviceType));
using (var peerName_NSString = new NSString(peerName))
using (var serviceType_NSString = new NSString(serviceType))
{
m_Ptr = InitWithName(peerName_NSString, serviceType_NSString);
}
}
public void SendToAllPeers(NSData data, MCSessionSendDataMode mode)
{
if (!Created)
throw new InvalidOperationException($"The {typeof(MCSession).Name} has not been created.");
if (!data.Created)
throw new ArgumentException($"'{nameof(data)}' is not valid.", nameof(data));
using (var error = SendToAllPeers(this, data, mode))
{
if (error.Valid)
throw error.ToException();
}
}
public int ReceivedDataQueueSize => GetReceivedDataQueueSize(this);
public NSData DequeueReceivedData()
{
if (!Created)
throw new InvalidOperationException($"The {typeof(MCSession).Name} has not been created.");
return DequeueReceivedData(this);
}
public int ConnectedPeerCount => GetConnectedPeerCount(this);
public void Dispose() => NativeApi.CFRelease(ref m_Ptr);
public override int GetHashCode() => m_Ptr.GetHashCode();
public override bool Equals(object obj) => (obj is MCSession) && Equals((MCSession)obj);
public bool Equals(MCSession other) => m_Ptr == other.m_Ptr;
public static bool operator==(MCSession lhs, MCSession rhs) => lhs.Equals(rhs);
public static bool operator!=(MCSession lhs, MCSession rhs) => !lhs.Equals(rhs);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_sendToAllPeers")]
static extern NSError SendToAllPeers(MCSession self, NSData data, MCSessionSendDataMode mode);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_initWithName")]
static extern IntPtr InitWithName(NSString name, NSString serviceType);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_ReceivedDataQueueSize")]
static extern int GetReceivedDataQueueSize(MCSession self);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_DequeueReceivedData")]
static extern NSData DequeueReceivedData(MCSession self);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_ConnectedPeerCount")]
static extern int GetConnectedPeerCount(MCSession self);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_SetEnabled")]
static extern void SetEnabled(MCSession self, bool enabled);
[DllImport("__Internal", EntryPoint="UnityMC_Delegate_GetEnabled")]
static extern bool GetEnabled(MCSession self);
}
}

11
Assets/Scripts/Multipeer/MCSession.cs.meta


fileFormatVersion: 2
guid: f0dd3cf9695cb42f7b9ffa20d3a59011
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
Assets/Scripts/Multipeer/MCSessionSendDataMode.cs


namespace Unity.iOS.Multipeer
{
/// <summary>
/// MCSession send modes
/// </summary>
public enum MCSessionSendDataMode
{
/// <summary>
/// Guaranteed reliable and in-order delivery.
/// </summary>
Reliable,
/// <summary>
/// Sent immediately without queuing, no guaranteed delivery.
/// </summary>
Unreliable
}
}

11
Assets/Scripts/Multipeer/MCSessionSendDataMode.cs.meta


fileFormatVersion: 2
guid: 5673d45a5dae34743841658416feef52
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

62
Assets/Scripts/Multipeer/NSData.cs


using System;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace Unity.iOS.Multipeer
{
[StructLayout(LayoutKind.Sequential)]
public struct NSData : IDisposable, IEquatable<NSData>
{
IntPtr m_Ptr;
internal NSData(IntPtr existing) => m_Ptr = existing;
public bool Created => m_Ptr != IntPtr.Zero;
public int Length => Created ? GetLength(this) : 0;
public unsafe NSData(NativeSlice<byte> bytes, bool copy)
{
if (copy)
{
m_Ptr = CreateWithBytes(bytes.GetUnsafePtr(), bytes.Length);
}
else
{
m_Ptr = CreateWithBytesNoCopy(bytes.GetUnsafePtr(), bytes.Length, false);
}
}
public unsafe NativeSlice<byte> Bytes
{
get
{
if (!Created)
throw new InvalidOperationException($"The {typeof(NSData).Name} has not been created.");
return NativeSliceUnsafeUtility.ConvertExistingDataToNativeSlice<byte>(GetBytes(this), 1, GetLength(this));
}
}
public void Dispose() => NativeApi.CFRelease(ref m_Ptr);
public override int GetHashCode() => m_Ptr.GetHashCode();
public override bool Equals(object obj) => (obj is NSData) && Equals((NSData)obj);
public bool Equals(NSData other) => m_Ptr == other.m_Ptr;
public static bool operator==(NSData lhs, NSData rhs) => lhs.Equals(rhs);
public static bool operator!=(NSData lhs, NSData rhs) => !lhs.Equals(rhs);
[DllImport("__Internal", EntryPoint="UnityMC_NSData_getLength")]
static extern int GetLength(NSData self);
[DllImport("__Internal", EntryPoint="UnityMC_NSData_getBytes")]
static extern unsafe void* GetBytes(NSData self);
[DllImport("__Internal", EntryPoint="UnityMC_NSData_createWithBytes")]
static extern unsafe IntPtr CreateWithBytes(void* bytes, int length);
[DllImport("__Internal", EntryPoint="UnityMC_NSData_createWithBytesNoCopy")]
static extern unsafe IntPtr CreateWithBytesNoCopy(void* bytes, int length, bool freeWhenDone);
}
}

11
Assets/Scripts/Multipeer/NSData.cs.meta


fileFormatVersion: 2
guid: 0aac506fdc0254d79b2479e6735d7ba3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

56
Assets/Scripts/Multipeer/NSError.cs


using System;
using System.Runtime.InteropServices;
namespace Unity.iOS.Multipeer
{
[StructLayout(LayoutKind.Sequential)]
public struct NSError : IDisposable, IEquatable<NSError>
{
IntPtr m_Ptr;
public bool Valid => m_Ptr != IntPtr.Zero;
public NSErrorException ToException()
{
return new NSErrorException(Code, Description);
}
public long Code
{
get
{
if (!Valid)
throw new InvalidOperationException($"The {typeof(NSError).Name} is not valid.");
return GetCode(this);
}
}
public string Description
{
get
{
if (!Valid)
throw new InvalidOperationException($"The {typeof(NSError).Name} is not valid.");
using (var description = GetLocalizedDescription(this))
{
return description.ToString();
}
}
}
public void Dispose() => NativeApi.CFRelease(ref m_Ptr);
public override int GetHashCode() => m_Ptr.GetHashCode();
public override bool Equals(object obj) => (obj is NSError) && Equals((NSError)obj);
public bool Equals(NSError other) => m_Ptr == other.m_Ptr;
public static bool operator==(NSError lhs, NSError rhs) => lhs.Equals(rhs);
public static bool operator!=(NSError lhs, NSError rhs) => !lhs.Equals(rhs);
[DllImport("__Internal", EntryPoint="UnityMC_NSError_code")]
static extern long GetCode(NSError error);
[DllImport("__Internal", EntryPoint="UnityMC_NSError_localizedDescription")]
static extern NSString GetLocalizedDescription(NSError error);
}
}

11
Assets/Scripts/Multipeer/NSError.cs.meta


fileFormatVersion: 2
guid: 10a04c15626774337abc28e3ed0dc046
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

18
Assets/Scripts/Multipeer/NSErrorException.cs


using System;
namespace Unity.iOS.Multipeer
{
public class NSErrorException : Exception
{
public NSErrorException(long code, string description)
: base($"NSError {code}: {description}")
{
Code = code;
Description = description;
}
public long Code { get; private set; }
public string Description { get; private set; }
}
}

11
Assets/Scripts/Multipeer/NSErrorException.cs.meta


fileFormatVersion: 2
guid: 491dc7d106d7642499e0ccfe7d11bf70
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

53
Assets/Scripts/Multipeer/NSMutableData.cs


using System;
using System.Runtime.InteropServices;
namespace Unity.iOS.Multipeer
{
[StructLayout(LayoutKind.Sequential)]
public struct NSMutableData : IDisposable, IEquatable<NSMutableData>
{
IntPtr m_Ptr;
internal NSMutableData(IntPtr existing) => m_Ptr = existing;
public bool Created => m_Ptr != IntPtr.Zero;
public NSData ToNSData() => new NSData(m_Ptr);
// public void Append(IntPtr bytes, int length)
// {
// if (!Created)
// throw new InvalidOperationException($"Cannot append to the {typeof(NSMutableData).Name} because it has not been created.");
// Append(m_Ptr, bytes, length);
// }
// public static NSMutableData CreateEmpty() => new NSMutableData(Init());
// public static NSMutableData CreateWithBytes(IntPtr bytes, int length)
// => new NSMutableData(InitWithBytes(bytes, length));
// public static NSMutableData CreateWithBytesNoCopy(IntPtr bytes, int length, bool freeBytesOnDisposal = false)
// => new NSMutableData(InitWithBytesNoCopy(bytes, length, freeBytesOnDisposal));
public void Dispose() => NativeApi.CFRelease(ref m_Ptr);
public override int GetHashCode() => m_Ptr.GetHashCode();
public override bool Equals(object obj) => (obj is NSMutableData) && Equals((NSMutableData)obj);
public bool Equals(NSMutableData other) => m_Ptr == other.m_Ptr;
public static bool operator==(NSMutableData lhs, NSMutableData rhs) => lhs.Equals(rhs);
public static bool operator!=(NSMutableData lhs, NSMutableData rhs) => !lhs.Equals(rhs);
// [DllImport("__Internal", EntryPoint="UnityMultipeer_NSMutableData_init")]
// static extern IntPtr Init();
// [DllImport("__Internal", EntryPoint="UnityMultipeer_NSMutableData_initWithBytes")]
// static extern IntPtr InitWithBytes(IntPtr bytes, int length);
// [DllImport("__Internal", EntryPoint="UnityARKit_NSMutableData_initWithBytesNoCopy")]
// static extern IntPtr InitWithBytesNoCopy(IntPtr bytes, int length, bool freeBytesOnDisposal);
// [DllImport("__Internal", EntryPoint="UnityARKit_NSMutableData_append")]
// static extern IntPtr Append(IntPtr ptr, IntPtr bytes, int length);
}
}

11
Assets/Scripts/Multipeer/NSMutableData.cs.meta


fileFormatVersion: 2
guid: 57f6bd29a74244f9590efc743e8ee5df
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

15
Assets/Scripts/Multipeer/NSMutableDataExtensions.cs


using System;
namespace Unity.iOS.Multipeer.Unsafe
{
public static unsafe class NSMutableDataUnsafeUtility
{
// public static NSMutableData CreateWithBytes(byte[] bytes, int offset, int length)
// {
// fixed(byte* ptr = &bytes[offset])
// {
// return NSMutableData.CreateWithBytes(new IntPtr(ptr), length);
// }
// }
}
}

11
Assets/Scripts/Multipeer/NSMutableDataExtensions.cs.meta


fileFormatVersion: 2
guid: 169ce82dbd54c49d4ae6fb74311fa48c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

79
Assets/Scripts/Multipeer/NSString.cs


using System;
using System.Runtime.InteropServices;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
namespace Unity.iOS.Multipeer
{
public struct NSString : IDisposable, IEquatable<NSString>
{
IntPtr m_Ptr;
internal NSString(IntPtr existing) => m_Ptr = existing;
public NSString(string text) => m_Ptr = CreateWithString(text, text.Length);
public NSString(NSData serializedString)
{
if (!serializedString.Created)
throw new ArgumentException("The serialized string is not valid.", nameof(serializedString));
m_Ptr = Deserialize(serializedString);
}
public bool Created => m_Ptr != IntPtr.Zero;
public int Length => GetLength(this);
public override unsafe string ToString()
{
if (!Created)
return string.Empty;
using (var buffer = new NativeArray<byte>(GetLengthOfBytes(this), Allocator.TempJob, NativeArrayOptions.UninitializedMemory))
{
if (GetBytes(this, buffer.GetUnsafePtr(), buffer.Length))
{
return Marshal.PtrToStringUni(new IntPtr(buffer.GetUnsafePtr()), Length);
}
else
{
return string.Empty;
}
}
}
public NSData Serialize()
{
if (!Created)
throw new InvalidOperationException($"The {typeof(NSString).Name} has not been created.");
return Serialize(this);
}
public void Dispose() => NativeApi.CFRelease(ref m_Ptr);
public override int GetHashCode() => m_Ptr.GetHashCode();
public override bool Equals(object obj) => (obj is NSString) && Equals((NSString)obj);
public bool Equals(NSString other) => m_Ptr == other.m_Ptr;
public static bool operator==(NSString lhs, NSString rhs) => lhs.Equals(rhs);
public static bool operator!=(NSString lhs, NSString rhs) => !lhs.Equals(rhs);
[DllImport("__Internal", EntryPoint="UnityMultipeer_NSString_createWithString")]
static extern IntPtr CreateWithString([MarshalAs(UnmanagedType.LPWStr)] string text, int length);
[DllImport("__Internal", EntryPoint="UnityMultipeer_NSString_lengthOfBytesUsingEncoding")]
static extern int GetLengthOfBytes(NSString self);
[DllImport("__Internal", EntryPoint="UnityMultipeer_NSString_getLength")]
static extern int GetLength(NSString self);
[DllImport("__Internal", EntryPoint="UnityMultipeer_NSString_getBytes")]
static extern unsafe bool GetBytes(NSString self, void* buffer, int length);
[DllImport("__Internal", EntryPoint="UnityMC_NSString_serialize")]
static extern NSData Serialize(NSString self);
[DllImport("__Internal", EntryPoint="UnityMC_NSString_deserialize")]
static extern IntPtr Deserialize(NSData data);
}
}

11
Assets/Scripts/Multipeer/NSString.cs.meta


fileFormatVersion: 2
guid: 2a2bbe15074f94af4b35f25e435174f6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

17
Assets/Scripts/Multipeer/NativeApi.cs


using System;
using System.Runtime.InteropServices;
namespace Unity.iOS.Multipeer
{
internal static class NativeApi
{
public static void CFRelease(ref IntPtr ptr)
{
CFRelease(ptr);
ptr = IntPtr.Zero;
}
[DllImport("__Internal", EntryPoint="UnityMultipeer_CFRelease")]
public static extern void CFRelease(IntPtr ptr);
}
}

11
Assets/Scripts/Multipeer/NativeApi.cs.meta


fileFormatVersion: 2
guid: 06e6b03cb95994416a492630b1c74fd0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Scripts/Multipeer/NativeCode.meta


fileFormatVersion: 2
guid: 841a08ffbdce6452182dcbfc65a3054c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

62
Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate-C-Bridge.m


#include "MultipeerDelegate.h"
typedef void* ManagedMultipeerDelegate;
typedef void* ManagedNSError;
ManagedMultipeerDelegate UnityMC_Delegate_initWithName(void* name_nsstring, void* serviceType_nsstring)
{
NSString* name = (__bridge NSString*)name_nsstring;
MultipeerDelegate* delegate = [[MultipeerDelegate alloc] initWithName:name
serviceType:(__bridge NSString*)serviceType_nsstring];
return (__bridge_retained void*)delegate;
}
ManagedNSError UnityMC_Delegate_sendToAllPeers(void* self, void* nsdata, int length, int mode)
{
NSData* data = (__bridge NSData*)nsdata;
MultipeerDelegate* delegate = (__bridge MultipeerDelegate*)self;
NSError* error = [delegate sendToAllPeers:data withMode:(MCSessionSendDataMode)mode];
return (__bridge_retained void*)error;
}
int UnityMC_Delegate_ReceivedDataQueueSize(void* self)
{
if (self == NULL)
return 0;
MultipeerDelegate* delegate = (__bridge MultipeerDelegate*)self;
return (int)delegate.queueSize;
}
void* UnityMC_Delegate_DequeueReceivedData(void* self)
{
MultipeerDelegate* delegate = (__bridge MultipeerDelegate*)self;
return (__bridge_retained void*)delegate.dequeue;
}
int UnityMC_Delegate_ConnectedPeerCount(void* self)
{
MultipeerDelegate* delegate = (__bridge MultipeerDelegate*)self;
return (int)delegate.connectedPeerCount;
}
void UnityMC_Delegate_SetEnabled(void* self, bool enabled)
{
MultipeerDelegate* delegate = (__bridge MultipeerDelegate*)self;
delegate.enabled = enabled;
}
bool UnityMC_Delegate_GetEnabled(void* self)
{
MultipeerDelegate* delegate = (__bridge MultipeerDelegate*)self;
return delegate.enabled;
}
void UnityMultipeer_CFRelease(void* ptr)
{
if (ptr)
{
CFRelease(ptr);
}
}

37
Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate-C-Bridge.m.meta


fileFormatVersion: 2
guid: f94297f07eee14b96b55b6ed49779df0
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings: {}
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

13
Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.h


#import <MultipeerConnectivity/MultipeerConnectivity.h>
@interface MultipeerDelegate : NSObject<MCSessionDelegate, MCNearbyServiceAdvertiserDelegate, MCNearbyServiceBrowserDelegate>
- (nullable instancetype)initWithName:(nonnull NSString *)name serviceType:(nonnull NSString*)serviceType;
- (nullable NSError*)sendToAllPeers:(nonnull NSData*)data withMode:(MCSessionSendDataMode)mode;
- (NSUInteger)connectedPeerCount;
- (NSUInteger)queueSize;
- (nonnull NSData*)dequeue;
@property BOOL enabled;
@end

27
Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.h.meta


fileFormatVersion: 2
guid: 598b4c4bd189e49b4b17a87ba0cf5418
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 1
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
userData:
assetBundleName:
assetBundleVariant:

132
Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.m


#import "MultipeerDelegate.h"
@implementation MultipeerDelegate
MCSession* m_Session;
MCPeerID* m_PeerID;
NSMutableArray* m_Queue;
MCNearbyServiceAdvertiser* m_ServiceAdvertiser;
MCNearbyServiceBrowser* m_ServiceBrowser;
BOOL m_Enabled;
- (instancetype)initWithName:(nonnull NSString *)name serviceType:(nonnull NSString *)serviceType
{
if (self = [super init])
{
m_Enabled = false;
m_Queue = [[NSMutableArray alloc] init];
m_PeerID = [[MCPeerID alloc] initWithDisplayName: name];
m_Session = [[MCSession alloc] initWithPeer:m_PeerID
securityIdentity:nil
encryptionPreference:MCEncryptionRequired];
m_Session.delegate = self;
m_ServiceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:m_PeerID
discoveryInfo:nil
serviceType:serviceType];
m_ServiceAdvertiser.delegate = self;
m_ServiceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:m_PeerID
serviceType:serviceType];
m_ServiceBrowser.delegate = self;
}
return self;
}
- (BOOL)enabled
{
return m_Enabled;
}
- (void)setEnabled:(BOOL)enabled
{
if (enabled)
{
[m_ServiceAdvertiser startAdvertisingPeer];
[m_ServiceBrowser startBrowsingForPeers];
}
else
{
[m_ServiceAdvertiser stopAdvertisingPeer];
[m_ServiceBrowser stopBrowsingForPeers];
[m_Queue removeAllObjects];
}
m_Enabled = enabled;
}
- (NSError*)sendToAllPeers:(nonnull NSData*)data withMode:(MCSessionSendDataMode)mode
{
if (m_Session.connectedPeers.count == 0)
return nil;
NSError* error = nil;
[m_Session sendData:data
toPeers:m_Session.connectedPeers
withMode:mode
error:&error];
return error;
}
- (NSUInteger)queueSize
{
return m_Queue.count;
}
- (nonnull NSData*)dequeue
{
NSData* data = [m_Queue objectAtIndex:0];
[m_Queue removeObjectAtIndex:0];
return data;
}
- (NSUInteger)connectedPeerCount
{
return m_Session.connectedPeers.count;
}
- (void)session:(nonnull MCSession *)session didFinishReceivingResourceWithName:(nonnull NSString *)resourceName fromPeer:(nonnull MCPeerID *)peerID atURL:(nullable NSURL *)localURL withError:(nullable NSError *)error {
// Not used.
}
- (void)session:(nonnull MCSession *)session didReceiveData:(nonnull NSData *)data fromPeer:(nonnull MCPeerID *)peerID
{
dispatch_async(dispatch_get_main_queue(), ^{
[m_Queue addObject:data];
});
}
- (void)session:(nonnull MCSession *)session didReceiveStream:(nonnull NSInputStream *)stream withName:(nonnull NSString *)streamName fromPeer:(nonnull MCPeerID *)peerID {
// Not used.
}
- (void)session:(nonnull MCSession *)session didStartReceivingResourceWithName:(nonnull NSString *)resourceName fromPeer:(nonnull MCPeerID *)peerID withProgress:(nonnull NSProgress *)progress {
// Not used.
}
- (void)session:(nonnull MCSession *)session peer:(nonnull MCPeerID *)peerID didChangeState:(MCSessionState)state {
// Not used.
}
- (void)advertiser:(nonnull MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(nonnull MCPeerID *)peerID withContext:(nullable NSData *)context invitationHandler:(nonnull void (^)(BOOL, MCSession * _Nullable))invitationHandler
{
invitationHandler(YES, m_Session);
}
- (void)browser:(nonnull MCNearbyServiceBrowser *)browser foundPeer:(nonnull MCPeerID *)peerID withDiscoveryInfo:(nullable NSDictionary<NSString *,NSString *> *)info
{
// Invite the peer to join our session
[browser invitePeer:peerID
toSession:m_Session
withContext:nil
timeout:10];
}
- (void)browser:(nonnull MCNearbyServiceBrowser *)browser lostPeer:(nonnull MCPeerID *)peerID
{
// Not used
}
@end

105
Assets/Scripts/Multipeer/NativeCode/MultipeerDelegate.m.meta


fileFormatVersion: 2
guid: cba1a95445f3e40ac8bbf00450e84734
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
'': Any
second:
enabled: 0
settings:
Exclude Android: 1
Exclude Editor: 1
Exclude Linux: 1
Exclude Linux64: 1
Exclude LinuxUniversal: 1
Exclude Lumin: 1
Exclude OSXUniversal: 1
Exclude Win: 1
Exclude Win64: 1
Exclude iOS: 0
- first:
Android: Android
second:
enabled: 0
settings:
CPU: ARMv7
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
Facebook: Win
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Facebook: Win64
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: Linux
second:
enabled: 0
settings:
CPU: x86
- first:
Standalone: Linux64
second:
enabled: 0
settings:
CPU: x86_64
- first:
Standalone: OSXUniversal
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: Win
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
Standalone: Win64
second:
enabled: 0
settings:
CPU: AnyCPU
- first:
iPhone: iOS
second:
enabled: 1
settings:
AddToEmbeddedBinaries: false
CompileFlags:
FrameworkDependencies: MultipeerConnectivity;
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

30
Assets/Scripts/Multipeer/NativeCode/NSData-C-Bridge.m


#import <Foundation/Foundation.h>
int UnityMC_NSData_getLength(void* self)
{
NSData* data = (__bridge NSData*)self;
return (int)data.length;
}
void* UnityMC_NSData_createWithBytes(void* bytes, int length)
{
NSData* data = [[NSData alloc] initWithBytes:bytes
length:length];
return (__bridge_retained void*)data;
}
void* UnityMC_NSData_createWithBytesNoCopy(void* bytes, int length, bool freeWhenDone)
{
NSData* data = [[NSData alloc] initWithBytesNoCopy:bytes
length:length
freeWhenDone:freeWhenDone];
return (__bridge_retained void*)data;
}
const void* UnityMC_NSData_getBytes(void* self)
{
NSData* data = (__bridge NSData*)self;
return data.bytes;
}

37
Assets/Scripts/Multipeer/NativeCode/NSData-C-Bridge.m.meta


fileFormatVersion: 2
guid: a452ea1f1d87e4256a5af3b742f78e18
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings: {}
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

12
Assets/Scripts/Multipeer/NativeCode/NSError-C-Bridge.m


#import <Foundation/Foundation.h>
long UnityMC_NSError_code(void* ptr)
{
return ((__bridge NSError*)ptr).code;
}
void* UnityMC_NSError_localizedDescription(void* ptr)
{
NSString* desc = ((__bridge NSError*)ptr).localizedDescription;
return (__bridge_retained void*)desc;
}

37
Assets/Scripts/Multipeer/NativeCode/NSError-C-Bridge.m.meta


fileFormatVersion: 2
guid: a2cf20442c9d3412f9b1978da0184a7f
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings: {}
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

22
Assets/Scripts/Multipeer/NativeCode/NSMutableData-C-Bridge.m


#import <Foundation/Foundation.h>
void* UnityMultipeer_NSMutableData_init()
{
NSMutableData* data = [[NSMutableData alloc] init];
return (__bridge_retained void*)data;
}
void* UnityMultipeer_NSMutableData_initWithBytes(const void* bytes, int length)
{
NSMutableData* data = [[NSMutableData alloc] initWithBytes:bytes
length:length];
return (__bridge_retained void*)data;
}
void* UnityMultipeer_NSMutableData_initWithBytesNoCopy(void* bytes, int length, bool freeWhenDone)
{
NSMutableData* data = [[NSMutableData alloc] initWithBytesNoCopy:bytes
length:length
freeWhenDone:freeWhenDone];
return (__bridge_retained void*)data;
}

37
Assets/Scripts/Multipeer/NativeCode/NSMutableData-C-Bridge.m.meta


fileFormatVersion: 2
guid: 3fc8bc4bd641e4dfcb418cd41edf6b64
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings: {}
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

52
Assets/Scripts/Multipeer/NativeCode/NSString-C-Bridge.m


#import <Foundation/Foundation.h>
int UnityMultipeer_NSString_lengthOfBytesUsingEncoding(void* self)
{
if (self == NULL)
return 0;
NSString* string = (__bridge NSString*)self;
return (int)[string lengthOfBytesUsingEncoding:NSUTF16LittleEndianStringEncoding];
}
bool UnityMultipeer_NSString_getBytes(void* self, void* buffer, int length)
{
NSString* string = (__bridge NSString*)self;
const NSRange range = NSMakeRange(0, string.length);
return [string getBytes:buffer
maxLength:length
usedLength:NULL
encoding:NSUTF16LittleEndianStringEncoding
options:0
range:range
remainingRange:NULL];
}
int UnityMultipeer_NSString_getLength(void* self)
{
NSString* string = (__bridge NSString*)self;
return (int)string.length;
}
void* UnityMultipeer_NSString_createWithString(void* bytes, int length)
{
NSString* string = [[NSString alloc] initWithBytes: bytes
length: 2 * length
encoding: NSUTF16LittleEndianStringEncoding];
return (__bridge_retained void*)string;
}
void* UnityMC_NSString_serialize(void* self)
{
NSString* string = (__bridge NSString*)self;
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:string];
return (__bridge_retained void*)data;
}
void* UnityMC_NSString_deserialize(void* serializedString)
{
NSData* data = (__bridge NSData*)serializedString;
NSString* string = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return (__bridge_retained void*)string;
}

37
Assets/Scripts/Multipeer/NativeCode/NSString-C-Bridge.m.meta


fileFormatVersion: 2
guid: f45e73c2d697643c898c3f259d718667
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
iPhone: iOS
second:
enabled: 1
settings: {}
- first:
tvOS: tvOS
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

17
Assets/Scripts/Multipeer/SerializedARCollaborationDataExtensions.cs


using NSData = Unity.iOS.Multipeer.NSData;
namespace UnityEngine.XR.ARKit
{
public static class SerializedARCollaborationDataExtensions
{
public static unsafe NSData ToNSData(this SerializedARCollaborationData serializedData)
{
return *(NSData*)&serializedData;
}
public static unsafe SerializedARCollaborationData ToSerializedCollaborationData(this NSData data)
{
return *(SerializedARCollaborationData*)&data;
}
}
}

11
Assets/Scripts/Multipeer/SerializedARCollaborationDataExtensions.cs.meta


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