所以我几天前问了一个问题,哪个安迪爵士好心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 中是如何应用的.下面的图片是我在控制台上打印的工作场景.

enter image description here

下面是我制作的一个示例,其中我使用了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

这就是我最终得到的,起作用的,也就是说,它显示了所有的物体和整个东西被拖在一起.[见下面的图片,后跟代码]

enter image description here

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    
        }            
    }
}

推荐答案

遍历子实体( node )

根据图表,你们的型号分别叫greenCuberedBoardblueCube.因此,从Reality Composer的场景层次 struct 中获取任何ModelEntity的最简单方法是应用findEntity(named:)方法,然后访问其子对象.

let model = cubesAnchor.findEntity(named: "greenCube")?.children[0] as! ModelEntity

...或者,多亏了现实composer 的命名方案:

let model = cubesAnchor.greenCube?.children[0] as! ModelEntity

名称greenCuberedBoardblueCube是实体的名称.这三款车的名字都是simpBld_root.因此,如果愿意,您可以重命名每个ModelEntity.

model.name = "Green_Cube_Model"

print(cubesAnchor.findEntity(named: "greenCube")!)

结果出现在Xcode的控制台中:

▿ 'greenCube' : Entity, children: 1
  ⟐ Transform
  ⟐ SynchronizationComponent
  ▿ 'Green_Cube_Model' : ModelEntity         // New name
    ⟐ PhysicsBodyComponent
    ⟐ Transform
    ⟐ SynchronizationComponent
    ⟐ CollisionComponent
    ⟐ ModelComponent

现在,您可以通过其唯一名称获取每个ModelEntity:

let green = cubesAnchor.findEntity(named: "Green_Cube_Model") as! ModelEntity

或者你也可以通过chain of children美元.


以下是我的代码:

import UIKit
import RealityKit
    
class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let scene = try! Experience.loadTwoBoxes()
        
        // MODEL 001 
        let model001 = scene.findEntity(named: "redCube")?.children[0] as! ModelEntity & HasPhysicsBody
        model001.position.y = 1.0
        model001.physicsBody = .init()
        model001.generateCollisionShapes(recursive: false)
        
        // MODEL 002       
        let forRenaming: Entity = scene.findEntity(named: "blueCube")!
        forRenaming.children[0].name = "Blue_Cube_Model"
        print(forRenaming)
    
        let model002 = scene.findEntity(named: "Blue_Cube_Model") as! ModelEntity & HasPhysicsBody
        model002.position.y = 1.0
        model002.physicsBody = .init()
        model002.generateCollisionShapes(recursive: false)

        arView.scene.anchors.append(scene)
    }
}

Swift相关问答推荐

通过withstickedContinuation传递通用类型T

防止NavigationLink自行返回导航视图

如何将EnvironmentObject从UIKit视图控制器传递到SwiftUI视图

SwiftUI - NavigationLink(value:)和navigationDestination不工作

为什么我不能在这个 Swift 间接枚举中返回 self ?

将变量设置为 @Published 会 destruct 代码而不会出现任何错误

允许视图在视图内更改

应该在ViewModel还是ViewController中使用"Task {}"?

是否可以利用 Codable 从 Dictionary 初始化符合类型

当变量首次用于其自己的初始化时,如何清除变量...在初始化之前使用错误?

在 Swift 中为(递归)扩展提供默认实例参数

故事板未在助手中显示视图控制器

如何在 SwiftUI 中通过按钮点击一次为一个更改设置动画?

在 Swift 中,如何查看 Process() 传递给 shell 的字符串?

符合协议要求委托变量在 ios13 中可用

无法使用锚激活约束

如何快速判断设备方向是横向还是横向?

EXC_BREAKPOINT 崩溃的原因范围

如何使用 swift 在 tableview 中填充两个不同数组的两个部分?

ISO8601DateFormatter 不解析 ISO 日期字符串