'init(make:update:attachments:)' is unavailable in visionOS

229 views Asked by At

I am learning to use RealityView, and I will encounter an error when using attachments: 'init(make:update:attachments:)' is unavailable in visionOS

import SwiftUI
import RealityKit

struct MoonOrbit: View {
    var body: some View {
        RealityView{ content, attachments in
            async let earth = ModelEntity(named: "Earth")
            if let earth = try? await earth{
                content.add(earth)
            }
        } attachments: {
            Text("Earth")
                .tag("earth_label")
        }
    }
}

I checked the interface documentation and everything is fine.

enter image description here

It's frustrating and makes me unable to continue.

1

There are 1 answers

0
Nullable On

I found the answer here, but I really didn't notice any difference the first time

Old:

RealityView { content, attachments in
    content.add(Entity())
} attachments: {
    Text("Hello")
}

New:

RealityView { content, attachments in
    content.add(Entity())
} attachments: {
    Attachment(id: "h1") {
        Text("Hello")
    }
}

Finally, I found that in the attachments method, I also need to use Attachment to nest it.

enter image description here