SceneKit: cannot add SCNReferenceNode programmatically though works in Xcode Scene Editor

别来无恙 提交于 2019-12-23 03:02:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!