问题描述
我目前正在一个房间规模的统一项目中工作,在那里我需要使用持久的世界锚点来在设备上本地保存docker run
的位置,而无需使用任何Internet访问或Wifi。
我正在使用GameObject
和Unity 2019.4.7
设备。
我遇到了来自早期HoloLens2
项目以及HoloLens1
的{{1}}类的一些实现,这似乎与{{1}的旧WorldAnchorManager
类似}。 (例如:https://forums.hololens.com/discussion/2667/how-to-use-unity-world-anchors或https://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);
}
}