[英]SceneKit - Loading a DAE into a SCNNode (unwrap error)
I'm currently trying to import a dae file into a SCNNode that will then be added to a scene. 我目前正在尝试将dae文件导入SCNNode,然后将其添加到场景中。 I found some great stuff on here but I've hit a wall.
我在这里发现了一些很棒的东西,但是我碰壁了。
The answer I've been trying to implement was found here - Stackoverflow: load a collada (dae) file into SCNNode (Swift - SceneKit) 我一直在尝试实现的答案在这里-Stackoverflow:将collada(dae)文件加载到SCNNode(Swift-SceneKit)
I've tried to implement the top solution but I get an error which says: 我尝试实现最佳解决方案,但收到一条错误消息:
"Value of optional type 'SCNNode?'
“可选类型'SCNNode的值? not unwrapped; did you mean to use '!'
没有包装;您的意思是使用'!' or '?'
要么 '?'
It may be that I've overlooked something very fundamental, I am very much a novice. 可能是我忽略了一些非常基本的内容,我非常新手。
I'll include my ViewController viewDidload code below, if anyone could shed some light on this I would be infinitely grateful! 我将在下面包含我的ViewController viewDidload代码,如果有人可以对此有所了解,我将万分感谢!
let scnView = self.view as SCNView
let scene = MasterScene()
scnView.scene = scene
scnView.backgroundColor = UIColor.grayColor()
// enable default lighting
scnView.autoenablesDefaultLighting = true
// enable default camera
scnView.allowsCameraControl = true
var node = SCNNode()
let assetScene = SCNScene(named: "s185.dae")
scene.rootNode.addChildNode(assetScene?.rootNode.childNodeWithName("s185", recursively: true))
// Last line produces error
Your assetScene
is an optional. 您的
assetScene
是可选的。 You seem to have got that part, since you're using optional chaining to access its rootNode
. 您似乎已经掌握了这一部分,因为您正在使用可选链访问其
rootNode
。
However, any result produced by an expression that uses optional chaining is itself an optional — you don't know that assetScene
exists, so you don't know if anything you tried to get out of it exists, either. 但是,使用可选链接的表达式产生的任何结果本身就是可选的-您不知道
assetScene
存在,因此也不知道是否试图摆脱它。
But addChildNode
takes a non-optional SCNNode
— you can't pass an optional to it. 但是
addChildNode
接受一个非可选的SCNNode
-您不能SCNNode
传递可选的。 You need to unwrap that optional first. 您需要先解开该可选项。 Even if you unwrap
assetScene
with an if let
, you'll still get an optional, because you don't know if childNodeWithName
found a node. 即使使用
if let
打开assetScene
,您仍然会得到一个可选的,因为您不知道childNodeWithName
找到了一个节点。
Try something like this: 尝试这样的事情:
if let assetScene = SCNScene(named: "s185.dae") {
if let node = assetScene.rootNode.childNodeWithName("s185", recursively: true) {
scene.rootNode.addChildNode(node)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.