I am using the following code on iOS 18 and it gives me issues that ModelEntity.loadAsync is now deprecated. Any ideas how to fix this issue?
import ARKit
import RealityKit
import Combine
class Coordinator: NSObject, ARSessionDelegate {
weak var view: ARView?
var cancellable: AnyCancellable?
@objc func handleTap(_ recognizer: UITapGestureRecognizer) {
guard let view = self.view else { return }
let tapLocation = recognizer.location(in: view)
let results = view.raycast(from: tapLocation, allowing: .estimatedPlane, alignment: .horizontal)
if let result = results.first {
// create anchor entity
let anchor = AnchorEntity(raycastResult: result)
cancellable = ModelEntity.loadAsync(named: "shoe")
.sink { loadCompletion in
if case let .failure(error) = loadCompletion {
print("Unable to load model \(error)")
}
self.cancellable?.cancel()
} receiveValue: { entity in
anchor.addChild(entity)
}
view.scene.addAnchor(anchor)
}
}
}
Pity that loadAsync(named:in:)
type method is deprecated. The evolution of a structured concurrency continues, and everything you need can be found in the RealityKit
/SwiftUI
API. The solution is obvious: create AR app based on both frameworks – SwiftUI and UIKit. If you want to asynchronously load an Entity from a bundle, use the following initializer:
@MainActor @preconcurrency
convenience init(
named name: String,
in bundle: Bundle? = nil
) async throws