ARPersistence is a Swift package that provides a utility for saving and loading AR anchors and model entity transforms in an ARKit and RealityKit project. It enables you to persist the state of AR objects and restore them later.
The above example demonstrates the usage of ARPersistence in an AR application. It showcases the ability to place multiple 3D models in an AR scene, save the final state of the scene, and load it later to restore the exact placement and configuration of the models.
- Save and load AR anchors and model entity transforms.
- Store AR world maps and model entity details in files.
- Retrieve and apply saved data to restore the AR experience.
- iOS 15.0+
You can add ARPersistence to your project using Swift Package Manager.
- Open your Xcode project.
- Go to “File” -> “Swift Packages” -> “Add Package Dependency”.
- Enter the repository URL:
- Choose the version or branch you want to use.
- Click “Next” and Xcode will resolve the package and add it to your project.
Import the ARPersistence module in your Swift file:
ARPersistence.sharedinstance to save and load the AR scene state:
// 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)
Ensure that the
ARAnchorname matches the
let arAnchor = ARAnchor(name: modelName, transform: matrix_identity_float4x4)
When creating the
ARAnchorto initialize it & Set the
nameproperty of the
let anchorEntity = AnchorEntity(anchor: arAnchor) anchorEntity.name = arAnchor.identifier.uuidString
Make sure to follow these steps to properly save and load the AR scene state, ensuring that the ARAnchor names match the modelName and the AnchorEntity is correctly initialized and named.
For any inquiries or suggestions, please feel free to reach out to Shiru99