0

I haven't find so far any good and update script for manage the Microsoft Spatial Azure Anchors, could you please review this code and tell me more or less if i am in the right way or not ? Actually I am trying to minimize the code for having the pure functionality, but i am stack and i can't even find an understandable guideline hope the code is readable and well documented.

thanks in advance. Andrea

using Microsoft.Azure;
using Microsoft.Azure.SpatialAnchors;
using Microsoft.Azure.SpatialAnchors.Unity;
using Microsoft.MixedReality.OpenXR;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.XR.WSA;
using UnityEngine.XR.WSA.Input;


public class AnchorsManager : MonoBehaviour
{    

    /// <summary>
    /// Input
    /// </summary>
    private GameObject localAnchor;

    /// <summary>
    /// AZURE settings AccountId
    /// </summary>
    private string AccountId = "00886172-b41a-....";
    
    /// <summary>
    /// AZURE settings AccountKey
    /// </summary>
    private string AccountKey = "09c3eddc-....";
    
    /// <summary>
    /// AZURE settings AccountDomain
    /// </summary>
    private string AccountDomain = "northeurope.mixedreality.azure.com";

    /// <summary>
    /// Azure session 
    /// </summary>
    private CloudSpatialAnchorSession cloudSession;


    /// <summary>
    /// For Storing the object Ids
    /// </summary>
    private AnchorLocateCriteria criteria =  new AnchorLocateCriteria();
    protected List<string> anchorIdsToLocate = new List<string>();


    void Start()
    {        
        InitializeSession();
    }


    /// <summary>
    /// Only when the user drag an object we can save the anchor with the latest position
    /// </summary>
    void onMouseObjectDown(GameObject prefab)
    {             
        this.localAnchor = prefab;
        this.cloudSession.Start();
    }


    /// <summary>
    /// Inizialize the cloudSpatialAnchorSession.
    /// </summary>
    void InitializeSession()
    {        
        
        try {
            this.cloudSession = new CloudSpatialAnchorSession();

            this.cloudSession.Configuration.AccountId = this.AccountId;
            this.cloudSession.Configuration.AccountKey = this.AccountKey;
            this.cloudSession.Configuration.AccountDomain = this.AccountDomain;             

            this.cloudSession.SessionUpdated += (sender, args) =>
            {
                if (args.Status.RecommendedForCreateProgress != 0)
                {
                    this.CreateAnchor();
                }
            };


            //WATCHERS            
            this.cloudSession.CreateWatcher(criteria);

            this.cloudSession.AnchorLocated += (object sender, AnchorLocatedEventArgs args) =>
            {
                switch (args.Status)
                {
                    case LocateAnchorStatus.Located:
                        CloudSpatialAnchor foundAnchor = args.Anchor;
                        Debug.Log("ASA Info: Anchor located! Identifier: " + args.Identifier);
                        break;
                    case LocateAnchorStatus.AlreadyTracked:
                        Debug.Log("This anchor has already been reported and is being tracked" + args.Identifier);
                        break;
                    case LocateAnchorStatus.NotLocated:
                        Debug.Log("ASA Info: Anchor not located. Identifier: " + args.Identifier);
                        break;
                    case LocateAnchorStatus.NotLocatedAnchorDoesNotExist:
                        Debug.LogError("ASA Error: Anchor not located does not exist. Identifier: " + args.Identifier);
                        break;
                }
            };

        }
        catch (Exception ex) {            
            Debug.LogError("Error on initialize the session: " + ex.Message);
        }

    }

    /// <summary>
    /// CreateAnchor 
    /// </summary>
    async void CreateAnchor()
    {

        // Create Anchor in local;
        localAnchor.AddComponent<CloudNativeAnchor>();
        CloudNativeAnchor cloudNativeAnchor = localAnchor.GetComponent<CloudNativeAnchor>();                                       
        if (cloudNativeAnchor.CloudAnchor == null) { await cloudNativeAnchor.NativeToCloud(); }

        // Add Anchor property;
        CloudSpatialAnchor cloudAnchor = cloudNativeAnchor.CloudAnchor;            
        cloudAnchor.AppProperties[@"room"] = @"bagno";
        cloudAnchor.AppProperties[@"product"] = @"saponetta";
        cloudAnchor.Expiration = DateTimeOffset.Now.AddDays(7);

        // Create Anchor
        await cloudSession.CreateAnchorAsync(cloudAnchor);        
        Debug.Log("Cloud anchor created with ID = " + cloudAnchor.Identifier);

        // Save Anchor ID
        this.anchorIdsToLocate.Add(cloudAnchor.Identifier);
        this.criteria.Identifiers = anchorIdsToLocate.ToArray();
        Debug.Log("Anchor Id has been saved on the array");

    }

}
Gelso77
  • 1,763
  • 6
  • 30
  • 47
  • I am not sure what device you are targeting and what exactly is the problem with above script. do you get an error? it would be helpful if you can provide more details you can use our [Quickstart] https://learn.microsoft.com/en-us/azure/spatial-anchors/quickstarts/get-started-unity-hololens?tabs=azure-portal . There are several github ASA samples https://github.com/Azure/azure-spatial-anchors-samples you can edit to your needs. – ClaudiuC_MSFT Jan 06 '23 at 10:43
  • I am trying to minimize the functionality in order to Create / Delete and Detect Anchors for reusing the script in different devices : actually it's very difficult to find the logic across all the scripts that you send me me https://github.com/Azure/azure-spatial-anchors-samples/tree/mehran/v2.7.0/Unity/Assets/AzureSpatialAnchors.Examples Moreover the example you provide me has 3 years – Gelso77 Jan 06 '23 at 14:52
  • i just want to initialize the session , create, delete, and detect anchors, seems easy but it's not from my point of view – Gelso77 Jan 06 '23 at 14:56
  • I think you are on the wrong branch. it should be "master" . https://github.com/Azure/azure-spatial-anchors-samples/blob/master/Unity/Assets/AzureSpatialAnchors.Examples/Scripts/DemoScriptBase.cs might contain what you are looking or this https://learn.microsoft.com/en-us/azure/spatial-anchors/tutorials/tutorial-new-unity-hololens-app?tabs=azure-portal – ClaudiuC_MSFT Jan 06 '23 at 18:18
  • hi. did this answered your question on "can't even find an understandable guideline" ? let me know if you need more info. – ClaudiuC_MSFT Jan 10 '23 at 12:54
  • Hello, thank you for your help, but too much code i can't understand the code and the logic, i need to proceed step by step ...tha's why i open another stackOverflow focusing on the initialize the cloud session https://stackoverflow.com/questions/75073925/azure-spatial-anchors-inizialize-the-cloudspatialanchorsession-exception many many many thanks – Gelso77 Jan 10 '23 at 17:48

0 Answers0