问题
Adding a SCNReferenceNode works when done from the Xcode Scene Editor. When done programmatically with the code below, however, nothing appears in the scene.
filePath
contains a valid path as proven by the print
statement as well as stepping through the code.
Help?
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)
}
}
回答1:
The class docs claim that the loading policy instructs the SCNReferenceNode
to load immediately by default:
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.
However, this doesn't seem to happen by default. You must invoke the load
function like so:
let referenceNode = SCNReferenceNode(URL: referenceURL
referenceNode.load()
Alternatively, perhaps you could play with the loadingPolicy
property as well, but invoking the load
function definitely works.
来源:https://stackoverflow.com/questions/39029865/scenekit-cannot-add-scnreferencenode-programmatically-though-works-in-xcode-sce