在本地将永久性世界锚保存到HoloLens2上

问题描述

我目前正在一个房间规模的统一项目中工作,在那里我需要使用持久的世界锚点来在设备上本地保存docker run的位置,而无需使用任何Internet访问或Wifi。

我正在使用GameObjectUnity 2019.4.7设备。

我遇到了来自早期HoloLens2项目以及HoloLens1的{​​{1}}类的一些实现,这似乎与{{1}的旧WorldAnchorManager类似}。 (例如:https://forums.hololens.com/discussion/2667/how-to-use-unity-world-anchorshttps://codeholo.com/2019/02/01/anchoring-your-gameobjects-in-hololens-apps

不幸的是,没有解决方案对我有用。

似乎MRTK 2.4甚至都没有保存在WorldAnchorManager中。我还尝试使用没有HoloToolkit级的官方Unity WorldAnchors,即使WorldAnchorStore中将弃用它。

有人可以给我提供一个可以在我的设置中工作的示例吗?

对于使锚起作用的每个提示,我也将感到高兴。

我当前使用WorldAnchorStore的代码如下所示。 WorldAnchorManager实例中的固定锚已启用。

Unity 2020

解决方法

这是我的一个项目中的一些代码行,可能对您有帮助。

导入

    private byte[] _importedData;
    private int _retryCount = 3;

    internal bool ImportAnchors()
    {
        _retryCount = 3;
        string path = GetWorldAnchorsDataPath(); // your path
        if (!File.Exists(path))
        {
            Debug.Log("Fail ImportWorldAnchors ImportAnchors File does not exist");
            return false;
        }

        _importedData = File.ReadAllBytes(path);
        Debug.Log("_importedData count " + _importedData.Length);
        WorldAnchorTransferBatch.ImportAsync(_importedData,OnImportComplete);
    }

    private void OnImportComplete(SerializationCompletionReason completionReason,WorldAnchorTransferBatch deserializedTransferBatch)
    {
        if (completionReason != SerializationCompletionReason.Succeeded)
        {
            Debug.Log("(" + _retryCount + ") Failed to import: " + completionReason.ToString());
            if (_retryCount > 0)
            {
                _retryCount--;
                WorldAnchorTransferBatch.ImportAsync(_importedData,OnImportComplete);
            }
            return;
        }
        Debug.Log("Success ImportWorldAnchors OnImportComplete,anchors count : " + deserializedTransferBatch.anchorCount);
        AnchorsManager.GetAndUpdateAnchorsFromBatch(deserializedTransferBatch);

        deserializedTransferBatch.Dispose();
    }    

  internal static string GetWorldAnchorsDataPath()
    {
        return Application.persistentDataPath + "/worldAnchors.dat";
    }
  

导出

 internal void ExportAnchors()
    {            
        string path = GetWorldAnchorsDataPath();
        if (File.Exists(path))
        {
            FileStream fileStream = File.Open(path,FileMode.Open);

            /* 
             * Set the length of filestream to 0 and flush it to the physical file.
             *
             * Flushing the stream is important because this ensures that
             * the changes to the stream trickle down to the physical file.
             * 
             */
            fileStream.SetLength(0);
            fileStream.Close(); // This flushes the content,too.
        }

        WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();

        AddAnchorsToBatch(transferBatch); // your own function to use your batch
        Debug.Log("Nb anchors in batch : " + transferBatch.anchorCount);
        WorldAnchorTransferBatch.ExportAsync(transferBatch,OnExportDataAvailable,OnExportComplete);
    }

    private void OnExportDataAvailable(byte[] data)
    {
        AppendAllBytes(GetWorldAnchorsDataPath(),data);    
    }

    private void OnExportComplete(SerializationCompletionReason completionReason)
    {
        if (completionReason != SerializationCompletionReason.Succeeded)
        {
            SendExportFailedToClient();
        }
        else
        {
            SendExportSucceededToClient();
        }
    }

    private void SendExportFailedToClient()
    {
        Debug.Log("Export fail");
        string path = GetWorldAnchorsDataPath();
        if (File.Exists(path))
        {
            File.Delete(path);
        }
        SuccessToExportWorldAnchors(false);
    }

    private void SendExportSucceededToClient()
    {
        Debug.Log("Export succeed");
        SuccessToExportWorldAnchors(true);
    }


    private static void AppendAllBytes(string path,byte[] bytes)
    {
        //argument-checking here.

        using (var stream = new FileStream(path,FileMode.Append))
        {
            stream.Write(bytes,bytes.Length);
        }
    }

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...