RealityKit –异步模型加载不起作用 [英] RealityKit – Asynchronous model loading doesn't work

查看:148
本文介绍了RealityKit –异步模型加载不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有效:

let entity = try! Entity.load(named: "toy_robot_vintage")
anchorEntity.addChild(entity)

但这不是:

_ = Entity.loadAsync(named: "toy_robot_vintage")
        .sink(receiveCompletion: { loadCompletion in
            print("This is never executed")
        }, receiveValue: { entity in
            print("This is never executed")
            anchorEntity.addChild(entity)
        })

可能是什么问题?

推荐答案

使用以下macOS代码版本来查找如何异步加载模型:

Use the following macOS code version to find out how to load model asynchronously:

import AppKit
import RealityKit
import Combine

class GameViewController: NSViewController {
    
    @IBOutlet var arView: ARView!
    var model: ModelEntity? = nil
    let anchor = AnchorEntity()
    var cancellable: AnyCancellable? = nil
    
    override func awakeFromNib() {
    
        arView.environment.background = .color(.systemTeal)
                
        cancellable = Entity.loadModelAsync(named: "Glasses.usdz")
            .sink(receiveCompletion: { completion in
                if case let .failure(error) = completion {
                    print("Unable to load a model due to error \(error)")
                }
                self.cancellable?.cancel()
                
            }, receiveValue: { [self] (model: Entity) in
                if let model = model as? ModelEntity {
                    self.model = model
                    cancellable?.cancel()
                    print("Congrats! Model is successfully loaded!")
                    anchor.addChild(model)
                    anchor.position = [0.4, 1.5, -1]
                    anchor.scale = [300, 300, 300]        // set appropriate scale
                    arView.scene.anchors.append(anchor)
                }
            })
    }
}

这篇关于RealityKit –异步模型加载不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆