-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWorldAnchorManager.cs
575 lines (504 loc) · 22.8 KB
/
WorldAnchorManager.cs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
#if UNITY_WSA
using System;
using System.Collections.Generic;
using UnityEngine.VR.WSA;
using UnityEngine.VR.WSA.Persistence;
#if !UNITY_EDITOR
using HoloToolkit.Unity.SpatialMapping;
#endif
#endif
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>
/// Debug text for displaying information.
/// </summary>
public TextMesh AnchorDebugText;
/// <summary>
/// Enables detailed logs in console window.
/// </summary>
/// <remarks>If the Sharing Service is used, it will inherit the log settings.</remarks>
[Tooltip("Enables detailed logs in console window. If the Sharing Service is used, it will inherit the log settings.")]
public bool ShowDetailedLogs;
/// <summary>
/// Enables anchors to be stored from subsequent game sessions.
/// </summary>
[Tooltip("Enables anchors to be stored from subsequent game sessions.")]
public bool PersistentAnchors;
#if UNITY_WSA
/// <summary>
/// To prevent initializing too many anchors at once
/// and to allow for the WorldAnchorStore to load asynchronously
/// 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>
protected struct AnchorAttachmentInfo
{
public GameObject AnchoredGameObject { get; set; }
public string AnchorName { get; set; }
public AnchorOperation Operation { get; set; }
}
/// <summary>
/// The data structure for anchor operations.
/// </summary>
protected enum AnchorOperation
{
/// <summary>
/// Save anchor to anchor store. Creates anchor if none exists.
/// </summary>
Save,
/// <summary>
/// Deletes anchor from anchor store.
/// </summary>
Delete
}
/// <summary>
/// The queue for local device anchor operations.
/// </summary>
protected Queue<AnchorAttachmentInfo> LocalAnchorOperations = new Queue<AnchorAttachmentInfo>();
/// <summary>
/// The WorldAnchorStore for the current application.
/// Can be null when the application starts.
/// </summary>
public WorldAnchorStore AnchorStore { get; protected set; }
/// <summary>
/// Internal list of anchors and their GameObject references.
/// </summary>
protected Dictionary<string, GameObject> AnchorGameObjectReferenceList = new Dictionary<string, GameObject>(0);
#region Unity Methods
protected override void Awake()
{
base.Awake();
AnchorStore = null;
}
protected virtual void Start()
{
WorldAnchorStore.GetAsync(AnchorStoreReady);
}
protected virtual void Update()
{
if (AnchorStore == null) { return; }
if (LocalAnchorOperations.Count > 0)
{
DoAnchorOperation(LocalAnchorOperations.Dequeue());
}
}
#endregion // Unity Methods
#region Event Callbacks
/// <summary>
/// Callback function that contains the WorldAnchorStore object.
/// </summary>
/// <param name="anchorStore">The WorldAnchorStore to cache.</param>
protected virtual void AnchorStoreReady(WorldAnchorStore anchorStore)
{
AnchorStore = anchorStore;
if (!PersistentAnchors)
{
AnchorStore.Clear();
}
}
/// <summary>
/// Called when tracking changes for a 'cached' anchor.
/// When an anchor isn't located immediately we subscribe to this event so
/// we can save the anchor when it is finally located or downloaded.
/// </summary>
/// <param name="anchor">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 anchor, bool located)
{
if (located && SaveAnchor(anchor))
{
if (ShowDetailedLogs)
{
Debug.LogFormat("[WorldAnchorManager] Successfully updated cached anchor \"{0}\".", anchor.name);
}
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nSuccessfully updated cached anchor \"{0}\".", anchor.name);
}
}
else
{
if (ShowDetailedLogs)
{
Debug.LogFormat("[WorldAnchorManager] Failed to locate cached anchor \"{0}\", attempting to acquire anchor again.", anchor.name);
}
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nFailed to locate cached anchor \"{0}\", attempting to acquire anchor again.", anchor.name);
}
GameObject anchoredObject;
AnchorGameObjectReferenceList.TryGetValue(anchor.name, out anchoredObject);
AnchorGameObjectReferenceList.Remove(anchor.name);
AttachAnchor(anchoredObject, anchor.name);
}
anchor.OnTrackingChanged -= Anchor_OnTrackingChanged;
}
#endregion // Event Callbacks
#endif
/// <summary>
/// Generates the name for the anchor.
/// If no anchor name was specified, the name of the anchor will be the same as the GameObject's name.
/// </summary>
/// <param name="gameObjectToAnchor">The GameObject to attach the anchor to.</param>
/// <param name="proposedAnchorname">Name of the anchor. If none provided, the name of the GameObject will be used.</param>
/// <returns>The name of the newly attached anchor.</returns>
public static string GenerateAnchorName(GameObject gameObjectToAnchor, string proposedAnchorname = null)
{
return string.IsNullOrEmpty(proposedAnchorname) ? gameObjectToAnchor.name : proposedAnchorname;
}
/// <summary>
/// Attaches an anchor to the GameObject.
/// If the anchor store has an anchor with the specified name it will load the anchor,
/// otherwise a new anchor will be saved under the specified name.
/// If no anchor name is provided, the name of the anchor will be the same as the GameObject.
/// </summary>
/// <param name="gameObjectToAnchor">The GameObject to attach the anchor to.</param>
/// <param name="anchorName">Name of the anchor. If none provided, the name of the GameObject will be used.</param>
/// <returns>The name of the newly attached anchor.</returns>
public string AttachAnchor(GameObject gameObjectToAnchor, string anchorName = null)
{
#if !UNITY_WSA || UNITY_EDITOR
Debug.LogWarning("World Anchor Manager does not work for this build. AttachAnchor will not be called.");
return null;
#else
if (gameObjectToAnchor == null)
{
Debug.LogError("[WorldAnchorManager] Must pass in a valid gameObject");
return null;
}
// This case is unexpected, but just in case.
if (AnchorStore == null)
{
Debug.LogWarning("[WorldAnchorManager] AttachAnchor called before anchor store is ready.");
}
anchorName = GenerateAnchorName(gameObjectToAnchor, anchorName);
LocalAnchorOperations.Enqueue(
new AnchorAttachmentInfo
{
AnchoredGameObject = gameObjectToAnchor,
AnchorName = anchorName,
Operation = AnchorOperation.Save
}
);
return anchorName;
#endif
}
/// <summary>
/// Removes the anchor component from the GameObject and deletes the anchor from the anchor store.
/// </summary>
/// <param name="gameObjectToUnanchor">The GameObject reference with valid anchor to remove from the anchor store.</param>
public void RemoveAnchor(GameObject gameObjectToUnanchor)
{
if (gameObjectToUnanchor == null)
{
Debug.LogError("[WorldAnchorManager] Invalid GameObject! Try removing anchor by name.");
if (AnchorDebugText != null)
{
AnchorDebugText.text += "\nInvalid GameObject! Try removing anchor by name.";
}
return;
}
RemoveAnchor(string.Empty, gameObjectToUnanchor);
}
/// <summary>
/// Removes the anchor from the anchor store, without a GameObject reference.
/// If a GameObject reference can be found, the anchor component will be removed.
/// </summary>
/// <param name="anchorName">The name of the anchor to remove from the anchor store.</param>
public void RemoveAnchor(string anchorName)
{
if (string.IsNullOrEmpty(anchorName))
{
Debug.LogErrorFormat("[WorldAnchorManager] Invalid anchor \"{0}\"! Try removing anchor by GameObject.", anchorName);
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nInvalid anchor \"{0}\"! Try removing anchor by GameObject.", anchorName);
}
return;
}
RemoveAnchor(anchorName, null);
}
/// <summary>
/// Removes the anchor from the game object and deletes the anchor
/// from the anchor store.
/// </summary>
/// <param name="anchorName">Name of the anchor to remove from the anchor store.</param>
/// <param name="gameObjectToUnanchor">GameObject to remove the anchor from.</param>
private void RemoveAnchor(string anchorName, GameObject gameObjectToUnanchor)
{
if (string.IsNullOrEmpty(anchorName) && gameObjectToUnanchor == null)
{
Debug.LogWarning("Invalid Remove Anchor Request!");
return;
}
#if !UNITY_WSA || UNITY_EDITOR
Debug.LogWarning("World Anchor Manager does not work for this build. RemoveAnchor will not be called.");
#else
// This case is unexpected, but just in case.
if (AnchorStore == null)
{
Debug.LogWarning("[WorldAnchorManager] RemoveAnchor called before anchor store is ready.");
}
LocalAnchorOperations.Enqueue(
new AnchorAttachmentInfo
{
AnchoredGameObject = gameObjectToUnanchor,
AnchorName = anchorName,
Operation = AnchorOperation.Delete
});
#endif
}
/// <summary>
/// Removes all anchors from the scene and deletes them from the anchor store.
/// </summary>
public void RemoveAllAnchors()
{
#if !UNITY_WSA || UNITY_EDITOR
Debug.LogWarning("World Anchor Manager does not work for this build. RemoveAnchor will not be called.");
#else
SpatialMappingManager spatialMappingManager = SpatialMappingManager.Instance;
// This case is unexpected, but just in case.
if (AnchorStore == null)
{
Debug.LogWarning("[WorldAnchorManager] RemoveAllAnchors called before anchor store is ready.");
}
var anchors = FindObjectsOfType<WorldAnchor>();
if (anchors == null) { return; }
for (var i = 0; i < anchors.Length; i++)
{
// Don't remove SpatialMapping anchors if exists
if (spatialMappingManager != null && anchors[i].gameObject.transform.parent.gameObject == spatialMappingManager.gameObject)
{ continue; }
// Let's check to see if there are anchors we weren't accounting for.
// Maybe they were created without using the WorldAnchorManager.
if (!AnchorGameObjectReferenceList.ContainsKey(anchors[i].name))
{
Debug.LogWarning("[WorldAnchorManager] Removing an anchor that was created outside of the WorldAnchorManager. Please use the WorldAnchorManager to create or delete anchors.");
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nRemoving an anchor that was created outside of the WorldAnchorManager. Please use the WorldAnchorManager to create or delete anchors.");
}
}
LocalAnchorOperations.Enqueue(new AnchorAttachmentInfo
{
AnchorName = anchors[i].name,
AnchoredGameObject = anchors[i].gameObject,
Operation = AnchorOperation.Delete
});
}
#endif
}
#if UNITY_WSA
/// <summary>
/// Executes the anchor operations from the localAnchorOperations queue.
/// </summary>
/// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
protected void DoAnchorOperation(AnchorAttachmentInfo anchorAttachmentInfo)
{
string anchorId = anchorAttachmentInfo.AnchorName;
GameObject anchoredGameObject = anchorAttachmentInfo.AnchoredGameObject;
switch (anchorAttachmentInfo.Operation)
{
case AnchorOperation.Save:
if (anchoredGameObject == null)
{
Debug.LogError("[WorldAnchorManager] The GameObject referenced must have been destroyed before we got a chance to anchor it.");
if (AnchorDebugText != null)
{
AnchorDebugText.text += "\nThe GameObject referenced must have been destroyed before we got a chance to anchor it.";
}
break;
}
if (string.IsNullOrEmpty(anchorId))
{
anchorId = anchoredGameObject.name;
}
// Try to load a previously saved world anchor.
WorldAnchor savedAnchor = AnchorStore.Load(anchorId, anchoredGameObject);
if (savedAnchor == null)
{
// Check if we need to import the anchor.
if (ImportAnchor(anchorId, anchoredGameObject) == false)
{
if (ShowDetailedLogs)
{
Debug.LogFormat("[WorldAnchorManager] Anchor could not be loaded for \"{0}\". Creating a new anchor.", anchoredGameObject.name);
}
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nAnchor could not be loaded for \"{0}\". Creating a new anchor.", anchoredGameObject.name);
}
// Create anchor since one does not exist.
CreateAnchor(anchoredGameObject, anchorId);
}
}
else
{
savedAnchor.name = anchorId;
if (ShowDetailedLogs)
{
Debug.LogFormat("[WorldAnchorManager] Anchor loaded from anchor store and updated for \"{0}\".", anchoredGameObject.name);
}
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nAnchor loaded from anchor store and updated for \"{0}\".", anchoredGameObject.name);
}
}
AnchorGameObjectReferenceList.Add(anchorId, anchoredGameObject);
break;
case AnchorOperation.Delete:
if (AnchorStore == null)
{
Debug.LogError("[WorldAnchorManager] Remove anchor called before anchor store is ready.");
break;
}
// If we don't have a GameObject reference, let's try to get the GameObject reference from our dictionary.
if (!string.IsNullOrEmpty(anchorId) && anchoredGameObject == null)
{
AnchorGameObjectReferenceList.TryGetValue(anchorId, out anchoredGameObject);
}
if (anchoredGameObject != null)
{
var anchor = anchoredGameObject.GetComponent<WorldAnchor>();
if (anchor != null)
{
anchorId = anchor.name;
DestroyImmediate(anchor);
}
else
{
Debug.LogErrorFormat("[WorldAnchorManager] Unable remove WorldAnchor from {0}!", anchoredGameObject.name);
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nUnable remove WorldAnchor from {0}!", anchoredGameObject.name);
}
}
}
else
{
Debug.LogError("[WorldAnchorManager] Unable find a GameObject to remove an anchor from!");
if (AnchorDebugText != null)
{
AnchorDebugText.text += "\nUnable find a GameObject to remove an anchor from!";
}
}
if (!string.IsNullOrEmpty(anchorId))
{
AnchorGameObjectReferenceList.Remove(anchorId);
DeleteAnchor(anchorId);
}
else
{
Debug.LogError("[WorldAnchorManager] Unable find an anchor to delete!");
if (AnchorDebugText != null)
{
AnchorDebugText.text += "\nUnable find an anchor to delete!";
}
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
/// <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.EnsureComponent<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>
/// Saves the anchor to the anchor store.
/// </summary>
/// <param name="anchor">Anchor.</param>
private bool SaveAnchor(WorldAnchor anchor)
{
// Save the anchor to persist holograms across sessions.
if (AnchorStore.Save(anchor.name, anchor))
{
if (ShowDetailedLogs)
{
Debug.LogFormat("[WorldAnchorManager] Successfully saved anchor \"{0}\".", anchor.name);
}
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nSuccessfully saved anchor \"{0}\".", anchor.name);
}
ExportAnchor(anchor);
return true;
}
Debug.LogErrorFormat("[WorldAnchorManager] Failed to save anchor \"{0}\"!", anchor.name);
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nFailed to save anchor \"{0}\"!", anchor.name);
}
return false;
}
/// <summary>
/// Deletes the anchor from the Anchor Store.
/// </summary>
/// <param name="anchorId">The anchor id.</param>
private void DeleteAnchor(string anchorId)
{
if (AnchorStore.Delete(anchorId))
{
Debug.LogFormat("[WorldAnchorManager] Anchor {0} deleted successfully.", anchorId);
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nAnchor {0} deleted successfully.", anchorId);
}
}
else
{
if (string.IsNullOrEmpty(anchorId))
{
anchorId = "NULL";
}
Debug.LogErrorFormat("[WorldAnchorManager] Failed to delete \"{0}\".", anchorId);
if (AnchorDebugText != null)
{
AnchorDebugText.text += string.Format("\nFailed to delete \"{0}\".", anchorId);
}
}
}
/// <summary>
/// Called before creating anchor. Used to check if import required.
/// </summary>
/// <param name="anchorId">Name of the anchor to import.</param>
/// <param name="objectToAnchor">GameObject to anchor.</param>
/// <returns>Success.</returns>
protected virtual bool ImportAnchor(string anchorId, GameObject objectToAnchor)
{
return false;
}
/// <summary>
/// Called after creating a new anchor.
/// </summary>
/// <param name="anchor">The anchor to export.</param>
/// <returns>Success.</returns>
protected virtual void ExportAnchor(WorldAnchor anchor) { }
#endif
}
}