在 Xcode Scene Editor 里添加 SCNReferenceNode 效果很好。但如果用下面的代码添加则可能没有效果:
if let filePath = NSBundle.mainBundle().pathForResource("RefTest", ofType: "scn", inDirectory: "TestScene.scnassets") {
let referenceURL = NSURL(fileURLWithPath: filePath)
if #available(iOS 9.0, *) {
let referenceNode = SCNReferenceNode(URL: referenceURL)
scene!.rootNode.addChildNode(referenceNode!)
print(referenceNode)
}
}
苹果的文档虽然说 SCNReferenceNode 的加载策略默认是立即加载:
When the reference node loads its content (either automatically, according to the loadingPolicy property, or when you call the load method), SceneKit loads the referenced scene file. All children of the scene file’s root node become children of the reference node.
但是,好像实际上并不是如此。必须要如下调用 load 函数:
let referenceNode = SCNReferenceNode(URL: referenceURL
referenceNode.load()
当然,也可以编辑 loadingPolicy 属性实现同样的效果,但调用 load 函数就已经足够了。