【问题标题】:How to use RealityKit's image AnchorEntity in SwiftUI?如何在 SwiftUI 中使用 RealityKit 的图像 AnchorEntity?
【发布时间】:2022-05-22 12:11:43
【问题描述】:

我想使用使用 Image 类型的锚定组件目标初始化的 RealityKit 的 AnchorEntity。这是我想要做的:

let anchorEntity = AnchorEntity(.image(group: "AR Resources", 
                                        name: "imgAnchor"))

我发现关于如何使用它的文档为零。

有人知道正确的做法吗?

【问题讨论】:

    标签: swift swiftui augmented-reality realitykit


    【解决方案1】:

    您可以在 SwiftUI 中使用 AnchorEntity(.image(...)),就像在 UIKit 中使用它一样。首先在 Project Navigator 窗格中单击 Assets.xcassets 并创建 AR Resources 文件夹。将您的图像拖到那里。在 Inspector 中设置其物理尺寸。然后复制/粘贴代码:

    import SwiftUI
    import RealityKit
    
    struct ARViewContainer: UIViewRepresentable {
        
        func makeUIView(context: Context) -> ARView {
            
            let arView = ARView(frame: .zero)
            let entity = ModelEntity(mesh: .generateBox(size: 0.1))
            let anchor = AnchorEntity(.image(group: "AR Resources", 
                                              name: "image.png"))
            entity.setParent(anchor)
            arView.scene.anchors.append(anchor)
            return arView
        }
        func updateUIView(_ uiView: ARView, context: Context) { }
    }
    
    struct ContentView: View {
        var body: some View {
            ARViewContainer().edgesIgnoringSafeArea(.all)
        }
    }
    

    【讨论】:

    • 通过右键单击“AR and Textures”->“New AR Resource Group”下的Assets.xcassets创建AR Resources文件夹。我没能很快找到它。 (Xcode 13.3。)
    • 在模拟器上构建它会给出以下(误导性错误): type 'AnchoringComponent.Target' has no member 'image' 文档没有显示:AnchorEntity(.image...) 我有在设备上构建它以使其工作。 (Xcode 13.3。)
    • @Darkwonder,模拟器无法激活图像锚。在这种情况下,您只需要使用 REAL 设备。
    • 我知道,只是我正在阅读错误,无法弄清楚为什么项目无法构建。我已将我的 iPhone 与 mac 断开连接,但没有看到模拟器是自动选择的。一直在谷歌搜索问题,而不仅仅是切换到设备。你之前在某个地方写过这个,这为我节省了很多时间而没有看到如此愚蠢的错误消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2020-09-21
    • 2020-06-27
    • 2021-05-30
    • 2023-03-26
    • 2022-01-16
    相关资源
    最近更新 更多