我正在try 从‘diceCollada.scn’文件创建一个新的SCNScene. But this file won't be loaded. enter image description here

该文件位于"ARDicee/art.sets"文件夹中.

不仅是"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"无法打开文件"diceCollada.scn",因为没有这样的文件."UserInfo={NSFilePath=/Applications/xcode/Development/ARDicee/ARDicee/art.scnassets/diceCollada.scn,NSUnderlyingError=0x282924570{错误域=NSPOSIX错误域代码=2"没有这样的文件或目录"}}


我正在try 从‘diceCollada.scn’文件创建一个新的SCNScene.

推荐答案

重置命令行工具的路径

在你的command line tools人中有一些问题.因此,art.scnassets文件夹中的内容不可读.为了解决这个问题,您需要为Xcode 14.1安装最新版本的Command_Line_Tools,然后在终端中执行以下命令:

sudo xcode-select --reset

sudo xcode-select -switch /Library/Developer/CommandLineTools

enter image description here

重新安装命令行工具

或者,您可以删除旧版本的command line tools,然后使用终端安装新的版本:

sudo rm -rf /Library/Developer/CommandLineTools

sudo xcode-select --install

然后重启你的Mac.

我也有同样的问题,这些步骤对我很有帮助.

重命名

如果上述步骤仍然不起作用,请将art.scnassets文件夹重命名为artisan.scnassets.

Swift相关问答推荐

当我点击模拟器中的文本字段时,UIKit CLLocationButton崩溃

如何在向照片添加文本时定义文本对齐、文本背景的Alpha和在文本之前添加图像

不使用`lockFocus`将两个重叠的NSImage转换为单个NSImage

background Task(.backgroundTask)不适用于MySQL

如何避免切换视图递归onChange调用SwiftUI

SwiftUI,如何更改具有多个按钮和一个布尔条件的一个按钮标签

可以将属性名称传递给 Swift 中的函数吗?

macOS SwiftUI: 如何触发删除一个项目?

如何在SwiftUI中更改NSTextView中的 Select colored颜色 ?

Swift Random Float Fatal Error:在无限范围内没有均匀分布

从文字创建数组时的 Swift 宏

如何让飞机与 RealityKit 中的物体相撞

在主要参与者中启动分离任务和调用非隔离函数之间的区别

将 struct 作为泛型类型传递并访问该泛型类型属性

iOS SwiftUI - 无法转换 ObservedObject 类型的值

macOS 守护进程应该由Command Line ToolXcode 模板制作吗?

Swift - 获取本地日期和时间

如何使用 Swift/iOS 为人类书写的笔画制作动画?

在 Swift for iOS 中沿圆形路径绘制文本

TabView 在切换选项卡时重置导航堆栈