ARKit 项目:在解包可选值时意外发现 nil

问题描述 投票:0回答:1

当我在 Xcode 上创建 ArKit 项目时,样板项目将无法运行并返回找不到 Ship.scn 资源的错误。

SceneKit 项目的样板代码:

    import UIKit
    import SceneKit
    import ARKit

class ViewController: UIViewController, ARSCNViewDelegate {

    @IBOutlet var sceneView: ARSCNView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        
        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true
        
        // Create a new scene
        let scene = SCNScene(named: "art.scnassets/ship.scn")!
        
        // Set the scene to the view
        sceneView.scene = scene
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // Create a session configuration
        let configuration = ARWorldTrackingConfiguration()

        // Run the view's session
        sceneView.session.run(configuration)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        
        // Pause the view's session
        sceneView.session.pause()
    }

    // MARK: - ARSCNViewDelegate
    
/*
    // Override to create and configure nodes for anchors added to the view's session.
    func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        let node = SCNNode()
     
        return node
    }
*/
    
    func session(_ session: ARSession, didFailWithError error: Error) {
        // Present an error message to the user
        
    }
    
    func sessionWasInterrupted(_ session: ARSession) {
        // Inform the user that the session has been interrupted, for example, by presenting an overlay
        
    }
    
    func sessionInterruptionEnded(_ session: ARSession) {
        // Reset tracking and/or remove existing anchors if consistent tracking is required
        
    }
}

错误:致命错误:解包可选值时意外发现 nil:文件

线程 1:致命错误:在展开可选值时意外发现 nil,显示在以下行中: 让场景= SCNScene(命名:“art.scnassets/ship.scn”)!

在更新 Xcode 之前,这对我有用(当前版本是 13.0,Mac OS Big Sur 版本 11.6)。

想知道为什么会发生这种情况,以及如何解决这个问题?

ios xcode scenekit arkit scnscene
1个回答
0
投票

我花了很多时间试图解决这个问题。这是一个奇怪的错误。但解决方案实际上非常简单,只需单击“art”文件夹并取消选中“首选压缩纹理”即可。 see the solution picture

您甚至可以在运行应用程序一次后再次打开它,它将完美运行。

© www.soinside.com 2019 - 2024. All rights reserved.