问题描述
我在将loadAsync用于TextResource类时遇到问题。它返回LoadRequest<TextureResource>
,但我也不知道该如何处理。这些对象的文档非常差,而且我也找不到任何教程。任何人都可以解决吗?谢谢!
let url = URL(fileURLWithPath: imageFilePath)
let texture = try! TextureResource.loadAsync(contentsOf: url).result!.get()
var material = SimpleMaterial()
material.baseColor = MaterialColorParameter.texture(texture)
let entity = ModelEntity(mesh: paintingMesh,materials: [material])
解决方法
好的,所以我解决了这个问题,我想分享答案! 因此,基本上我们必须使用Combine框架来处理该异步请求 有一个代码示例,请记住添加导入合并
var textureRequest: AnyCancellable? = nil
textureRequest = TextureResource.loadAsync(contentsOf: url).sink { (error) in
print(error)
textureRequest?.cancel()
} receiveValue: { (texture) in
var material = UnlitMaterial()
material.baseColor = MaterialColorParameter.texture(texture)
// Do another setup
textureRequest?.cancel()
}
重要的是,我们必须取消请求才能取消请求。 您可以在Apple文档https://developer.apple.com/documentation/combine/receiving-and-handling-events-with-combine中找到更多信息 还是有https://medium.com/@ethansaadia/realitykit-assets-52ada3f9465f