ARKitXRObjectTrackingSubsystem.cs
5.75 KB
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
using System;
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARKit
{
/// <summary>
/// An ARKit-specific implementation of the <c>XRObjectTrackingSubsystem</c>. Do not create this directly. Use the <c>SubsystemManager</c> instead.
/// </summary>
[Preserve]
public sealed class ARKitXRObjectTrackingSubsystem : XRObjectTrackingSubsystem
{
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Creates the ARKit-specific implementation which will service the `XRObjectTrackingSubsystem`.
/// </summary>
/// <returns>A new instance of the `Provider` specific to ARKit.</returns>
protected override Provider CreateProvider() => new ARKitProvider();
#endif
class ARKitProvider : Provider
{
#if UNITY_2020_2_OR_NEWER
/// <summary>
/// Invoked when <c>Start</c> is called on the subsystem. This method is only called if the subsystem was not previously running.
/// </summary>
public override void Start() { }
/// <summary>
/// Invoked when <c>Stop</c> is called on the subsystem. This method is only called if the subsystem was previously running.
/// </summary>
public override void Stop() { }
#endif
[DllImport("__Internal")]
static extern void UnityARKit_ObjectTracking_Initialize();
[DllImport("__Internal")]
static extern void UnityARKit_ObjectTracking_Shutdown();
[DllImport("__Internal")]
static extern void UnityARKit_ObjectTracking_Stop();
[DllImport("__Internal")]
static extern SetReferenceLibraryResult UnityARKit_ObjectTracking_TrySetLibrary(
[MarshalAs(UnmanagedType.LPWStr)] string name, int nameLength, Guid guid);
[DllImport("__Internal")]
static extern unsafe void* UnityARKit_ObjectTracking_AcquireChanges(
out void* addedPtr, out int addedLength,
out void* updatedPtr, out int updatedLength,
out void* removedPtr, out int removedLength,
out int elementSize);
[DllImport("__Internal")]
static extern unsafe void UnityARKit_ObjectTracking_ReleaseChanges(void* changes);
public override unsafe XRReferenceObjectLibrary library
{
set
{
if (value == null)
{
UnityARKit_ObjectTracking_Stop();
}
else
{
switch (UnityARKit_ObjectTracking_TrySetLibrary(value.name, value.name.Length, value.guid))
{
case SetReferenceLibraryResult.Success:
break;
case SetReferenceLibraryResult.FeatureUnavailable:
throw new InvalidOperationException(string.Format(
"Failed to set requested image library '{0}' on ARKit - this feature only works on versions of ARKit 12.0 and newer.",
value.name));
case SetReferenceLibraryResult.ResourceDoesNotExist:
throw new InvalidOperationException(string.Format(
"Failed to find requested image library '{0}' on ARKit - there is no matching resource group, or the resource group does not contain any reference objects.",
value.name));
}
}
}
}
public override unsafe TrackableChanges<XRTrackedObject> GetChanges(
XRTrackedObject defaultTrackedObject,
Allocator allocator)
{
int addedLength, updatedLength, removedLength, elementSize;
void* addedPtr, updatedPtr, removedPtr;
var context = UnityARKit_ObjectTracking_AcquireChanges(
out addedPtr, out addedLength,
out updatedPtr, out updatedLength,
out removedPtr, out removedLength,
out elementSize);
try
{
return new TrackableChanges<XRTrackedObject>(
addedPtr, addedLength,
updatedPtr, updatedLength,
removedPtr, removedLength,
defaultTrackedObject, elementSize,
allocator);
}
finally
{
UnityARKit_ObjectTracking_ReleaseChanges(context);
}
}
public override void Destroy() => UnityARKit_ObjectTracking_Shutdown();
public ARKitProvider() => UnityARKit_ObjectTracking_Initialize();
}
/// <summary>
/// This method is run on startup of the app to register this provider with XR Subsystem Manager
/// </summary>
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void RegisterDescriptor()
{
// No support before iOS 12.0
if (!Api.AtLeast12_0())
return;
var capabilities = new XRObjectTrackingSubsystemDescriptor.Capabilities
{
};
#if UNITY_2020_2_OR_NEWER
Register<ARKitXRObjectTrackingSubsystem.ARKitProvider, ARKitXRObjectTrackingSubsystem>("ARKit-ObjectTracking", capabilities);
#else
Register<ARKitXRObjectTrackingSubsystem>("ARKit-ObjectTracking", capabilities);
#endif
}
}
}