ARKitAnchorSubsystem.cs
4.58 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
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine.Scripting;
using UnityEngine.XR.ARSubsystems;
namespace UnityEngine.XR.ARKit
{
/// <summary>
/// The ARKit implementation of the <c>XRAnchorSubsystem</c>. Do not create this directly.
/// Use the <c>SubsystemManager</c> instead.
/// </summary>
[Preserve]
public sealed class ARKitAnchorSubsystem : XRAnchorSubsystem
{
#if !UNITY_2020_2_OR_NEWER
/// <summary>
/// Creates the ARKit-specific implementation which will service the `XRAnchorSubsystem`.
/// </summary>
/// <returns>A new instance of the `Provider` specific to ARKit.</returns>
protected override Provider CreateProvider() => new ARKitProvider();
#endif
class ARKitProvider : Provider
{
public override void Start() => UnityARKit_refPoints_onStart();
public override void Stop() => UnityARKit_refPoints_onStop();
public override void Destroy() => UnityARKit_refPoints_onDestroy();
public override unsafe TrackableChanges<XRAnchor> GetChanges(
XRAnchor defaultAnchor,
Allocator allocator)
{
void* addedPtr, updatedPtr, removedPtr;
int addedCount, updatedCount, removedCount, elementSize;
var context = UnityARKit_refPoints_acquireChanges(
out addedPtr, out addedCount,
out updatedPtr, out updatedCount,
out removedPtr, out removedCount,
out elementSize);
try
{
return new TrackableChanges<XRAnchor>(
addedPtr, addedCount,
updatedPtr, updatedCount,
removedPtr, removedCount,
defaultAnchor, elementSize,
allocator);
}
finally
{
UnityARKit_refPoints_releaseChanges(context);
}
}
public override bool TryAddAnchor(Pose pose, out XRAnchor anchor)
{
return UnityARKit_refPoints_tryAdd(pose, out anchor);
}
public override bool TryAttachAnchor(
TrackableId trackableToAffix,
Pose pose,
out XRAnchor anchor)
{
return UnityARKit_refPoints_tryAttach(trackableToAffix, pose, out anchor);
}
public override bool TryRemoveAnchor(TrackableId anchorId)
{
return UnityARKit_refPoints_tryRemove(anchorId);
}
[DllImport("__Internal")]
static extern void UnityARKit_refPoints_onStart();
[DllImport("__Internal")]
static extern void UnityARKit_refPoints_onStop();
[DllImport("__Internal")]
static extern unsafe void UnityARKit_refPoints_onDestroy();
[DllImport("__Internal")]
static extern unsafe void* UnityARKit_refPoints_acquireChanges(
out void* addedPtr, out int addedCount,
out void* updatedPtr, out int updatedCount,
out void* removedPtr, out int removedCount,
out int elementSize);
[DllImport("__Internal")]
static extern unsafe void UnityARKit_refPoints_releaseChanges(void* changes);
[DllImport("__Internal")]
static extern bool UnityARKit_refPoints_tryAdd(
Pose pose,
out XRAnchor anchor);
[DllImport("__Internal")]
static extern bool UnityARKit_refPoints_tryAttach(
TrackableId trackableToAffix,
Pose pose,
out XRAnchor anchor);
[DllImport("__Internal")]
static extern bool UnityARKit_refPoints_tryRemove(TrackableId anchorId);
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void RegisterDescriptor()
{
#if UNITY_IOS && !UNITY_EDITOR
var cinfo = new XRAnchorSubsystemDescriptor.Cinfo
{
id = "ARKit-Anchor",
#if UNITY_2020_2_OR_NEWER
providerType = typeof(ARKitAnchorSubsystem.ARKitProvider),
subsystemTypeOverride = typeof(ARKitAnchorSubsystem),
#else
subsystemImplementationType = typeof(ARKitAnchorSubsystem),
#endif
supportsTrackableAttachments = true
};
XRAnchorSubsystemDescriptor.Create(cinfo);
#endif
}
}
}