首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在RealityKit中创建

在RealityKit中创建
EN

Stack Overflow用户
提问于 2022-07-21 18:53:16
回答 1查看 84关注 0票数 2

我想在一个简单的swiftUI页面和AR相机ContentView之间添加一个Reality Kit项目中的segue。这是我的SegueView,有一个按钮的迅捷页面,我想从AR视图中分离出来。

代码语言:javascript
复制
struct SegueView: View {
    @State private var isActive: Bool = false
    var body: some View {
        NavigationLink(destination: ContentView(rootActive: $isActive), isActive: $isActive, label: {
            Text("See in AR")
        })
    }
}

下面是来自我的ContentView的代码,它只呈现一个框:

代码语言:javascript
复制
struct ContentView : View {
    @Binding var rootActive: Bool
    var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        // Load the "Box" scene from the "Experience" Reality File
        let anchor = AnchorEntity(plane: .horizontal)
        let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .blue, isMetallic: true)])
        
        anchor.addChild(box)
        // Add the box anchor to the scene
        arView.scene.anchors.append(anchor)
        
        return arView
        
    }
    func updateUIView(_ uiView: ARView, context: Context) {} 
}

我按照教程创建了segue,但是它不包含AR视图,我也找不到这样的教程。我还必须在这里添加应用程序委托中的rootActive变量:

代码语言:javascript
复制
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView(rootActive: .constant(true))

        // Use a UIHostingController as window root view controller.
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
        return true
    }

但是由于本教程没有这一步,所以我不完全确定该做什么。当我运行这个应用程序时,只有ContentView会被呈现。有人知道我如何创建一个SegueView首先呈现的segue,然后当导航链接"see“被按下时,它会转到ContentView上吗?

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-21 21:17:22

在ContentView中,将NavigationLink放在NavigationView中。

代码语言:javascript
复制
import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {           
        NavigationView {
            ZStack {
                Color.black
                
                VStack {    
                    NavigationLink(destination: Reality().ignoresSafeArea()) {
                        Text("Go to ARView")
                            .foregroundColor(.cyan)
                            .font(.title3)
                    }                   
                }
            }.ignoresSafeArea()
        }
    }
}

ARView

代码语言:javascript
复制
struct Reality: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let model = ModelEntity(mesh: .generateSphere(radius: 0.2))
        let anchor = AnchorEntity(plane: .horizontal)
        anchor.addChild(model)
        arView.scene.anchors.append(anchor)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
}

P. S.

在项目的Info选项卡中添加键Privacy - Camera Usage Description

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73071348

复制
相关文章

相似问题

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