一、效果展示:
二、程序代码(您需要创建两个文件)
【文件1】(MyApp):
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
Reality()
}
}
}
【文件2】(Reality):
import SwiftUI
import RealityKit
struct Reality: View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero, cameraMode: .ar, automaticallyConfigureSession: true)
let anchorEntity = AnchorEntity(plane: .horizontal)//水平类场景
guard let ARentity = try? ModelEntity.load(named: "U3aQM13") else {
fatalError("U3aQM13 model is not!")
}//导入reality/usdz文件
anchorEntity.addChild(ARentity)//锚点
arView.scene.anchors.append(anchorEntity)//场景
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
}
}
备注:制作一个 reality 或 usdz 文件,将文件导入程序。
目录:RealityKit AR教程