定位器定位后,应用程序突然关闭

时间:2019-08-21 13:55:02

标签: c# hololens azure-spatial-anchors

我最近正在Hololens中开发Azure空间锚。从Microsoft网站here阅读完本教程之后,我调整了一些代码。到目前为止,它运行良好。直到找到我创建的锚点为止。找到我的锚点(实例化定位的锚点)后,我的应用突然关闭。直到显示文本“找到!”的部分。这是我的代码中找到锚点的一部分。

    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();
    }

我使用了Azure Spatial Anchor SDK v1.1.0,Unity3D 2019.1.10f和Visual Studio 2017。

有人知道原因吗?

1 个答案:

答案 0 :(得分:0)

由于大多数Unity Engine API只能在主线程中调用,因此对UI更新的更新只能在主线程上进行。

更多信息,请参见:Thread safety in Unity