private void InitializeObjectFromAnchor(UnityEngine.VR.WSA.WorldAnchor anchor) { gameObject.transform.position = anchor.transform.position; gameObject.GetComponent <HandDraggable>().IsDraggingEnabled = false; gameObject.GetComponent <MeshRenderer>().enabled = false; _anchorLoaded = true; }
/// <summary> /// Called when a remote anchor has been deserialized /// </summary> /// <param name="status">Tracks if the import worked</param> /// <param name="wat">The WorldAnchorTransferBatch that has the anchor information.</param> private void ImportComplete(UnityEngine.VR.WSA.Sharing.SerializationCompletionReason status, UnityEngine.VR.WSA.Sharing.WorldAnchorTransferBatch wat) { if (status == UnityEngine.VR.WSA.Sharing.SerializationCompletionReason.Succeeded && wat.GetAllIds().Length > 0) { Debug.Log("Import complete"); string first = wat.GetAllIds()[0]; Debug.Log("Anchor name: " + first); UnityEngine.VR.WSA.WorldAnchor existingAnchor = objectToAnchor.GetComponent <UnityEngine.VR.WSA.WorldAnchor>(); if (existingAnchor != null) { DestroyImmediate(existingAnchor); } UnityEngine.VR.WSA.WorldAnchor anchor = wat.LockObject(first, objectToAnchor); WorldAnchorManager.Instance.AnchorStore.Save(first, anchor); ImportInProgress = false; AnchorEstablished = true; } else { // if we failed, we can simply try again. gotOne = true; Debug.Log("Import fail"); } }
/// <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. UnityEngine.VR.WSA.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 <UnityEngine.VR.WSA.WorldAnchor>(); if (anchor != null) { AnchorStore.Delete(anchor.name); DestroyImmediate(anchor); } else { Debug.LogError("Cannot get anchor while deleting"); } break; } }
/// <summary> /// Saves the anchor to the anchor store. /// </summary> /// <param name="anchor"></param> private void SaveAnchor(UnityEngine.VR.WSA.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."); } }
/// <summary> /// If we are supposed to create the anchor for export, this is the function to call. /// </summary> public void CreateAnchor() { objectToAnchor = SharedCollection.Instance.gameObject; UnityEngine.VR.WSA.Sharing.WorldAnchorTransferBatch watb = new UnityEngine.VR.WSA.Sharing.WorldAnchorTransferBatch(); UnityEngine.VR.WSA.WorldAnchor worldAnchor = objectToAnchor.GetComponent <UnityEngine.VR.WSA.WorldAnchor>(); if (worldAnchor == null) { worldAnchor = objectToAnchor.AddComponent <UnityEngine.VR.WSA.WorldAnchor>(); } exportingAnchorName = Guid.NewGuid().ToString(); Debug.Log("exporting " + exportingAnchorName); watb.AddWorldAnchor(exportingAnchorName, worldAnchor); UnityEngine.VR.WSA.Sharing.WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete); }
/// <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(UnityEngine.VR.WSA.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."); } }