1

这是苹果文档:

1.在不生成代码的情况下手动加载 Reality Composer 文件

我使用了“从 URL 异步加载场景”代码:

 func loadRealityComposerSceneAsync (filename: String,
                                     fileExtension: String,
                                     sceneName: String,
                                     completion: @escaping (Swift.Result<(Entity & HasAnchoring)?, Swift.Error>) -> Void) {
     
     guard let realityFileSceneURL = createRealityURL(filename: filename, fileExtension: fileExtension, sceneName: sceneName) else {
         print("Error: Unable to find specified file in application bundle")
         return
     }
     
     let loadRequest = Entity.loadAnchorAsync(contentsOf: realityFileSceneURL)
     
     let cancellable = loadRequest.sink(receiveCompletion: { (loadCompletion) in
         if case let .failure(error) = loadCompletion {
             completion(.failure(error))
         }
     }, receiveValue: { (entity) in
         //  entity -> AnchorEntity
         completion(.success(entity))
     })
     cancellable.store(in: &streams)
 }

在 viewDidLoad :

  loadRealityComposerSceneAsync(filename: "TestScene", fileExtension: "reality", sceneName: "space") { result in
            switch result {
            case .success(let anchor):
                print("space is \(anchor)\n")
                // anchor -> (Entity & HasAnchoring)?
                // How to show the TestScene file on the self.arView.scene

                break
             
            case .failure(let error):
                print(error.localizedDescription)
                break
            }
        }

打印空间是:

Optional(▿ '' : AnchorEntity, children: 1
  ⟐ SynchronizationComponent
  ⟐ Transform
  ⟐ AnchoringComponent
  ▿ '' : Entity, children: 3
    ⟐ SynchronizationComponent
    ⟐ Transform
    ▿ 'boxs' : Entity, children: 1
      ⟐ SynchronizationComponent
      ⟐ Transform
      ▿ 'simpBld_root' : ModelEntity
        ⟐ ModelComponent
        ⟐ SynchronizationComponent
        ⟐ Transform
        ⟐ CollisionComponent
    ▿ '8C519DF7-2575-4AEE-A729-57F7CE0EFB41' : Entity, children: 1
      ⟐ SynchronizationComponent
      ⟐ Transform
      ▿ 'simpBld_root' : ModelEntity
        ⟐ ModelComponent
        ⟐ SynchronizationComponent
        ⟐ Transform
    ▿ 'star' : Entity, children: 1
      ⟐ SynchronizationComponent
      ⟐ Transform
      ▿ 'simpBld_root' : ModelEntity
        ⟐ ModelComponent
        ⟐ SynchronizationComponent
        ⟐ Transform
)

所以我的问题是:如何在 self.arView.scene 上显示我的 TestScene.reality 文件。谢谢!!!

4

0 回答 0