所以我几天前问了一个问题,哪个安迪爵士好心answered分.所以我一直在玩弄答案,虽然我能够实现一个解决方案,但它是不完整的,因为他使用的方法是使用RealityKit的findEntity(named:)
.而我用现实composer 来回答我自己的问题.
Problem #1: 100 method doesn't work on Reality Composer when you have multiple objects.所以我意识到使用findEntity(名:)方法不起作用.因为场景中的所有对象都被命名为"simpBld_root"
,所以无法触发我想要触发的内容.
Problem #2: When using the parenting notation, I am doing a trial and error because I don't really understand what is what.个 我如何调用每个对象?这也是我自己不能正确理解从现实composer 那里调用什么的结果,因为我不认为我真的理解父母符号在现实composer 中是如何应用的.下面的图片是我在控制台上打印的工作场景.
下面是我制作的一个示例,其中我使用了Child[0],而不是Andy的findEntity解决方案(名为:)
以下是我在使用findEntity()方法时try 的代码,但该方法不起作用:
let redBoardModelEntity = cubesAnchor.redBoard?.findEntity(named: "simpBld_root") as! ModelEntity
let greenCubeModelEntity = cubesAnchor.greenCube?.findEntity(named: "simpBld_root") as! ModelEntity
let blueCubeModelEntity = cubesAnchor.blueCube?.findEntity(named: "simpBld_root") as! ModelEntity
这就是我最终得到的,起作用的,也就是说,它显示了所有的物体和整个东西被拖在一起.[见下面的图片,后跟代码]
import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
let cubesAnchor = try! Experience.loadCubes()
arView.scene.anchors.append(cubesAnchor)
print (cubesAnchor)
cubesAnchor.actions.behavior.onAction = handleTapOnEntity(_:)
let redModel = cubesAnchor.children[0].children[0]
let group = ModelEntity() as ModelEntity & HasCollision
group.addChild(redModel)
// group.addChild(blueCubeModelEntity)
// group.addChild(greenCubeModelEntity)
group.generateCollisionShapes(recursive: false)
self.arView.installGestures(.all, for: group)
let shape = ShapeResource.generateBox(width: 1, height: 1, depth: 1)
let collision = CollisionComponent(shapes: [shape],
mode: .trigger,
filter: .sensor)
group.components.set(collision)
let anchor = AnchorEntity()
anchor.addChild(group)
anchor.scale = [5,5,5]
arView.scene.anchors.append(anchor)
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0){
// self.arView.scene.anchors.removeAll()
let coneAnchor = try! Experience.loadCone()
self.arView.scene.anchors.append(coneAnchor)
coneAnchor.actions.behavior.onAction = handleTapOnEntity(_:)
// print(cubesAnchor)
}
boxAnchor.actions.behavior.onAction = handleTapOnEntity(_:)
func handleTapOnEntity(_ entity: Entity?){
guard let entity = entity else { return }
// self.myEntity = entity
// self.myEntity?.isEnabled = false
}
}
}