Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Last active August 2, 2024 21:32
Show Gist options
  • Select an option

  • Save drewolbrich/bf6fde46c4fcae01a856eff58fdc56a1 to your computer and use it in GitHub Desktop.

Select an option

Save drewolbrich/bf6fde46c4fcae01a856eff58fdc56a1 to your computer and use it in GitHub Desktop.
A RealityKit Entity extension that enumerates the entities in a hierarchy
//
// Entity+EnumerateHierarchy.swift
//
// Created by Drew Olbrich on 7/18/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import RealityKit
extension Entity {
/// Executes a closure for each of the entity's child and descendant entities, as
/// well as for the entity itself.
func enumerateHierarchy(body: (Entity) -> Void) {
enumerateHierarchy { entity, _ in
body(entity)
}
}
/// Asynchronously executes a closure for each of the entity's child and descendant
/// entities, as well as for the entity itself.
///
/// This method does not return until all asynchronous `body` closures have finished
/// executing.
///
/// If you see the compiler error "Ambiguous use of 'enumerateHierarchy(body:)'",
/// call `enumerateHierarchy` like this:
///
/// await rootEntity.enumerateHierarchy { entity async in
/// ...
/// }
func enumerateHierarchy(body: @escaping (Entity) async -> Void) async {
await withTaskGroup(of: Void.self) { taskGroup in
enumerateHierarchy { entity, _ in
taskGroup.addTask {
await body(entity)
}
}
}
}
/// Executes a closure for each of the entity's child and descendant entities, as
/// well as for the entity itself.
///
/// Set `stop` to true in the closure to abort further processing of the child entity subtree.
func enumerateHierarchy(body: (Entity, UnsafeMutablePointer<Bool>) -> Void) {
var stop = false
func enumerate(body: (Entity, UnsafeMutablePointer<Bool>) -> Void) {
guard !stop else {
return
}
body(self, &stop)
for child in children {
guard !stop else {
break
}
child.enumerateHierarchy(body: body)
}
}
enumerate(body: body)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment