8 using System.Collections.Generic;
10 #if UNITY_2017_2_OR_NEWER 30 public event Action<bool> AnchorUploaded;
38 public event Action<bool, GameObject> AnchorDownloaded;
44 private const uint MinTrustworthySerializedAnchorDataSize = 100000;
50 private WorldAnchorTransferBatch currentAnchorTransferBatch;
52 private bool isExportingAnchors;
53 private bool shouldExportAnchors;
58 private List<byte> rawAnchorUploadData =
new List<byte>(0);
60 private bool canUpdate;
61 private bool isImportingAnchors;
62 private bool shouldImportAnchors;
67 private byte[] rawAnchorDownloadData;
71 protected override void Start()
91 Debug.LogError(
"SharingWorldAnchorManager requires the SharingStage.");
95 protected override void Update()
97 if (AnchorStore == null ||
103 if (LocalAnchorOperations.Count > 0)
105 if (!isExportingAnchors && !isImportingAnchors)
107 DoAnchorOperation(LocalAnchorOperations.Dequeue());
112 if (shouldImportAnchors && !isImportingAnchors && !isExportingAnchors)
114 if (AnchorDebugText != null)
116 AnchorDebugText.text +=
"\nStarting Anchor Download...";
119 isImportingAnchors =
true;
120 shouldImportAnchors =
false;
121 WorldAnchorTransferBatch.ImportAsync(rawAnchorDownloadData, ImportComplete);
124 if (shouldExportAnchors && !isExportingAnchors && !isImportingAnchors)
126 if (AnchorDebugText != null)
128 AnchorDebugText.text +=
"\nStarting Anchor Upload...";
131 isExportingAnchors =
true;
132 shouldExportAnchors =
false;
133 WorldAnchorTransferBatch.ExportAsync(currentAnchorTransferBatch, WriteBuffer, ExportComplete);
138 protected override void OnDestroy()
144 #endregion // Unity Methods 146 #region Event Callbacks 152 protected override void AnchorStoreReady(WorldAnchorStore anchorStore)
154 AnchorStore = anchorStore;
158 if (AnchorDebugText != null)
160 AnchorDebugText.text +=
"\nClearing Anchor Store...";
172 private void Connected(
object sender = null, EventArgs e = null)
177 SharingStage.
Instance.RoomManagerAdapter.UserJoinedRoomEvent += RoomManagerListener_OnRoomJoined;
180 if (ShowDetailedLogs)
182 Debug.Log(
"[SharingWorldAnchorManager] Connected to server.");
185 if (AnchorDebugText != null)
187 AnchorDebugText.text +=
"\nConnected to Server.";
196 private void Disconnected(
object sender = null, EventArgs e = null)
203 SharingStage.
Instance.RoomManagerAdapter.UserJoinedRoomEvent -= RoomManagerListener_OnRoomJoined;
207 if (ShowDetailedLogs)
209 Debug.Log(
"[SharingWorldAnchorManager] Disconnected from server.");
212 if (AnchorDebugText != null)
214 AnchorDebugText.text +=
"\nDisconnected from Server.";
219 private void RoomManagerListener_OnRoomJoined(
Room room,
int userId)
224 SharingStage.
Instance.RoomManagerAdapter.AnchorUploadedEvent += RoomManagerListener_AnchorUploaded;
225 SharingStage.
Instance.RoomManagerAdapter.AnchorsChangedEvent += RoomManagerListener_AnchorsChanged;
226 SharingStage.
Instance.RoomManagerAdapter.AnchorsDownloadedEvent += RoomManagerListener_AnchorDownloaded;
230 if (ShowDetailedLogs)
232 Debug.LogFormat(
"[SharingWorldAnchorManager] In room {0} with {1} anchors.",
237 if (AnchorDebugText != null)
239 AnchorDebugText.text +=
string.Format(
"\nIn room {0} with {1} anchors.",
246 private void RoomManagerListener_OnLeftRoom(
Room room,
int userId)
250 SharingStage.
Instance.RoomManagerAdapter.AnchorUploadedEvent -= RoomManagerListener_AnchorUploaded;
251 SharingStage.
Instance.RoomManagerAdapter.AnchorsChangedEvent -= RoomManagerListener_AnchorsChanged;
252 SharingStage.
Instance.RoomManagerAdapter.AnchorsDownloadedEvent -= RoomManagerListener_AnchorDownloaded;
256 if (ShowDetailedLogs)
260 if (AnchorDebugText != null)
262 AnchorDebugText.text +=
string.Format(
"\nLeft room {0}", room.
GetName().
GetString());
270 private void RoomManagerListener_AnchorUploaded(
bool successful,
XString failureReason)
274 string[] anchorIds = currentAnchorTransferBatch.GetAllIds();
276 for (
int i = 0; i < anchorIds.Length; i++)
278 if (ShowDetailedLogs)
280 Debug.LogFormat(
"[SharingWorldAnchorManager] Successfully uploaded anchor \"{0}\".", anchorIds[i]);
283 if (AnchorDebugText != null)
285 AnchorDebugText.text +=
string.Format(
"\nSuccessfully uploaded anchor \"{0}\".", anchorIds[i]);
291 Debug.LogError(
"[SharingWorldAnchorManager] Upload failed: " + failureReason);
292 if (AnchorDebugText != null)
294 AnchorDebugText.text +=
string.Format(
"\nUpload failed: " + failureReason);
298 rawAnchorUploadData.Clear();
299 currentAnchorTransferBatch.Dispose();
300 currentAnchorTransferBatch = null;
301 isExportingAnchors =
false;
303 if (AnchorUploaded != null)
305 AnchorUploaded(successful);
319 if (ShowDetailedLogs)
321 Debug.LogFormat(
"[SharingWorldAnchorManager] Downloaded {0} bytes.", dataSize.ToString());
324 if (AnchorDebugText != null)
326 AnchorDebugText.text +=
string.Format(
"\nDownloaded {0} bytes.", dataSize.ToString());
329 rawAnchorDownloadData =
new byte[dataSize];
330 request.
GetData(rawAnchorDownloadData, dataSize);
331 shouldImportAnchors =
true;
335 Debug.LogWarning(
"[SharingWorldAnchorManager] Anchor DL failed " + failureReason);
336 if (AnchorDebugText != null)
338 AnchorDebugText.text +=
string.Format(
"\nAnchor DL failed " + failureReason);
347 private void RoomManagerListener_AnchorsChanged(
Room room)
351 if (AnchorDebugText != null)
353 AnchorDebugText.text +=
"\nRoom Anchors Updated! Clearing the local Anchor Store and attempting to download the update...";
360 if (ShowDetailedLogs)
362 Debug.LogFormat(
"[SharingWorldAnchorManager] Anchors updated for room \"{0}\".\nClearing the local Anchor Store and attempting to download the update...", room.
GetName().
GetString());
365 if (AnchorDebugText != null)
367 AnchorDebugText.text +=
string.Format(
"\nAnchors updated for room \"{0}\".\nClearing the local Anchor Store and attempting to download the update...", room.
GetName().
GetString());
372 for (
int i = 0; i < roomAnchorCount; i++)
374 GameObject anchoredObject;
377 if (AnchorGameObjectReferenceList.TryGetValue(roomAnchorId, out anchoredObject))
379 if (ShowDetailedLogs)
381 Debug.LogFormat(
"[SharingWorldAnchorManager] Found cached GameObject reference for \"{0}\".", roomAnchorId);
384 if (AnchorDebugText != null)
386 AnchorDebugText.text +=
string.Format(
"\nFound cached GameObject reference for \"{0}\".", roomAnchorId);
389 AttachAnchor(anchoredObject, roomAnchorId);
393 anchoredObject = GameObject.Find(roomAnchorId);
395 if (anchoredObject != null)
397 if (ShowDetailedLogs)
399 Debug.LogFormat(
"[SharingWorldAnchorManager] Found a GameObject reference form scene for \"{0}\".", roomAnchorId);
402 if (AnchorDebugText != null)
404 AnchorDebugText.text +=
string.Format(
"\nFound a GameObject reference form scene for \"{0}\".", roomAnchorId);
407 AttachAnchor(anchoredObject, roomAnchorId);
411 Debug.LogWarning(
"[SharingWorldAnchorManager] Unable to find a matching GameObject for anchor!");
412 if (AnchorDebugText != null)
414 AnchorDebugText.text +=
"\nUnable to find a matching GameObject for anchor!";
422 #endregion // Event Callbacks 430 protected override bool ImportAnchor(
string anchorId, GameObject objectToAnchor)
436 Debug.LogErrorFormat(
"[SharingWorldAnchorManager] Failed to import anchor \"{0}\"! The sharing service was not ready.", anchorId);
437 if (AnchorDebugText != null)
439 AnchorDebugText.text +=
string.Format(
"\nFailed to import anchor \"{0}\"! The sharing service was not ready.", anchorId);
447 for (
int i = 0; i < roomAnchorCount; i++)
451 if (roomAnchorId.
GetString().Equals(anchorId))
457 if (ShowDetailedLogs)
459 Debug.Log(
"[SharingWorldAnchorManager] Found a match! Attempting to download anchor...");
462 if (AnchorDebugText != null)
464 AnchorDebugText.text +=
"\nFound a match! Attempting to download anchor...";
469 Debug.LogWarning(
"[SharingWorldAnchorManager] Found a match, but we've failed to start download!");
470 if (AnchorDebugText != null)
472 AnchorDebugText.text +=
"\nFound a match, but we've failed to start download!";
476 return downloadStarted;
480 if (ShowDetailedLogs)
482 Debug.LogFormat(
"[SharingWorldAnchorManager] No matching anchor found for \"{0}\" in room {1}.", anchorId,
SharingStage.
Instance.CurrentRoom.GetName().GetString());
485 if (AnchorDebugText != null)
487 AnchorDebugText.text +=
string.Format(
"\nNo matching anchor found for \"{0}\" in room {1}.", anchorId,
SharingStage.
Instance.CurrentRoom.GetName().GetString());
498 protected override void ExportAnchor(WorldAnchor anchor)
504 Debug.LogErrorFormat(
"[SharingWorldAnchorManager] Failed to export anchor \"{0}\"! The sharing service was not ready.", anchor.name);
505 if (AnchorDebugText != null)
507 AnchorDebugText.text +=
string.Format(
"\nFailed to export anchor \"{0}\"! The sharing service was not ready.", anchor.name);
513 if (!shouldExportAnchors)
515 if (ShowDetailedLogs)
517 Debug.LogWarningFormat(
"[SharingWorldAnchorManager] Attempting to export anchor \"{0}\".", anchor.name);
520 if (AnchorDebugText != null)
522 AnchorDebugText.text +=
string.Format(
"\nAttempting to export anchor \"{0}\".", anchor.name);
525 if (currentAnchorTransferBatch == null)
527 currentAnchorTransferBatch =
new WorldAnchorTransferBatch();
528 if (AnchorDebugText != null)
530 AnchorDebugText.text +=
"\nCreating a new World Anchor Transfer Batch...";
535 Debug.LogWarning(
"[SharingWorldAnchorManager] We didn't properly cleanup our WorldAnchorTransferBatch!");
536 if (AnchorDebugText != null)
538 AnchorDebugText.text +=
"\nWe didn't properly cleanup our WorldAnchorTransferBatch!";
542 currentAnchorTransferBatch.AddWorldAnchor(anchor.name, anchor);
543 shouldExportAnchors =
true;
551 private void ExportComplete(SerializationCompletionReason status)
553 if (status == SerializationCompletionReason.Succeeded &&
554 rawAnchorUploadData.Count > MinTrustworthySerializedAnchorDataSize)
556 if (ShowDetailedLogs)
558 Debug.LogFormat(
"[SharingWorldAnchorManager] Exporting {0} anchors with {1} bytes.", currentAnchorTransferBatch.anchorCount.ToString(), rawAnchorUploadData.ToArray().Length.ToString());
561 if (AnchorDebugText != null)
563 AnchorDebugText.text +=
string.Format(
"\nExporting {0} anchors with {1} bytes.",
564 currentAnchorTransferBatch.anchorCount.ToString(),
565 rawAnchorUploadData.ToArray().Length.ToString());
568 string[] anchorNames = currentAnchorTransferBatch.GetAllIds();
570 for (var i = 0; i < anchorNames.Length; i++)
575 rawAnchorUploadData.ToArray(),
576 rawAnchorUploadData.Count);
581 Debug.LogWarning(
"[SharingWorldAnchorManager] Failed to upload anchor!");
583 if (AnchorDebugText != null)
585 AnchorDebugText.text +=
"\nFailed to upload anchor!";
588 if (rawAnchorUploadData.Count < MinTrustworthySerializedAnchorDataSize)
590 Debug.LogWarning(
"[SharingWorldAnchorManager] Anchor data was not valid. Try creating the anchor again.");
592 if (AnchorDebugText != null)
594 AnchorDebugText.text +=
"\nAnchor data was not valid. Try creating the anchor again.";
605 private void ImportComplete(SerializationCompletionReason status, WorldAnchorTransferBatch anchorBatch)
607 bool successful = status == SerializationCompletionReason.Succeeded;
608 GameObject objectToAnchor = null;
612 if (ShowDetailedLogs)
614 Debug.LogFormat(
"[SharingWorldAnchorManager] Successfully imported \"{0}\" anchors.", anchorBatch.anchorCount.ToString());
617 if (AnchorDebugText != null)
619 AnchorDebugText.text +=
string.Format(
"\nSuccessfully imported \"{0}\" anchors.", anchorBatch.anchorCount.ToString());
622 string[] anchorNames = anchorBatch.GetAllIds();
624 for (var i = 0; i < anchorNames.Length; i++)
626 if (AnchorGameObjectReferenceList.TryGetValue(anchorNames[i], out objectToAnchor))
628 AnchorStore.Save(anchorNames[i], anchorBatch.LockObject(anchorNames[i], objectToAnchor));
633 Debug.LogWarning(
"[SharingWorldAnchorManager] Unable to import anchor! We don't know which GameObject to anchor!");
635 if (AnchorDebugText != null)
637 AnchorDebugText.text +=
"\nUnable to import anchor! We don\'t know which GameObject to anchor!";
644 Debug.LogError(
"[SharingWorldAnchorManager] Import failed!");
646 if (AnchorDebugText != null)
648 AnchorDebugText.text +=
"\nImport failed!";
652 if (AnchorDownloaded != null)
654 AnchorDownloaded(successful, objectToAnchor);
657 anchorBatch.Dispose();
658 rawAnchorDownloadData = null;
659 isImportingAnchors =
false;
666 private void WriteBuffer(byte[] data)
668 rawAnchorUploadData.AddRange(data);