forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorldAnchorManager.cs
More file actions
294 lines (261 loc) · 10.9 KB
/
WorldAnchorManager.cs
File metadata and controls
294 lines (261 loc) · 10.9 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.VR.WSA.Persistence;
using UnityEngine.VR.WSA;
using HoloToolkit.Unity.SpatialMapping;
namespace HoloToolkit.Unity
{
/// <summary>
/// Wrapper around world anchor store to streamline some of the persistence api busy work.
/// </summary>
public class WorldAnchorManager : Singleton<WorldAnchorManager>
{
/// <summary>
/// To prevent initializing too many anchors at once
/// and to allow for the WorldAnchorStore to load asyncronously
/// without callers handling the case where the store isn't loaded yet
/// we'll setup a queue of anchor attachment operations.
/// The AnchorAttachmentInfo struct has the data needed to do this.
/// </summary>
private struct AnchorAttachmentInfo
{
public GameObject GameObjectToAnchor { get; set; }
public string AnchorName { get; set; }
public AnchorOperation Operation { get; set; }
}
private enum AnchorOperation
{
Create,
Delete
}
/// <summary>
/// The queue mentioned above.
/// </summary>
private Queue<AnchorAttachmentInfo> anchorOperations = new Queue<AnchorAttachmentInfo>();
/// <summary>
/// The WorldAnchorStore for the current application.
/// Can be null when the application starts.
/// </summary>
public WorldAnchorStore AnchorStore { get; private set; }
/// <summary>
/// Callback function that contains the WorldAnchorStore object.
/// </summary>
/// <param name="anchorStore">The WorldAnchorStore to cache.</param>
private void AnchorStoreReady(WorldAnchorStore anchorStore)
{
AnchorStore = anchorStore;
}
/// <summary>
/// When the app starts grab the anchor store immediately.
/// </summary>
protected override void Awake()
{
base.Awake();
AnchorStore = null;
WorldAnchorStore.GetAsync(AnchorStoreReady);
}
/// <summary>
/// Each frame see if there is work to do and if we can do a unit, do it.
/// </summary>
private void Update()
{
if (AnchorStore != null && anchorOperations.Count > 0)
{
DoAnchorOperation(anchorOperations.Dequeue());
}
}
/// <summary>
/// Attaches an anchor to the game object. If the anchor store has
/// an anchor with the specified name it will load the acnhor, otherwise
/// a new anchor will be saved under the specified name.
/// </summary>
/// <param name="gameObjectToAnchor">The Gameobject to attach the anchor to.</param>
/// <param name="anchorName">Name of the anchor.</param>
public void AttachAnchor(GameObject gameObjectToAnchor, string anchorName)
{
if (gameObjectToAnchor == null)
{
Debug.LogError("Must pass in a valid gameObject");
return;
}
if (string.IsNullOrEmpty(anchorName))
{
Debug.LogError("Must supply an AnchorName.");
return;
}
anchorOperations.Enqueue(
new AnchorAttachmentInfo
{
GameObjectToAnchor = gameObjectToAnchor,
AnchorName = anchorName,
Operation = AnchorOperation.Create
}
);
}
/// <summary>
/// Removes the anchor from the game object and deletes the anchor
/// from the anchor store.
/// </summary>
/// <param name="gameObjectToUnanchor">gameObject to remove the anchor from.</param>
public void RemoveAnchor(GameObject gameObjectToUnanchor)
{
if (gameObjectToUnanchor == null)
{
Debug.LogError("Invalid GameObject");
return;
}
// This case is unexpected, but just in case.
if (AnchorStore == null)
{
Debug.LogError("remove anchor called before anchor store is ready.");
return;
}
anchorOperations.Enqueue(
new AnchorAttachmentInfo
{
GameObjectToAnchor = gameObjectToUnanchor,
AnchorName = string.Empty,
Operation = AnchorOperation.Delete
});
}
/// <summary>
/// Removes all anchors from the scene and deletes them from the anchor store.
/// </summary>
public void RemoveAllAnchors()
{
SpatialMappingManager spatialMappingManager = SpatialMappingManager.Instance;
// This case is unexpected, but just in case.
if (AnchorStore == null)
{
Debug.LogError("remove all anchors called before anchor store is ready.");
}
WorldAnchor[] anchors = FindObjectsOfType<WorldAnchor>();
if (anchors != null)
{
foreach (WorldAnchor anchor in anchors)
{
// Don't remove SpatialMapping anchors if exists
if (spatialMappingManager == null ||
anchor.gameObject.transform.parent.gameObject != spatialMappingManager.gameObject)
{
anchorOperations.Enqueue(new AnchorAttachmentInfo()
{
AnchorName = anchor.name,
GameObjectToAnchor = anchor.gameObject,
Operation = AnchorOperation.Delete
});
}
}
}
}
/// <summary>
/// Function that actually adds the anchor to the game object.
/// </summary>
/// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
private void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
{
switch (anchorAttachmentInfo.Operation)
{
case AnchorOperation.Create:
string anchorName = anchorAttachmentInfo.AnchorName;
GameObject gameObjectToAnchor = anchorAttachmentInfo.GameObjectToAnchor;
if (gameObjectToAnchor == null)
{
Debug.LogError("GameObject must have been destroyed before we got a chance to anchor it.");
break;
}
// Try to load a previously saved world anchor.
WorldAnchor savedAnchor = AnchorStore.Load(anchorName, gameObjectToAnchor);
if (savedAnchor == null)
{
// Either world anchor was not saved / does not exist or has a different name.
Debug.LogWarning(gameObjectToAnchor.name + " : World anchor could not be loaded for this game object. Creating a new anchor.");
// Create anchor since one does not exist.
CreateAnchor(gameObjectToAnchor, anchorName);
}
else
{
savedAnchor.name = anchorName;
Debug.Log(gameObjectToAnchor.name + " : World anchor loaded from anchor store and updated for this game object.");
}
break;
case AnchorOperation.Delete:
if (AnchorStore == null)
{
Debug.LogError("Remove anchor called before anchor store is ready.");
break;
}
GameObject gameObjectToUnanchor = anchorAttachmentInfo.GameObjectToAnchor;
var anchor = gameObjectToUnanchor.GetComponent<WorldAnchor>();
if (anchor != null)
{
AnchorStore.Delete(anchor.name);
DestroyImmediate(anchor);
}
else
{
Debug.LogError("Cannot get anchor while deleting");
}
break;
}
}
/// <summary>
/// Creates an anchor, attaches it to the gameObjectToAnchor, and saves the anchor to the anchor store.
/// </summary>
/// <param name="gameObjectToAnchor">The GameObject to attach the anchor to.</param>
/// <param name="anchorName">The name to give to the anchor.</param>
private void CreateAnchor(GameObject gameObjectToAnchor, string anchorName)
{
var anchor = gameObjectToAnchor.AddComponent<WorldAnchor>();
anchor.name = anchorName;
// Sometimes the anchor is located immediately. In that case it can be saved immediately.
if (anchor.isLocated)
{
SaveAnchor(anchor);
}
else
{
// Other times we must wait for the tracking system to locate the world.
anchor.OnTrackingChanged += Anchor_OnTrackingChanged;
}
}
/// <summary>
/// When an anchor isn't located immediately we subscribe to this event so
/// we can save the anchor when it is finally located.
/// </summary>
/// <param name="self">The anchor that is reporting a tracking changed event.</param>
/// <param name="located">Indicates if the anchor is located or not located.</param>
private void Anchor_OnTrackingChanged(WorldAnchor self, bool located)
{
if (located)
{
Debug.Log(gameObject.name + " : World anchor located successfully.");
SaveAnchor(self);
// Once the anchor is located we can unsubscribe from this event.
self.OnTrackingChanged -= Anchor_OnTrackingChanged;
}
else
{
Debug.LogError(gameObject.name + " : World anchor failed to locate.");
}
}
/// <summary>
/// Saves the anchor to the anchor store.
/// </summary>
/// <param name="anchor"></param>
private void SaveAnchor(WorldAnchor anchor)
{
// Save the anchor to persist holograms across sessions.
if (AnchorStore.Save(anchor.name, anchor))
{
Debug.Log(gameObject.name + " : World anchor saved successfully.");
}
else
{
Debug.LogError(gameObject.name + " : World anchor save failed.");
}
}
}
}