SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.4 KiB

4 years ago
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. using UnityEngine.Scripting;
  6. using UnityEngine.XR.ARSubsystems;
  7. namespace UnityEngine.XR.ARKit
  8. {
  9. /// <summary>
  10. /// ARKit implementation of the <c>XRParticipantSubsystem</c>. Do not create this using <c>new</c>. Instead, use the
  11. /// <a href="https://docs.unity3d.com/ScriptReference/SubsystemManager.GetSubsystemDescriptors.html">SubsystemManager</a>
  12. /// to enumerate the available <see cref="XRParticipantSubsystemDescriptor"/>s and get or create an instance from the descriptor.
  13. /// </summary>
  14. [Preserve]
  15. public sealed class ARKitParticipantSubsystem : XRParticipantSubsystem
  16. {
  17. protected override Provider CreateProvider() => new ARKitProvider();
  18. class ARKitProvider : Provider
  19. {
  20. IntPtr m_Ptr;
  21. bool created => m_Ptr != IntPtr.Zero;
  22. public ARKitProvider() => m_Ptr = UnityARKit_Participant_init();
  23. public override void Start()
  24. {
  25. if (!created)
  26. throw new InvalidOperationException($"The {typeof(ARKitParticipantSubsystem).Name} provider has not been created.");
  27. Api.UnityARKit_TrackableProvider_start(m_Ptr);
  28. }
  29. public override void Stop()
  30. {
  31. if (!created)
  32. throw new InvalidOperationException($"The {typeof(ARKitParticipantSubsystem).Name} provider has not been created.");
  33. Api.UnityARKit_TrackableProvider_stop(m_Ptr);
  34. }
  35. public override void Destroy() => Api.CFRelease(ref m_Ptr);
  36. public unsafe override TrackableChanges<XRParticipant> GetChanges(
  37. XRParticipant defaultParticipant,
  38. Allocator allocator)
  39. {
  40. if (!created)
  41. throw new InvalidOperationException($"The {typeof(ARKitParticipantSubsystem).Name} provider has not been created.");
  42. using (var nativeChanges = Api.UnityARKit_TrackableProvider_acquireChanges(m_Ptr))
  43. {
  44. var changes = new TrackableChanges<XRParticipant>(
  45. nativeChanges.addedLength,
  46. nativeChanges.updatedLength,
  47. nativeChanges.removedLength,
  48. allocator, defaultParticipant);
  49. Api.UnityARKit_TrackableProvider_copyChanges(
  50. m_Ptr, nativeChanges,
  51. UnsafeUtility.SizeOf<XRParticipant>(),
  52. changes.added.GetUnsafePtr(),
  53. changes.updated.GetUnsafePtr(),
  54. changes.removed.GetUnsafePtr());
  55. return changes;
  56. }
  57. }
  58. [DllImport("__Internal")]
  59. static extern IntPtr UnityARKit_Participant_init();
  60. }
  61. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  62. static void RegisterDescriptor()
  63. {
  64. #if UNITY_IOS && !UNITY_EDITOR
  65. if (OSVersion.Parse(UnityEngine.iOS.Device.systemVersion) < new OSVersion(13))
  66. return;
  67. XRParticipantSubsystemDescriptor.Register<ARKitParticipantSubsystem>(
  68. "ARKit-Participant",
  69. XRParticipantSubsystemDescriptor.Capabilities.None);
  70. #endif
  71. }
  72. }
  73. }