Swift SCNNode 子类总是 hittest returns SCNNode *不是* 子类

Swift SCNNode subclass hittest always returns SCNNode *not* subclass

我有一个 SCNNode 的子class "ExSCNNode",可以向 SCNNode 添加更多属性和行为。

class ExSCNNode : SCNNode {
...
}

我用 ExSCNNode 构建了一个场景。

let testnode = ExSCNNode()

命中测试场景时:

// check what nodes are tapped
let p = gestureRecognize.location(in: scnView)
let hitResults = scnView.hitTest(p, options: [:])

// check that we clicked on at least one object
if hitResults.count > 0 {

for hit in hitResults {
let hitnode = hit.node
...

hitnode 是 SCNNode 而不是 ExSCNNode。 但我想让 ExSCNNode 访问高级功能。

如何访问 subclass 而不是 SCNNode class?

只需将对象转换为您的子类:

// check what nodes are tapped
let p = gestureRecognize.location(in: scnView)
let hitResults = scnView.hitTest(p, options: [:])

for hit in hitResults {
    if let hitnode = hit.node as? ExSCNNode {

        …
    }