首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导入和显示.usdz文件

导入和显示.usdz文件
EN

Stack Overflow用户
提问于 2022-03-09 22:25:47
回答 1查看 294关注 0票数 0

我尝试将一些.usdz文件导入到应用程序中,并使用SceneView显示3D模型。当我在我的Xcode文件夹中显示一些模型时,它可以工作。但是当在我的手机上运行这个应用程序并尝试从我的手机导入文件时,打印(FileName)仍然可以获得正确的文件名,但是在SceneView中没有显示任何内容.有人能帮忙吗?

代码语言:javascript
复制
@State var fileName = ""
@State var openFile = false
@State var model = Model(id: 0, modelName: "")

var body: some View {
    VStack{
        VStack {
            Text(fileName)
                .fontWeight(.bold)

            Button {
                openFile.toggle()
            } label: {
                Text("Come On")
            }
        }
        .fileImporter(isPresented: $openFile, allowedContentTypes: [.usdz]) {  result in
            switch result {
            case .success(let url):

                _ = url.startAccessingSecurityScopedResource()
                print(url)
                self.fileName = url.lastPathComponent
                print(fileName)
                model.modelName = fileName

            case.failure(let error):
                print(error)
            }
        }
        SceneView(scene: SCNScene(named: model.modelName) , options: [.autoenablesDefaultLighting,.allowsCameraControl])
            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height / 2)
EN

回答 1

Stack Overflow用户

发布于 2022-09-29 14:06:23

SCNScene(named: model.modelName)更改为let scene = try? SCNScene(url: url!)

示例

代码语言:javascript
复制
struct Viewer3D: View {

@State var urlLocalModel: URL?
@State var openFile = false

var body: some View {
    VStack{
        VStack {
            Button {
                openFile.toggle()
            } label: {
                Text("Open File Local USDZ")
            }
        }
        .fileImporter(isPresented: $openFile, allowedContentTypes: [.usdz]) {  result in
            switch result {
            case .success(let url):
                _ = url.startAccessingSecurityScopedResource()
                self.urlLocalModel = url
                print(url)
            case.failure(let error):
                print(error)
            }
        }
        if (urlLocalModel != nil) {
            let scene = try? SCNScene(url: urlLocalModel!)
            SceneView(scene: scene, options: [.autoenablesDefaultLighting,.allowsCameraControl])
                .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.width)
        }
    }
}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71416712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档