In my project I'm trying to store an ARMeshAnchor into UserdDefaluts converting it to Data. I'm unable to find a solution.
This one of my errors
Instance method encode(_:forKey:)
requires that ARMeshAnchor
conform to Encodable
.
Maybe it is better to convert them to an SCNNode and try to convert the node to Data?
Thanks
For this you should use NSKeyedArchiver
class and your code might look like this:
@IBOutlet var arView: ARView!
fileprivate func convertMeshAnchors() -> UserDefaults {
guard let frame = arView.session.currentFrame else { return }
let meshAnchors = frame.anchors.compactMap { $0 as? ARMeshAnchor }
let data = try! NSKeyedArchiver.archivedData(withRootObject: meshAnchors,
requiringSecureCoding: false)
let savedAnchors = UserDefaults.standard
savedAnchors.set(data, forKey: "MeshAnchors")
savedAnchors.synchronize()
return savedAnchors
}
And remember in RealityKit there's also a scene object where anchors' collection is located:
arView.scene.anchors