ARPersistence 是一个 Swift 包,它提供了一个实用工具,用于在 ARKit 和 RealityKit 项目中保存和加载 AR 锚点和模型实体变换。 它使您能够持久化 AR 对象的状态,并在以后恢复它们。
上面的示例演示了 ARPersistence 在 AR 应用程序中的用法。 它展示了在 AR 场景中放置多个 3D 模型,保存场景的最终状态,并在以后加载它以恢复模型的精确位置和配置的能力。
详细的功能特性
您可以使用 Swift Package Manager 将 ARPersistence 添加到您的项目中。
https://github.com/Shiru99/ARPersistence
。在您的 Swift 文件中导入 ARPersistence 模块
import ARPersistence
使用 ARPersistence.shared
实例来保存和加载 AR 场景状态
// Save the current state of the AR scene
ARPersistence.shared.save(arView)
// Load the previously saved state of the AR scene
ARPersistence.shared.load(arView)
确保 ARAnchor
名称与 modelName
匹配。(必须)
let arAnchor = ARAnchor(name: modelName, transform: matrix_identity_float4x4)
在创建 AnchorEntity
时,使用 ARAnchor
初始化它 & 将 AnchorEntity
的 name
属性设置为 arAnchor.identifier.uuidString
。(必须)
let anchorEntity = AnchorEntity(anchor: arAnchor)
anchorEntity.name = arAnchor.identifier.uuidString
请务必按照这些步骤正确保存和加载 AR 场景状态,确保 ARAnchor 名称与 modelName 匹配,并且 AnchorEntity 已正确初始化和命名。
如有任何疑问或建议,请随时联系 Shiru99