簡體   English   中英

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

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

我聽說 Xcode 12(現在是 Beta 6),Swift 包管理器現在能夠包含資源。 但我無法打開現實(.rcproject)文件。

這是我嘗試過的; (你可以復制)

  1. 我創建了一個新的Augmented Reality App項目。 (RealityKit + SwiftUI + Swift)
  2. 現在,如果您嘗試運行該項目,一切正常,您會看到一個默認的金屬框。
  3. 現在我創建了一個新的SPM (Swift 包管理器)
  4. 現在,我將本地創建的SPM拖到項目中,並將其添加到“常規”>“目標”選項卡中的框架中。 (通知項目有關本地添加的 spm)
  5. 我將Experience.rcproject & ContentView (也復制了自動生成的Experience枚舉,您可以通過 Cmd+Click 訪問它)到SPM
  6. 修復了ContentView一些訪問初始化問題並添加了平台支持platforms: [.iOS(.v13)],SPM
  7. SPM為存在路徑Experience.rcproject添加了resources

完成這些步驟后,我希望有一個包含 AR 的 swift 包管理器。
但是自動生成的Experience枚舉會拋出.fileNotFound("Experience.reality")錯誤。
好像還是沒能在 Bundle 中找到真人檔案?

你有沒有嘗試過類似的東西。 等待任何幫助。 謝謝..


文件夾結構

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")
        }

    }

}

ContentView文件中,我簡單地顯示EKARView

Xcode 知道如何將.rcproject文件處理為構建應用程序時所需的.reality文件。 不幸的是,在使用 Swift 包訪問項目文件時沒有完成此處理。

您概述的步驟幾乎可以奏效。 歸結為使用已編譯的.reality文件代替.rcproject文件。 為了轉換默認的.rcproject文件,您需要使用 Apple 的Reality Composer應用程序。

使用 Xcode 12 概述的步驟...

  • 選擇Experience.rcproject文件后,單擊“在 Reality Composer 中打開”按鈕。
  • 打開后,通過“文件”菜單導出項目。
  • 選擇“項目”並單擊“導出”。 這會生成一個Experience.reality文件。
  • 將該文件放在您的 swift 包資源中。
  • 確保將Experience.swift文件中的Bundle引用替換為Bundle.module ,因為現有引用將針對您的應用程序包。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM