使用 .loadModelAsync() 时可用动画集合为空
Available animations collection is empty, while using .loadModelAsync()
我使用 LoadModelAsync 在我的 RealityKit 场景中成功地异步加载了 USDZ 文件。我正在使用苹果提供的 toy_biplane USDZ 文件,它具有默认动画。如果我异步加载模型,availableAnimations 数组总是空的!
我的代码:
func loadASingleModel(name: String,
completion: @escaping (_ model: ModelEntity?) -> Void) {
var cancellable: AnyCancellable?
cancellable = Entity.loadModelAsync(named: name)
.sink(receiveCompletion: { handler in
if case let .failure(error) = handler {
print("Unable to load a model due to error \(error)")
}
cancellable?.cancel()
completion(nil)
}, receiveValue: { [self] (model: ModelEntity?) in
if let model = model {
cancellable?.cancel()
print("Congrats! Model is successfully loaded!")
print(model.availableAnimations,
//Here availableAnimations is empty
"Debug: Any Animation?")
completion(model)
}
})
}
}
如何使用动画异步加载 USDZ 文件?
使用ModelEntity.loadAsync
。它加载完整的层次结构,包括动画。
import RealityKit
import Combine
let anchor = AnchorEntity()
var cancellable: AnyCancellable? = nil
self.cancellable = ModelEntity.loadAsync(named: "biplane.usdz").sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Unable to load a model \(error)")
}
self.cancellable?.cancel()
},
receiveValue: { [self] planeModel in
let animation = planeModel.availableAnimations[0]
anchor.addChild(planeModel)
anchor.position = [0, 0,-2]
anchor.scale = [0.1, 0.1, 0.1]
arView.scene.anchors.append(anchor)
planeModel.playAnimation(animation.repeat(count: .max))
}
)
我使用 LoadModelAsync 在我的 RealityKit 场景中成功地异步加载了 USDZ 文件。我正在使用苹果提供的 toy_biplane USDZ 文件,它具有默认动画。如果我异步加载模型,availableAnimations 数组总是空的!
我的代码:
func loadASingleModel(name: String,
completion: @escaping (_ model: ModelEntity?) -> Void) {
var cancellable: AnyCancellable?
cancellable = Entity.loadModelAsync(named: name)
.sink(receiveCompletion: { handler in
if case let .failure(error) = handler {
print("Unable to load a model due to error \(error)")
}
cancellable?.cancel()
completion(nil)
}, receiveValue: { [self] (model: ModelEntity?) in
if let model = model {
cancellable?.cancel()
print("Congrats! Model is successfully loaded!")
print(model.availableAnimations,
//Here availableAnimations is empty
"Debug: Any Animation?")
completion(model)
}
})
}
}
如何使用动画异步加载 USDZ 文件?
使用ModelEntity.loadAsync
。它加载完整的层次结构,包括动画。
import RealityKit
import Combine
let anchor = AnchorEntity()
var cancellable: AnyCancellable? = nil
self.cancellable = ModelEntity.loadAsync(named: "biplane.usdz").sink(
receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Unable to load a model \(error)")
}
self.cancellable?.cancel()
},
receiveValue: { [self] planeModel in
let animation = planeModel.availableAnimations[0]
anchor.addChild(planeModel)
anchor.position = [0, 0,-2]
anchor.scale = [0.1, 0.1, 0.1]
arView.scene.anchors.append(anchor)
planeModel.playAnimation(animation.repeat(count: .max))
}
)