1

当我点击我在 Reality Composer 中制作的 Reality 文件中的对象时,只需尝试打印一个 hello。无法设置通知和应用内操作之间的链接。

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {
        ZStack{
            ARViewContainer()
            Text("Level 1")
        }
    }
}

struct ARViewContainer : UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let yellowEntity = try! ModelEntity.load(named: "Yellow")
        let anchorEntity = AnchorEntity(plane: .horizontal)
        anchorEntity.addChild(yellowEntity)
        arView.scene.addAnchor(anchorEntity)
        yellowEntity.actions.Yellowtapped.onAction = handleTap(_:)
        func handleTap(_entity: Entity?){
            guard let entity = entity else {return}
            print("Hello")
        }
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {
    }
}
4

1 回答 1

0

如果您使用.rcproject所有作品完美:

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        let scene = try! Experience.loadBox()
        
        scene.actions.notifier.onAction = printer
        
        let anchor = AnchorEntity(plane: .horizontal)
        anchor.addChild(scene)
        arView.scene.addAnchor(anchor)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
    
    func printer(_ entity: Entity?) -> Void { print("Hello") }
}

在此处输入图像描述

附言

不要忘记将 2 个动作合并在一起(在 Reality Composer 中)。

于 2021-12-20T23:33:25.917 回答