Swift-SceneKit-无法从 '.scn' 加载 'art.scnassets' 文件

标签 swift scenekit scnscene

我正在尝试从“diceCollada.scn”文件创建一个新的 SCNScene。 但是这个文件不会被加载。 enter image description here

此文件位于“ARDicee/art.assets”文件夹中。

不仅是“diceCollada.scn”,它还无法加载默认的“ship.scn”。 我不知道为什么它不加载文件。


这是我的代码。


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. ---------- The error is here ---------------
        guard let diceScene = SCNScene(named: "art.scnassets/diceCollada.scn") else {
            fatalError()
        }
        // Setting node
        if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: true) {
            diceNode.position = SCNVector3(x: 0, y: 0, z: -0.1)
            sceneView.scene.rootNode.addChildNode(diceNode)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if ARWorldTrackingConfiguration.isSupported {
            
            // 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()
    }
}


Xcode - 版本 14.1

macOS Ventura - 版本 13.0.1

GitHub - This project


我还尝试以另一种方式创建 SCNScene

override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the view's delegate
        sceneView.delegate = self
        
        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true

        // --- Another way to create SCNScene ---
        let filePath = URL(fileURLWithPath: "/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn")
        do {
            let diceScene = try SCNScene(url: filePath)
            if let diceNode = diceScene.rootNode.childNode(withName: "Dice", recursively: true) {
                diceNode.position = SCNVector3(x: 0, y: 0, z: -0.1)
                sceneView.scene.rootNode.addChildNode(diceNode)
            }
        } catch {
            print(error)
        }
    }

但它给出了这个错误。

Error Domain=NSCocoaErrorDomain Code=260 "The file “diceCollada.scn” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn, NSUnderlyingError=0x282924570 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}


我正在尝试从“diceCollada.scn”文件创建一个新的SCNScene

最佳答案

如果应用无法从 Apple 模板加载宇宙飞船,则项目可能会以某种方式中断。尝试创建一个全新的默认 SceneKit/ARKit 项目,直接编译它并检查飞船是否正确加载。如果是,请将当前项目中的代码复制并粘贴到新项目中。如果宇宙飞船甚至没有从新模板加载,您的 xCode 安装可能会被破坏。您还可以清理项目构建文件夹或删除派生数据,这里有关于如何执行此类操作的文章。此外,您可以在 StackOverflow 上分享您的项目,以便我们查看。

关于Swift-SceneKit-无法从 '.scn' 加载 'art.scnassets' 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74653849/

相关文章:

ios - UISlider 设置值

swift - 如何在 TextView 中停止计时器?

image - 将图像转换为数据,反之亦然

swift - Xcode 11.3.1中SceneKit中的粒子系统文件在哪里

ios - SceneKit - 凹面碰撞盒

iOS Swift WKWebView 不缩放

swift - 使用 Swift SceneKit 的享元设计模式。对象重用

ios - 如何定位一个SCNNode来覆盖整个SCNView?

swift - 如何将 3D 模型添加到场景中,使其具有特定的 XYZ 位置?

ios - 将 .jpg 和 .mtl 文件应用于 SceneKit 中的 .obj 文件