5

I want to remove an Entity from my Scene.

I created my Entity inside of the UpdateUIView function like this:

// create anchor and model-Entity
let anchor = AnchorEntity(plane: .horizontal)
    
let cube = MeshResource.generateBox(size: 0.1, cornerRadius: 0.03)
let material = SimpleMaterial(color: .gray, roughness: 0.2, isMetallic: true)
let cubeEntity = ModelEntity(mesh: cube, materials: [material])
    
anchor.addChild(cubeEntity)
uiView.scene.addAnchor(anchor)

Now I want to remove it, by pressing a Button in my User Interface. By pressing the button I change a var from false to true. Then I wrote inside the UpdateUIView function:

if remove == true {
    uiView.scene.removeAnchor(anchor)
}

When I press the button it changes the Boolean to true but the Entity does not disappear.

Any suggestions on how to solve this problem?

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
User_404
  • 91
  • 5

1 Answers1

3

updateUIView(...)

Use the following code to get a desired result:

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")
                }
            }
        }
    }
}    

Coordinator

It's not obligatory to update your content inside updateUIView(...) instance method. Instead you could use Coordinator class with custom makeCoordinator() method.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • so the only difference is, that I declare the anchor outside of the functions and inside the Structure? Do you know why it does not work when I write everything inside the "updateUIView" function? – User_404 Oct 10 '20 at 21:15
  • 1
    Hi @User_404, That difference matters, doesn't it? `makeUIView(...)` method creates the view object and configures its initial state. Then `updateUIView(...)` method just updates a stuff that was created earlier. – Andy Jazz Oct 11 '20 at 07:00
  • 1
    Hey Andy, your method worked, but I tried to load multiple .USDZ files and remove them with your method. this did not work. I changed the name of the Entity every time I pressed a button for a specific model to create a new Entity. But the removeAnchor function does not delete any of those Entities anymore. Any suggestions on this? – User_404 Oct 11 '20 at 15:26
  • 1
    You can name anchors or models using `.name` instance property. Then just find it using `arView.scene.findEntity(named: "NAME")` – Andy Jazz Oct 11 '20 at 15:28
  • 1
    Thx for the reply, Andy. I posted another question where I show my code so far. maybe u can have a look at this and tell me where my problem might be. I am very new to swift and don't understand everything that happens inside the code. – User_404 Oct 11 '20 at 15:47
  • 1
    I'll have a look tomorrow)) – Andy Jazz Oct 11 '20 at 15:49