Reputation: 31
Is there any way to load usdz model to a custom entity class?
I tried to cast the returned ModelEntity to my custom class but it didn't work out.
let entity: CustomEntity = try! CustomEntity.load(named: name) as! CustomEntity
Upvotes: 2
Views: 716
Reputation: 58093
import UIKit
import RealityKit
class CustomClass: Entity, HasModel {
let modelName: String? = "gramophone"
let myAnchor = AnchorEntity()
func loader() -> AnchorEntity {
if let name = self.modelName {
let modelEntity = try! CustomClass.loadModel(named: name)
myAnchor.addChild(modelEntity)
}
return myAnchor
}
}
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
let modelName: String? = "gramophone"
override func viewDidLoad() {
super.viewDidLoad()
let usdz = CustomClass().loader()
arView.scene.anchors.append(usdz)
}
}
import SwiftUI
import RealityKit
class CustomClass: Entity, HasModel {
func printer() {
print("I'm inside CustomClass...")
}
}
struct ARViewContainer: UIViewRepresentable {
let modelName: String? = "gramophone"
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
typealias CustomEntity = ModelEntity
var modelEntity = CustomEntity()
if let name = self.modelName {
modelEntity = try! CustomClass.loadModel(named: name)
let anchor = AnchorEntity()
anchor.addChild(modelEntity)
arView.scene.anchors.append(anchor)
}
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
CustomClass().printer()
}
}
Upvotes: 0