0

I'm recently developing Azure Spatial Anchor in Hololens. After following this tutorial from the Microsoft Website here, I tweak some of my code. It worked well so far. Until I try to find the anchor I have created. After my anchor is found (and the located anchor instantiated), my apps suddenly closed. It reach until the part where it shows text "Found!". Here is part of my code that finding the anchor.

    void Update(){
        lock (dispatchQueue)
        {
            if (dispatchQueue.Count > 0)
            {
                dispatchQueue.Dequeue()();
            }
        }

    }

    protected void QueueOnUpdate(Action updateAction)
    {
        lock (dispatchQueue)
        {
            dispatchQueue.Enqueue(updateAction);
        }
    }


    private void CloudSpatialAnchorSession_AnchorLocated(object sender, AnchorLocatedEventArgs args){
        switch (args.Status){
            case LocateAnchorStatus.Located:
                QueueOnUpdate(() =>{
                    var cube = GameObject.Instantiate(cubePrefab) as GameObject;
                    cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
                    cube.AddComponent<WorldAnchor>();
                    cube.GetComponent<UnityEngine.XR.WSA.WorldAnchor>().SetNativeSpatialAnchorPtr(args.Anchor.LocalAnchor);
                    cubeMaterial = cube.GetComponent<Renderer>().material;
                    cubeMaterial.color = Color.red;

                    this.cubes.Add(cube);
                    cloudAnchorId = "";
                    this.msg.text = "Found!";
                    Task.Run(async () =>{
                        await Task.Delay(1000);
                    });
                });
                break;
            case LocateAnchorStatus.AlreadyTracked:
                this.msg.text = "ASA Info: Anchor already tracked. Identifier: " + args.Identifier;
                break;
            case LocateAnchorStatus.NotLocated:
                this.msg.text = "ASA Info: Anchor not located. Identifier: " + args.Identifier;
                break;
            case LocateAnchorStatus.NotLocatedAnchorDoesNotExist:
                this.msg.text = "ASA Error: Anchor not located does not exist. Identifier: " + args.Identifier;
                break;
        }
    }
    private void CloudSpatialAnchorSession_LocateAnchorsCompleted(object sender, LocateAnchorsCompletedEventArgs args){
        this.msg.text = "ASA Info: Locate anchors completed. Watcher identifier: " + args.Watcher.Identifier;
        Task.Run(async () =>{
            await Task.Delay(2500);
        });
        args.Watcher.Stop();
    }

I used Azure Spatial Anchor SDK v1.1.0, Unity3D 2019.1.10f, and Visual Studio 2017.

anyone know the reason?

BroColly
  • 45
  • 8
  • Is there any error message when the error occurs? Have you tried to restore the code to the original state for testing?There aren't too many anchors in the code of tutorial. It shouldn't cause the game crashed error due to creating too many threads, so there is no need to add delay at the end of the loop. – Hernando - MSFT Aug 22 '19 at 09:54
  • @Hernando I'm new to hololens so I don't know the best practice to see the error log. I found out that showing the error log to Text GameObject causes the crash, I don't know why though. Now, my apps is working well without problem after I comment all "this.msg.text =" code in function. I also have try to restore the original state code in the tutorial, and apps suddenly crash after showing log in Text GameObject just when the session started. – BroColly Aug 22 '19 at 10:56
  • Thank you for your input. This problem has been solved. – BroColly Aug 22 '19 at 10:58

1 Answers1

0

Since most Unity Engine API can only be called in the main thread, updates to the UI updates can only happen on the main thread.

More information please see:Thread safety in Unity

Hernando - MSFT
  • 2,895
  • 1
  • 5
  • 11