RealityKit – 什么是 `steelBox` 实例加载?
RealityKit – What is `steelBox` instance loading?
当您使用标准 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 { [=10=] === 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
中名为“Steel Box”的立方体。但是如果我将这个字符串重命名为任何不在场景中的随机名称,多维数据集仍然可以正常加载,项目也是如此......并且在初始化期间调用此方法!
这是为什么?这到底在加载什么?
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])
这导致打印实体的层次结构:
我们可以从控制台看到 "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"
当您使用标准 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 { [=10=] === 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
中名为“Steel Box”的立方体。但是如果我将这个字符串重命名为任何不在场景中的随机名称,多维数据集仍然可以正常加载,项目也是如此......并且在初始化期间调用此方法!
这是为什么?这到底在加载什么?
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])
这导致打印实体的层次结构:
我们可以从控制台看到 "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"