0

我最近在 Hololens 中开发 Azure Spatial Anchor。从这里的 Microsoft 网站学习本教程后,我调整了一些代码。到目前为止效果很好。直到我试图找到我创建的锚。在找到我的锚点(并实例化定位的锚点)后,我的应用程序突然关闭。它一直到达显示文本“找到!”的部分。这是我找到锚点的代码的一部分。

    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 空间锚 SDK v1.1.0、Unity3D 2019.1.10f 和 Visual Studio 2017。

有人知道原因吗?

4

1 回答 1

0

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

更多信息请参阅:Unity 中的线程安全

于 2019-08-22T12:26:30.767 回答