Search Unity

Using Azure Spatial Anchors, cloud portion on native anchor keeps returning null

Discussion in 'AR' started by mr_kodjo, Apr 14, 2020.

  1. mr_kodjo

    mr_kodjo

    Joined:
    Jan 18, 2020
    Posts:
    4
    so I am using azure spatial anchors in my application and whenever i try to save an anchor to azure, i get a CloudSpatialException because the cloud anchor on my native anchor returns null. this is the code i am using to get the native anchor and save it to azure:


    protected virtual async Task SaveCurrentObjectAnchorToCloudAsync()
    {
    CloudNativeAnchor nativeAnchor = spawnedObject.GetComponent<CloudNativeAnchor>();
    if(nativeAnchor.CloudAnchor == null)
    {
    feedback.text = "The cloud portion on the\nnative anchor is null";
    nativeAnchor.NativeToCloud();
    }

    CloudSpatialAnchor spatialAnchor = nativeAnchor.CloudAnchor;

    while (!CloudManager.IsReadyForCreate)
    {
    await Task.Delay(330);
    float createProgess = CloudManager.SessionStatus.RecommendedForCreateProgress;
    feedback.text = $"Move your device to capture more\nof your environment: {createProgess:0%}";
    }

    bool success = false;

    feedback.text = "Saving anchor to Azure...";

    try
    {
    feedback.text = "We are in try block";
    await CloudManager.CreateAnchorAsync(spatialAnchor);
    currentSpatialAnchor = spatialAnchor;
    feedback.text = $"Saving anchor...\nCloud spatial anchor ID: {currentSpatialAnchor.Identifier}";
    success = currentSpatialAnchor != null;
    if(success && !isErrorActive)
    {
    feedback.text = "Saving anchor was a success";
    await OnSaveCloudAnchorSuccessfulAsync();
    }
    else
    {
    feedback.text = "Saving anchor was a failure";
    OnSaveCloudAnchorFailed(new Exception("Failed to save, but no exception was thrown"));
    }
    }
    catch(Exception ex)
    {
    OnSaveCloudAnchorFailed(ex);
    }
    }
     
  2. ixdeveloper

    ixdeveloper

    Joined:
    Jan 5, 2016
    Posts:
    14
    I had the same issue, it appears there is a bug in the SDK that fails to create a reference to the cloud anchor. A workaround is to modify the CloudToNative method:
    Code (CSharp):
    1. public void CloudToNative(CloudSpatialAnchor cloudAnchor)
    2.         {
    3.             // Validate
    4.             if (cloudAnchor == null) { throw new ArgumentNullException(nameof(cloudAnchor)); }
    5.  
    6.             // Apply and store updated native anchor
    7.             nativeAnchor = gameObject.ApplyCloudAnchor(cloudAnchor);
    8.             this.cloudAnchor = cloudAnchor;
    9.         }
    See this issue for more info: https://github.com/Azure/azure-spatial-anchors-samples/issues/113