Skip to content

Instantly share code, notes, and snippets.

@vrat28
Forked from eospi/loadEntityAsync.swift
Created June 6, 2021 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrat28/253d334ff09868d1a692c924f66e0e8a to your computer and use it in GitHub Desktop.
Save vrat28/253d334ff09868d1a692c924f66e0e8a 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