📅  最后修改于: 2022-03-11 14:57:51.800000             🧑  作者: Mango
import SwiftUI
import RealityKit
struct ARViewContainer: UIViewRepresentable {
@Binding var showed: Bool
let anchor = AnchorEntity(world: [0, 0,-1])
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let cube = MeshResource.generateBox(size: 0.8, cornerRadius: 0.02)
let material = SimpleMaterial(color: .red, isMetallic: true)
let cubeEntity = ModelEntity(mesh: cube, materials: [material])
anchor.addChild(cubeEntity)
arView.scene.addAnchor(anchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
if showed == true {
uiView.scene.removeAnchor(anchor)
}
}
}
struct ContentView : View {
@State private var show = false
var body: some View {
VStack {
ARViewContainer(showed: $show)
VStack {
Button(action: { self.show.toggle() }) {
Text("Remove Model")
}
}
}
}
}