【问题标题】:Setting SCNNode geometry property设置 SCNNode 几何属性
【发布时间】:2016-03-20 04:56:20
【问题描述】:

我有一个 SCNNode,我正在使用这种方法将 .dae 文件中的几何图形应用到它:

func addNode(gridPosition: SCNVector3, row: Int, gamePlaneHeight: Float, sceneArray: [ObjectInfo]) -> SCNNode {

    // Create a material using the model_texture.tga image
    let carMaterial = SCNMaterial()
    carMaterial.diffuse.contents = UIImage(named: "assets.scnassets/Textures/model_texture.tga")
    carMaterial.locksAmbientWithDiffuse = false

    // Create a clone of the Car node of the carScene - you need a clone because you need to add many cars
    var carNode: SCNNode!
    let randomNumb = AppDelegate().randRange(0, upper: sceneArray.count - 1)
    let selectedObject = sceneArray[randomNumb]
    carNode = selectedObject.objectscene.rootNode.childNodeWithName(selectedObject.objectname, recursively: false)!.clone() as SCNNode

    carNode.scale = SCNVector3(0.5, 0.5, AppDelegate().randomBetweenNumbers(0.8, secondNum: 2.2))
    carNode.name = selectedObject.objectname
    carNode.position = gridPosition
    carNode.position.y = carNode.position.y + gamePlaneHeight

    // Set the material
    carNode.geometry?.firstMaterial = carMaterial

    return carNode
}

该方法大部分时间都有效,但偶尔会在上线时崩溃

carNode.geometry?.firstMaterial = carMaterial

有错误

fatal error: unexpectedly found nil while unwrapping an Optional value
warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

我不明白为什么,因为大多数时候它都有效。

【问题讨论】:

    标签: ios scenekit


    【解决方案1】:

    carNode 是从 selectedObject.objectname 构建的。 如果场景中不存在selectedObject.objectname,则childNodeWithName 将返回nil。因为carNode 是一个隐式解包的可选项,Swift 会尝试解包nil 并崩溃。

    您应该将 carNode 设为可选 (var carNode: SCNNode?) 或添加诸如 if carNode == nil { ... } 之类的测试,并尝试调试您的输入 (selectedObject) 是什么以及为什么它不正确。

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 2018-12-29
      • 2021-08-20
      • 2017-07-22
      • 2022-11-02
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 2018-05-16
      相关资源
      最近更新 更多