gpt4 book ai didi

swift - RealityKit - 异步模型加载不起作用

转载 作者:行者123 更新时间:2023-12-02 16:35:45 27 4
gpt4 key购买 nike

此代码有效:

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 代码版本了解如何异步加载模型:

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

enter image description here

关于swift - RealityKit - 异步模型加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62752649/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com