Skip to content

Instantly share code, notes, and snippets.

@eospi
Last active August 2, 2024 19:27
Show Gist options
  • 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()
})
}
@nschos
Copy link

nschos commented Aug 2, 2024

Pretty nice 👍 It helped me a lot!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment