Hello!
maybe I come late to this thread but I have a problem and I couldn't find a solution yet.
I have this ARAnchor subclass and I think it has all it needs to be saved (I splitted SIMD3 in three FLoat variables because I thought it would give me less problems to save).
import ARKit
import RealityKit
class CustomARAnchor: ARAnchor {
var modelScalex: Float
var modelScaley: Float
var modelScalez: Float
init(name: String, transform: float4x4, modelScale: SIMD3<Float>) {
self.modelScalex = modelScale.x
self.modelScaley = modelScale.y
self.modelScalez = modelScale.z
super.init(name: name, transform: transform)
}
required init(anchor: ARAnchor) {
let other = anchor as! CustomARAnchor
self.modelScalex = other.modelScalex
self.modelScaley = other.modelScaley
self.modelScalez = other.modelScalez
super.init(anchor: other)
}
override class var supportsSecureCoding: Bool {
return true
}
required init?(coder aDecoder: NSCoder) {
if let modelScalex = aDecoder.decodeObject(forKey: "modelScalex") as? Float, let modelScaley = aDecoder.decodeObject(forKey: "modelScaley") as? Float, let modelScalez = aDecoder.decodeObject(forKey: "modelScalez") as? Float {
self.modelScalex = modelScalex
self.modelScaley = modelScaley
self.modelScalez = modelScalez
} else {
return nil
}
super.init(coder: aDecoder)
}
override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
aCoder.encode(self.modelScalex, forKey: "modelScalex")
aCoder.encode(self.modelScaley, forKey: "modelScaley")
aCoder.encode(self.modelScalez, forKey: "modelScalez")
}
}
In fact it seems it's correctly saved as this code inside the getWorldMap function prints me the names of my custom anchors
self.arView.session.getCurrentWorldMap { worldMap, _ in
guard let map = worldMap else {
print("Can't get current world map")
return
}
for anchor in map.anchors {
if let anchor = anchor as? CustomARAnchor{
if let name = anchor.name{
print("custom anchor " + name + " in map")
}
}
}
do {
let data = try NSKeyedArchiver.archivedData(withRootObject: map, requiringSecureCoding: true)
try data.write(to: URL(fileURLWithPath: self.worldMapFilePath), options: [.atomic])
} catch {
fatalError("Can't save map: \(error.localizedDescription)")
}
}
I save the world map in a file and then load it in another application.
The Problem is that when i load the map the custom anchors won't be recognized or even loaded because the func session(_, didAdd anchors) adds just one anchor (i guess its a plane recognized) and not my custom anchors.
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
print("did add anchor: \(anchors.count) anchors in total")
for anchor in anchors {
if let name = anchor.name {
print("DEBUG: anchor" + name)
}
if let _ = anchor as? CustomARAnchor {
print("DEBUG: found custom anchor")
}
addAnchorEntityToScene(anchor: anchor)
}
}
All the code works fine if i use normal ARAnchors and when I use CustomARAnchors it works fluently (no crashes or errors) but it stops doing what it is supposed to do.
If anybody can have an idea of where the problem is I would be very grateful.
Thank in advance even for reading this!