Hololens anchor使用与共享

原创文档,转载请注明地址http://blog.csdn.net/fdbvm/article/details/79324718

hololens中anchor代表空间坐标的一个锚点,当给gameobject增加一个WorldAnchor时,相当于把这个物体固定在该位置,将无法移动和旋转该物体。
创建anchor: gameObject.AddComponent<WorldAnchor>();
销毁anchor: Destroy(transform.GetComponent<WorldAnchor>());
--------------------------------------------------
anchorStore 用来将anchor信息在该设备中存储起来,以便下次再次通过周围空间信息识别出这个anchor的时候来使用
anchorStore 初始化:
   private void Start()
    {
        WorldAnchorStore.GetAsync(StoreLoaded);
    }
    private void StoreLoaded(WorldAnchorStore store)
    {
        this.anchorStore = store;
    }
启动的时候从设备将anchorStore重新加载出来,从而复用之前已经保存好的anchor信息
anchorStore中添加anchor: 
anchorStore.Save(exportingAnchorName, transform.GetComponent<WorldAnchor>());
从anchorstore中加载anchor到指定gameobject上:anchorStore.Load(exportingAnchorName, gameObject);

------------------------------------
原创文档,转载请注明地址http://blog.csdn.net/fdbvm/article/details/79324718
anchor可以在多个设备间共享,来使得不同设备可以使用相同的空间位置,可以通过WorldAnchorTransferBatch将anchor信息导出为byte数组,在另外一台设备中加载这个数组并重新还原出anchor信息
导出代码如下:
 private List<byte> exportingAnchorBytes = new List<byte>();

            WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();
            transferBatch.AddWorldAnchor(exportingAnchorName, transform.GetComponent<WorldAnchor>());
            WorldAnchorTransferBatch.ExportAsync(transferBatch, OnExportDataAvailable, OnExportComplete);

    private void OnExportComplete(SerializationCompletionReason completionReason)
    {
        if (completionReason != SerializationCompletionReason.Succeeded)
        {
            Debug.Log("share anchor complete");
        }
        else
        { }
    }
    private void OnExportDataAvailable(byte[] data)
    {
        exportingAnchorBytes.AddRange(data);
    }
导入代码如下:
WorldAnchorTransferBatch.ImportAsync(exportingAnchorBytes.ToArray(), OnImportComplete);

  int retryCount = 5;
    private void OnImportComplete(SerializationCompletionReason completionReason, WorldAnchorTransferBatch deserializedTransferBatch)
    {
        if (completionReason != SerializationCompletionReason.Succeeded)
        {
            Debug.Log("Failed to import: " + completionReason.ToString());
            if (retryCount > 0)
            {
                retryCount--;
                WorldAnchorTransferBatch.ImportAsync(exportingAnchorBytes.ToArray(), OnImportComplete);
            }
            return;
        }
       
        string[] ids = deserializedTransferBatch.GetAllIds();
        Debug.Log("load anchor count " + ids.Length);
        foreach (string id in ids)
        {
            Debug.Log("load anchor "+id);
            if (cubeTransform.gameObject != null&&id.Equals(exportingAnchorName))
            {
                Debug.Log("find anchor form share");
                if (cubeTransform.GetComponent<WorldAnchor>() == null) cubeTransform.gameObject.AddComponent<WorldAnchor>();
                deserializedTransferBatch.LockObject(id, cubeTransform.gameObject);
                return;
            }
        }
    }

没有更多推荐了,返回首页