Hi -
As others have mentioned, you need to properly define how your custom properties will be encoded and decoded. Not doing so will crash the app when trying to archive the data as you've described. Here is a quick example of a custom ARAnchor that can successfully be archived and unarchived in your saved world data:
class CustomAnchor: ARAnchor {
// Your added property
var customProperty: String
// Init + your custom property
init(name: String, transform: simd_float4x4, customProperty: String) {
self.customProperty = customProperty
super.init(name: name, transform: transform)
}
override class var supportsSecureCoding: Bool {
return true
}
required init?(coder aDecoder: NSCoder) {
// Try to decode and initialize your custom value based on the key you set in your encoder
if let customProperty = aDecoder.decodeObject(forKey: "customProperty") as? String {
self.customProperty = customProperty
} else {
return nil
}
super.init(coder: aDecoder)
}
// As others have mentioned - this is required to maintain your custom properties as ARKit refreshes
required init(anchor: ARAnchor) {
let other = anchor as! CustomAnchor
self.customProperty = other.customProperty
super.init(anchor: anchor)
}
// Encode your custom property using a key to be decoded
override func encode(with aCoder: NSCoder) {
super.encode(with: aCoder)
aCoder.encode(customProperty, forKey: "customProperty")
}
}
This code is a slimmed down version of one of Apple's examples creating a custom anchor to store "snapshot" data in their persistence demo app: https://developer.apple.com/documentation/arkit/data_management/saving_and_loading_world_data
Topic:
App & System Services
SubTopic:
Core OS
Tags: