简体   繁体   English

如何在 Swift 包管理器中添加 Reality 文件?

[英]How to add Reality file in a Swift Package Manager?

I've heard with the Xcode 12 (now in Beta 6), Swift package manager is now able to include resources.我听说 Xcode 12(现在是 Beta 6),Swift 包管理器现在能够包含资源。 But I am not able to open a reality (.rcproject) file.但我无法打开现实(.rcproject)文件。

Here is what I have tried;这是我尝试过的; (& you can reproduce) (你可以复制)

  1. I created a new Augmented Reality App project.我创建了一个新的Augmented Reality App项目。 (RealityKit + SwiftUI + Swift) (RealityKit + SwiftUI + Swift)
  2. Now if you try to run the project, everything works, you see a default metallic box.现在,如果您尝试运行该项目,一切正常,您会看到一个默认的金属框。
  3. Now I created a new SPM (Swift package manager)现在我创建了一个新的SPM (Swift 包管理器)
  4. Now I dragged locally created SPM to the project and added it to frameworks in General > Targets tab.现在,我将本地创建的SPM拖到项目中,并将其添加到“常规”>“目标”选项卡中的框架中。 (To inform the project about locally added spm) (通知项目有关本地添加的 spm)
  5. I dragged Experience.rcproject & ContentView (also copied the autogenerated Experience enum, you can reach it via Cmd+Click) to SPM我将Experience.rcproject & ContentView (也复制了自动生成的Experience枚举,您可以通过 Cmd+Click 访问它)到SPM
  6. Fixed some access initializer issue for ContentView & added platform support platforms: [.iOS(.v13)], in the SPM修复了ContentView一些访问初始化问题并添加了平台支持platforms: [.iOS(.v13)],SPM
  7. Added resources in the SPM for the path Experience.rcproject existSPM为存在路径Experience.rcproject添加了resources

After those steps finished I'd except to have an AR included swift package manager.完成这些步骤后,我希望有一个包含 AR 的 swift 包管理器。
But auto generated Experience enum throws .fileNotFound("Experience.reality") error.但是自动生成的Experience枚举会抛出.fileNotFound("Experience.reality")错误。
Seems still not able to find reality file in Bundle?好像还是没能在 Bundle 中找到真人档案?

Have you tried something similar.你有没有尝试过类似的东西。 Waiting any helps.等待任何帮助。 Thanks..谢谢..


文件夹结构

Package.swift

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "ARSPM",
    platforms: [.iOS(.v13)],
    products: [
        .library(
            name: "ARSPM",
            targets: ["ARSPM"]),
    ],
    dependencies: [],
    targets: [
        .target(
            name: "ARSPM",
            dependencies: [], resources: [
                .copy("Resources")
            ]),
        .testTarget(
            name: "ARSPMTests",
            dependencies: ["ARSPM"]),
    ]
)

ARView.swift

import SwiftUI
import RealityKit

public struct EKARView : View {
    public init() { }
    public var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

public struct ARViewContainer: UIViewRepresentable {
    
    public func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        // Load the "Box" scene from the "Experience" Reality File
        let boxAnchor = try! Experience.loadBox()
        
        // Add the box anchor to the scene
        arView.scene.anchors.append(boxAnchor)
        
        return arView
        
    }
    
    public func updateUIView(_ uiView: ARView, context: Context) {}
    
}

GeneratedExperienceFile.swift

//
// Experience.swift
// GENERATED CONTENT. DO NOT EDIT.
//

import Foundation
import RealityKit
import simd
import Combine

internal enum Experience {

    public enum LoadRealityFileError: Error {
        case fileNotFound(String)
    }

    private static var streams = [Combine.AnyCancellable]()

    public static func loadBox() throws -> Experience.Box {
        guard let realityFileURL =
//                Also tried >> Foundation.Bundle.module
                Foundation.Bundle(for: Experience.Box.self)
                    .url(forResource: "Experience", withExtension: "reality") else {
            throw Experience.LoadRealityFileError.fileNotFound("Experience.reality")
        }

        let realityFileSceneURL = realityFileURL.appendingPathComponent("Box", isDirectory: false)
        let anchorEntity = try Experience.Box.loadAnchor(contentsOf: realityFileSceneURL)
        return createBox(from: anchorEntity)
    }

    public static func loadBoxAsync(completion: @escaping (Swift.Result<Experience.Box, Swift.Error>) -> Void) {
        guard let realityFileURL = Foundation.Bundle(for: Experience.Box.self).url(forResource: "Experience", withExtension: "reality") else {
            completion(.failure(Experience.LoadRealityFileError.fileNotFound("Experience.reality")))
            return
        }

        var cancellable: Combine.AnyCancellable?
        let realityFileSceneURL = realityFileURL.appendingPathComponent("Box", isDirectory: false)
        let loadRequest = Experience.Box.loadAnchorAsync(contentsOf: realityFileSceneURL)
        cancellable = loadRequest.sink(receiveCompletion: { loadCompletion in
            if case let .failure(error) = loadCompletion {
                completion(.failure(error))
            }
            streams.removeAll { $0 === cancellable }
        }, receiveValue: { entity in
            completion(.success(Experience.createBox(from: entity)))
        })
        cancellable?.store(in: &streams)
    }

    private static func createBox(from anchorEntity: RealityKit.AnchorEntity) -> Experience.Box {
        let box = Experience.Box()
        box.anchoring = anchorEntity.anchoring
        box.addChild(anchorEntity)
        return box
    }

    public class Box: RealityKit.Entity, RealityKit.HasAnchoring {

        public var steelBox: RealityKit.Entity? {
            return self.findEntity(named: "Steel Box")
        }

    }

}

And in ContentView file, I simple show EKARView .ContentView文件中,我简单地显示EKARView

Xcode knows how to process a .rcproject file into the .reality file it needs when building an application. Xcode 知道如何将.rcproject文件处理为构建应用程序时所需的.reality文件。 Unfortunately this processing isn't done when accessing the project file using a Swift Package.不幸的是,在使用 Swift 包访问项目文件时没有完成此处理。

The steps you've outlined will almost work.您概述的步骤几乎可以奏效。 What it comes down to is using the already compiled .reality file in place of the .rcproject file.归结为使用已编译的.reality文件代替.rcproject文件。 In order to transform the default .rcproject file, you'll need to use Apple's Reality Composer application.为了转换默认的.rcproject文件,您需要使用 Apple 的Reality Composer应用程序。

steps outlined using Xcode 12...使用 Xcode 12 概述的步骤...

  • With the Experience.rcproject file selected, click the 'Open in Reality Composer' button.选择Experience.rcproject文件后,单击“在 Reality Composer 中打开”按钮。
  • Once open, export the project through the 'File' menu.打开后,通过“文件”菜单导出项目。
  • Choose 'Project' and click export.选择“项目”并单击“导出”。 This produces a Experience.reality file.这会生成一个Experience.reality文件。
  • Place that file in your swift package resources.将该文件放在您的 swift 包资源中。
  • Make sure replace the Bundle references in your Experience.swift file with Bundle.module , as the existing reference will target your application bundle.确保将Experience.swift文件中的Bundle引用替换为Bundle.module ,因为现有引用将针对您的应用程序包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Swift package 管理器添加依赖项 - How to add dependency using Swift package manager 如何在 Swift 包管理器中添加二进制框架作为依赖项? - How to add Binary framework in Swift Package Manager as dependency? 如何在 xcode 13 中添加 Swift package 管理器作为动态库? - How to add a Swift package manager as a dynamic library in xcode 13? Swift Package 管理器 - 桥接 Header,找不到文件 - Swift Package Manager - Bridging Header, File Not Found 使用 Swift Package 管理器为二进制目标添加 package 依赖项 - Add package dependency for a binary target with Swift Package Manager 如何更改 Swift Package 管理器中的库版本? - How to change the version of library in Swift Package Manager? Swift Package 管理器 - 将 package 依赖项添加到您的 Package.swift 以进行本机反应 - Swift Package Manager - add the package dependency to your Package.swift for react native 使用 Swift Package Manager 时如何导入包的依赖? - How to import a package's dependency when using Swift Package Manager? 如何使用 Swift 包管理器从现有项目创建包? - How to create package from existing project with Swift Package Manager? 在 Swift 包管理器中找不到“framework/header1.h”文件 - 'framework/header1.h' file not found in Swift Package Manager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM