Skip to content

Instantly share code, notes, and snippets.

@eospi
Last active May 4, 2023 21:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eospi/ff37e4fa2e1333544b46054f98ca43cf to your computer and use it in GitHub Desktop.
Save eospi/ff37e4fa2e1333544b46054f98ca43cf to your computer and use it in GitHub Desktop.
RealityKit Asynchronous Loading
import Combine
func loadEntityAsync() {
// Create an world anchor at the origin and add it to the scene
let anchor = AnchorEntity(world: [0,0,0])
arView.scene.addAnchor(anchor)
let usdzPath = "path/to/usdz/asset"
// Load the asset asynchronously
var cancellable: AnyCancellable? = nil
cancellable = ModelEntity.loadModelAsync(named: usdzPath)
.sink(receiveCompletion: { error in
print("Unexpected error: \(error)")
cancellable?.cancel()
}, receiveValue: { entity in
anchor.addChild(entity)
cancellable?.cancel()
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment