private async void CreateAzureAnchor(AnchorPosition anchorPos) { // change the color of the currentAnchor to inprogress anchorPos.AnchorInProgress(); if (cloudManager.Session == null) { // Creates a new session if one does not exist Debug.Log("\ncloudManager.CreateSessionAsync()"); await cloudManager.CreateSessionAsync(); } // Starts the session if not already started Debug.Log("\ncloudManager.StartSessionAsync()"); await cloudManager.StartSessionAsync(); // Notify AnchorFeedbackScript OnCreateAnchorStarted?.Invoke(); // Create local cloud anchor var localCloudAnchor = new CloudSpatialAnchor(); // Create native XR anchor at the location of the object anchorPos.gameObject.CreateNativeAnchor(); Debug.Log("anchorPosition.gameObject.CreateNativeAnchor()"); // create a cloud anchor at the position of the local anchor localCloudAnchor.LocalAnchor = anchorPos.gameObject.FindNativeAnchor().GetPointer(); Debug.Log("anchorPosition.gameObject.FindNativeAnchor().GetPointer()"); // Check to see if we got the local XR anchor pointer if (localCloudAnchor.LocalAnchor == IntPtr.Zero) { Debug.Log("Didn't get the local anchor..."); return; } else { Debug.Log("Local anchor created"); } // Set expiration (when anchor will be deleted from Azure) localCloudAnchor.Expiration = DateTimeOffset.Now.AddDays(90); // upload cloud anchor to the cloud while (!cloudManager.IsReadyForCreate) { // check with the create progress await Task.Delay(330); var createProgress = cloudManager.SessionStatus.RecommendedForCreateProgress; UnityDispatcher.InvokeOnAppThread(() => Debug.Log($"Move your device to capture more environment data: {createProgress:0%}")); } Debug.Log("cloudManager is ready."); try { // Actually save Debug.Log("await cloudManager.CreateAnchorAsync(localCloudAnchor)"); await cloudManager.CreateAnchorAsync(localCloudAnchor); Debug.Log("Anchor created!"); // Store currentCloudAnchor = localCloudAnchor; localCloudAnchor = null; // Success? var success = currentCloudAnchor != null; if (success) { // update the spaital anchor id of the currentSpatialAnchor Debug.Log($"Azure anchor with ID '{currentCloudAnchor.Identifier}' created successfully"); anchorPos.SpatialAnchorObject.SpatialAnchorId = currentCloudAnchor.Identifier; // Update the current Azure anchor ID Debug.Log($"Current Azure anchor ID updated to '{currentCloudAnchor.Identifier}'"); OnCreateAnchorSucceeded?.Invoke(); } else { Debug.Log($"Failed to save cloud anchor with ID '{currentCloudAnchor.Identifier}' to Azure"); // Notify AnchorFeedbackScript OnCreateAnchorFailed?.Invoke(); } } catch (Exception ex) { Debug.Log(ex.ToString()); } // StopAzureSession(); }
public async void CreateAzureAnchor(GameObject theObject) { Debug.Log("\nAnchorModuleScript.CreateAzureAnchor()"); // Notify AnchorFeedbackScript OnCreateAnchorStarted?.Invoke(); // First we create a native XR anchor at the location of the object in question theObject.CreateNativeAnchor(); // Notify AnchorFeedbackScript OnCreateLocalAnchor?.Invoke(); // Then we create a new local cloud anchor CloudSpatialAnchor localCloudAnchor = new CloudSpatialAnchor(); // Now we set the local cloud anchor's position to the native XR anchor's position localCloudAnchor.LocalAnchor = theObject.FindNativeAnchor().GetPointer(); // Check to see if we got the local XR anchor pointer if (localCloudAnchor.LocalAnchor == IntPtr.Zero) { Debug.Log("Didn't get the local anchor..."); return; } else { Debug.Log("Local anchor created"); } // In this sample app we delete the cloud anchor explicitly, but here we show how to set an anchor to expire automatically localCloudAnchor.Expiration = DateTimeOffset.Now.AddDays(7); // Save anchor to cloud while (!cloudManager.IsReadyForCreate) { await Task.Delay(330); float createProgress = cloudManager.SessionStatus.RecommendedForCreateProgress; QueueOnUpdate(new Action(() => Debug.Log($"Move your device to capture more environment data: {createProgress:0%}"))); } bool success; try { Debug.Log("Creating Azure anchor... please wait..."); // Actually save await cloudManager.CreateAnchorAsync(localCloudAnchor); // Store currentCloudAnchor = localCloudAnchor; localCloudAnchor = null; // Success? success = currentCloudAnchor != null; if (success) { Debug.Log($"Azure anchor with ID '{currentCloudAnchor.Identifier}' created successfully"); // Notify AnchorFeedbackScript OnCreateAnchorSucceeded?.Invoke(); // Update the current Azure anchor ID Debug.Log($"Current Azure anchor ID updated to '{currentCloudAnchor.Identifier}'"); currentAzureAnchorID = currentCloudAnchor.Identifier; } else { Debug.Log($"Failed to save cloud anchor with ID '{currentAzureAnchorID}' to Azure"); // Notify AnchorFeedbackScript OnCreateAnchorFailed?.Invoke(); } } catch (Exception ex) { Debug.Log(ex.ToString()); } }
private async void CreateAsaAnchor(Transform indicatorTransform) { Debug.Log("\nAnchorManager.CreateAsaAnchor()"); anchorCreationController.StartProgressIndicatorSession(); if (cloudManager.Session == null) { // Creates a new session if one does not exist Debug.Log("await cloudManager.CreateSessionAsync()"); await cloudManager.CreateSessionAsync(); } // Starts the session if not already started Debug.Log("await cloudManager.StartSessionAsync()"); await cloudManager.StartSessionAsync(); var anchorPositionIndicator = Instantiate(anchorPositionPrefab, indicatorTransform.position, indicatorTransform.rotation); // Create native XR anchor at the location of the object anchorPositionIndicator.gameObject.CreateNativeAnchor(); Debug.Log("anchorPosition.gameObject.CreateNativeAnchor()"); // Create local cloud anchor var localCloudAnchor = new CloudSpatialAnchor(); // Set the local cloud anchor's position to the native XR anchor's position localCloudAnchor.LocalAnchor = anchorPositionIndicator.gameObject.FindNativeAnchor().GetPointer(); Debug.Log("anchorPosition.gameObject.FindNativeAnchor().GetPointer()"); // Check to see if we got the local XR anchor pointer if (localCloudAnchor.LocalAnchor == IntPtr.Zero) { Debug.Log("Didn't get the local anchor..."); return; } else { Debug.Log("Local anchor created"); } // Set expiration (when anchor will be deleted from Azure) localCloudAnchor.Expiration = DateTimeOffset.Now.AddDays(7); // Save anchor to cloud while (!cloudManager.IsReadyForCreate) { await Task.Delay(330); var createProgress = cloudManager.SessionStatus.RecommendedForCreateProgress; UnityDispatcher.InvokeOnAppThread(() => Debug.Log($"Move your device to capture more environment data: {createProgress:0%}")); } Debug.Log("cloudManager is ready."); try { // Actually save Debug.Log("await cloudManager.CreateAnchorAsync(localCloudAnchor)"); await cloudManager.CreateAnchorAsync(localCloudAnchor); Debug.Log("Anchor created!"); // Store currentCloudAnchor = localCloudAnchor; // Success? var success = currentCloudAnchor != null; if (success) { Debug.Log($"Azure anchor with ID '{currentCloudAnchor.Identifier}' created successfully"); // Update the current Azure anchor ID Debug.Log($"Current Azure anchor ID updated to '{currentCloudAnchor.Identifier}'"); currentTrackedObject.SpatialAnchorId = currentCloudAnchor.Identifier; activeAnchors.Add(currentTrackedObject.SpatialAnchorId, anchorPositionIndicator); // Notify subscribers Debug.Log("OnCreateAnchorSucceeded?.Invoke(this, currentCloudAnchor.Identifier)"); AppDispatcher.Instance().Enqueue(() => { anchorPositionIndicator.Init(currentTrackedObject); currentTrackedObject = null; OnCreateAnchorSucceeded?.Invoke(this, currentCloudAnchor.Identifier); }); } else { Debug.Log($"Failed to save cloud anchor with ID '{currentCloudAnchor.Identifier}' to Azure"); // Notify subscribers AppDispatcher.Instance().Enqueue(() => { currentTrackedObject = null; Destroy(anchorPositionIndicator.gameObject); OnCreateAnchorFailed?.Invoke(this, EventArgs.Empty); }); } } catch (Exception ex) { Debug.Log(ex.ToString()); } StopAzureSession(); }