Let's say I've created a scene with 3 models inside side by side. Now upon user interaction, I'd like to change these models to another model (that is also in the same reality composer pro project). Is that possible? How can one do that?
One way I can think of is to just load all the individual models in RealityView and then just toggle the opacity to show/hide the models. But this doesn't seem like the right way for performance/memory reasons.
How do you swap in and out usdz models?
Right now I load a scene using this code:
struct VolumeView: View {
@State private var show = false
@State var car: Entity?
@State var plane: Entity?
var body: some View {
VStack {
Toggle("Show/Hide", isOn: $show)
.onChange(of: show) { _, newValue in
if newValue {
} else {
}
}.padding()
RealityView { content in
await plane = try? Entity(named: "Plane",
in: realityKitContentBundle)
await car = try? Entity(named: "Car",
in: realityKitContentBundle)
if let plane = plane, let car = car {
content.add(car)
content.add(plane)
}
} update: { content in
if show {
/// show new model somehow
} else {
/// revert to old model someohow
}
}
}
}
}
You haven't provided much information about behavior but
updatewould get called each timeshowis changed so you would just have toremoveFromParentandaddChildorcontent.removeandcontent.add.Which is right depends on if you want to swap children from the parent or if you want to work with
contentdirectly.Here is some swapping logic.
And the
contentapproach is as follows.You can also create any combination of these.