Android设备Azure空间锚点存储报错:Could not obtain the ARAnchor.nativePtr
问题现象
在Android设备上将空间锚点保存到Azure Spatial Anchor云服务时,持续触发以下错误:
InvalidOperationException: Could not obtain the ARAnchor.nativePtr
错误发生在执行await cloudNativeAnchor.NativeToCloud()时;后续尝试手动获取原生锚点指针的代码中,GetPointer()返回空值。
环境配置
- Unity 2020.3.30f1
- ASA 2.13.3
- AR Core XR Plugin 4.1.13
- MRTK 2.8.2
用户尝试的代码
初始实现代码
private async Task CreateAnchor(Vector3 position) { //... GameObject anchorGameObject = GameObject.Instantiate(_CubePrefab); anchorGameObject.GetComponent<MeshRenderer>().material.color = Color.white; anchorGameObject.transform.position = position; anchorGameObject.transform.rotation = orientationTowardsHead; anchorGameObject.transform.localScale = Vector3.one * 0.1f; //Add and configure ASA components CloudNativeAnchor cloudNativeAnchor = anchorGameObject.AddComponent<CloudNativeAnchor>(); if (cloudNativeAnchor.CloudAnchor == null) { await cloudNativeAnchor.NativeToCloud(); } // 此处触发错误 CloudSpatialAnchor cloudSpatialAnchor = cloudNativeAnchor.CloudAnchor; cloudSpatialAnchor.Expiration = DateTimeOffset.Now.AddDays(3); //... }
更新后的尝试代码
//Add and configure ASA components CloudNativeAnchor cloudNativeAnchor = anchorGameObject.AddComponent<CloudNativeAnchor>(); // Then we create a new local cloud anchor CloudSpatialAnchor cloudSpatialAnchor = new CloudSpatialAnchor(); // Now we set the local cloud anchor's position to the native XR anchor's position cloudSpatialAnchor.LocalAnchor = await anchorGameObject.FindNativeAnchor().GetPointer(); // Check to see if we got the local XR anchor pointer if (cloudSpatialAnchor.LocalAnchor == IntPtr.Zero) { UnityEngine.Debug.Log("Didn't get the local anchor..."); return; } else { UnityEngine.Debug.Log("Local anchor created"); }
解决方案
核心原因
报错本质是当前GameObject未关联有效的原生ARAnchor实例。CloudNativeAnchor.NativeToCloud()依赖AR Foundation创建的本地ARAnchor生成云锚点关联,直接实例化的Cube预制体本身没有绑定原生ARAnchor,导致无法获取nativePtr。
修复步骤
先创建原生ARAnchor,再关联GameObject
不要直接给普通GameObject添加CloudNativeAnchor,需先通过AR Core API创建本地ARAnchor,再绑定到GameObject:private async Task CreateAnchor(Vector3 position, Quaternion rotation) { // 1. 检查AR Session状态 if (ARSession.state != ARSessionState.SessionTracking) { Debug.LogWarning("AR Session未处于追踪状态,请等待初始化完成"); return; } var session = ARSession.Session; // 2. 创建原生ARAnchor ARAnchor nativeAnchor = session.AddAnchor(new Pose(position, rotation)); if (nativeAnchor == null) { Debug.LogError("创建原生ARAnchor失败"); return; } // 3. 将原生Anchor绑定到GameObject GameObject anchorGameObject = GameObject.Instantiate(_CubePrefab); anchorGameObject.GetComponent<MeshRenderer>().material.color = Color.white; anchorGameObject.transform.SetPositionAndRotation(position, rotation); anchorGameObject.transform.localScale = Vector3.one * 0.1f; // 4. 添加CloudNativeAnchor并关联原生Anchor CloudNativeAnchor cloudNativeAnchor = anchorGameObject.AddComponent<CloudNativeAnchor>(); await cloudNativeAnchor.NativeToCloud(nativeAnchor); // 传入原生ARAnchor实例 // 5. 配置云锚点并保存 CloudSpatialAnchor cloudSpatialAnchor = cloudNativeAnchor.CloudAnchor; cloudSpatialAnchor.Expiration = DateTimeOffset.Now.AddDays(3); // 保存到ASA云服务 var saveOperation = _cloudSpatialAnchorSession.CreateAnchorAsync(cloudSpatialAnchor); await saveOperation; if (saveOperation.Status == CloudSpatialAnchorOperationStatus.Succeeded) { Debug.Log("云锚点保存成功"); } else { Debug.LogError($"保存失败: {saveOperation.ErrorMessage}"); } }确保AR Session处于有效状态
在创建锚点前必须确认AR Session完成初始化并处于SessionTracking状态,否则无法创建有效原生ARAnchor。验证插件兼容性
当前ASA 2.13.3与AR Core XR Plugin 4.1.13版本兼容,若问题仍存在,可尝试将ASA升级至最新兼容版本,或回退AR Core插件到官方推荐版本。避免无绑定状态下调用FindNativeAnchor()
FindNativeAnchor()仅在GameObject已绑定原生ARAnchor时返回有效结果,未提前创建原生Anchor时会返回空值,导致GetPointer()无法获取指针。
内容的提问来源于stack exchange,提问作者ChocooPanda

