2

当您使用标准 Xcode 模板创建增强现实项目时,Xcode 会将此swift文件添加到您的项目中:

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

import Foundation
import RealityKit
import simd
import Combine

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

    }

}

让我们关注代码的最后一部分

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

这部分显然是在加载立方体,从标准中命名为“钢盒” Experience.rcproject。但是如果我将此字符串重命名为不在场景中的任何随机名称,立方体仍然可以正常加载,项目也是如此......并且在初始化期间调用此方法!

这是为什么?这真正加载的是什么?

4

1 回答 1

4

文件中生成的内容Experience.swift直接链接到来自 Reality Composer 的场景。steelBox属性是在 RC 场景中建模实体的“关键”。它是为了方便开发人员快速访问所需模型而设计的。

该字符串"Steel Box"不是强制性的,它是可选的。您可以随意命名您的模型。但是,您甚至可以将此字符串字段保留为空(在这种情况下,Xcode 会为其分配一长串字母和数字,或者只是一个empty string而不是修改后的名称"Steel Box"- 并且不会steelBox为您分配任何属性)。

"Steel Box" name was automatically used to create `steelBox` property

因此,例如,如果您在 Reality Composer 场景中命名了一个模型"Plastic Sphere",它会自动创建一个名为的变量plasticSphere

public var plasticSphere: Entity? { get }


在 RealityKit 中,您可以通过以下方式访问您的多维数据集实体:

 let boxAnchor = try! Experience.loadBox()
 arView.scene.anchors.append(boxAnchor)

 print(boxAnchor.steelBox!)

或为其层次结构使用下标:

 let boxAnchor = try! Experience.loadBox()
 arView.scene.anchors.append(boxAnchor)

 print(boxAnchor.children[0].children[0].children[0])

这导致打印实体的层次结构:

在此处输入图像描述

正如我们从 Console 中看到的,"Steel Box"它只是一个steelBox的实体名称。

print(boxAnchor.steelBox!.name as Any)

// "Steel Box"


但是,正如我之前所说,当您在 Reality Composer 中将此字段留为模型名称为空时...

在此处输入图像描述

...您无法通过场景中的属性名称检索实体。

您只能使用子层次结构检索它:

let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)

print(boxAnchor.children[0].children[0].children[0])

在此处输入图像描述

因此,正如我们所见,现在控制台中没有实体名称(字符串为空)。

但是您可以随时分配它:

boxAnchor.steelBox!.name = "My favorite box"
于 2020-06-24T06:12:19.893 回答