ARPersistence

ARPersistence 是一个 Swift 包,它提供了一个实用工具,用于在 ARKit 和 RealityKit 项目中保存和加载 AR 锚点和模型实体变换。 它使您能够持久化 AR 对象的状态,并在以后恢复它们。

上面的示例演示了 ARPersistence 在 AR 应用程序中的用法。 它展示了在 AR 场景中放置多个 3D 模型,保存场景的最终状态,并在以后加载它以恢复模型的精确位置和配置的能力。

功能

功能特性

详细的功能特性

要求

安装

您可以使用 Swift Package Manager 将 ARPersistence 添加到您的项目中。

  1. 打开您的 Xcode 项目。
  2. 转到 "File" -> "Swift Packages" -> "Add Package Dependency"。
  3. 输入存储库 URL:https://github.com/Shiru99/ARPersistence
  4. 选择您要使用的版本或分支。
  5. 单击 "Next",Xcode 将解析包并将其添加到您的项目中。

用法

  1. 在您的 Swift 文件中导入 ARPersistence 模块

    import ARPersistence
    
  2. 使用 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)
    
  3. 确保 ARAnchor 名称与 modelName 匹配。(必须)

    let arAnchor = ARAnchor(name: modelName, transform: matrix_identity_float4x4)
    
  4. 在创建 AnchorEntity 时,使用 ARAnchor 初始化它 & 将 AnchorEntityname 属性设置为 arAnchor.identifier.uuidString。(必须)

    let anchorEntity = AnchorEntity(anchor: arAnchor)
    anchorEntity.name = arAnchor.identifier.uuidString
    

请务必按照这些步骤正确保存和加载 AR 场景状态,确保 ARAnchor 名称与 modelName 匹配,并且 AnchorEntity 已正确初始化和命名。

联系方式

如有任何疑问或建议,请随时联系 Shiru99