swift - ModelEntity.loadAsync is deprecated in iOS 18 - Stack Overflow

I am using the following code on iOS 18 and it gives me issues that ModelEntity.loadAsync is now deprec

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)
        }
    }
}

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)
        }
    }
}
Share Improve this question edited Nov 16, 2024 at 9:10 Andy Jazz 59.3k18 gold badges162 silver badges257 bronze badges asked Nov 15, 2024 at 20:57 john doejohn doe 9,72223 gold badges94 silver badges178 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745671903a4639454.html

相关推荐

  • swift - ModelEntity.loadAsync is deprecated in iOS 18 - Stack Overflow

    I am using the following code on iOS 18 and it gives me issues that ModelEntity.loadAsync is now deprec

    6小时前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信