Sample CodevisionOSReviewed 2026-07-21View on Apple Developer

Integrating virtual objects with your environment

At a glance

Item Summary
Purpose Create an immersive game using native anchor support, environmental blending, model manipulation, and mesh instance duplication.
App architecture TinyTreasureTroveApp owns AppModel; ImmersiveView loads authored content and GameObjectComponent/GameObjectSystem coordinate virtual objects with the reconstructed environment.
Main patterns SwiftUI scene composition, UI–RealityKit bridge, Observable state owner, Entity-component-system, Authored-content boundary, Environment-aware ECS
Project style Code-rich sample with 16 scanned Swift file(s) and 1160 implementation line(s).

Project structure

Source bundle/
├── TinyTreasureTrove/AppModel.swift  # AppModel, ImmersiveSpaceState, GameSetUpState
├── TinyTreasureTrove/Gameplay/GameObjectSystem.swift  # GameObjectSystem
├── TinyTreasureTrove/UI/ImmersiveView.swift  # ImmersiveView
├── TinyTreasureTrove/TinyTreasureTroveApp.swift  # TinyTreasureTroveApp
├── TinyTreasureTrove/ECS/AnimationCallbackComponent.swift  # AnimationCallbackComponent
├── TinyTreasureTrove/ECS/ChestComponent.swift  # ChestComponent
├── TinyTreasureTrove/ECS/GameObjectComponent.swift  # GameObjectComponent
└── Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json  # authored RealityKit content

Structure observations

  • The runtime boundary is WindowGroup + volumetric window + ImmersiveSpace; the tree is pruned to lifecycle, state, framework, rendering, and ECS files.
  • The source is Swift; role-named types and feature folders separate the major responsibilities.
  • Authored .reality, .rkassets, or Reality Composer Pro content is an implementation boundary; Swift loads or drives it rather than reproducing its entity graph.

Overall architecture

Reference code

TinyTreasureTrove/TinyTreasureTroveApp.swift:11 — executable or app-lifecycle anchor.

struct TinyTreasureTroveApp: App {
    // ...
}

The diagram is a responsibility flow, not a claim that every adjacent node directly calls the next. It separates lifecycle, presentation, stable state, framework/session work, and the RealityKit entity graph.

Ownership and state

Ownership evidence

TinyTreasureTrove/TinyTreasureTroveApp.swift:12 — representative stored state or nearest verified lifecycle anchor.

    @State private var appModel = AppModel()
Owner Object or state Relationship Mutation authority
TinyTreasureTroveApp AppModel as appModel creates and retains The declaring type controls writes
ImmersiveView SpatialTrackingSession as spatialTrackingSession creates and retains The owning type coordinates writes
ImmersiveView AppModel as appModel receives a shared or non-owning reference The upstream owner controls lifetime; this scope may invoke the exposed mutable API
AppModel ImmersiveSpaceState as immersiveSpaceState owns value state The owning type coordinates writes

Ownership is intentionally narrow: environment, bindings, observed objects, weak links, and delegate callbacks do not prove lifetime ownership. Initialized stored services and wrapper-managed state do; entities added inside a RealityKit content closure belong to that entity graph.

Class and protocol design

Type Responsibility Depends on or conforms to
TinyTreasureTroveApp (TinyTreasureTrove/TinyTreasureTroveApp.swift:11) Declares app lifecycle and top-level dependency lifetime. App
ImmersiveView (TinyTreasureTrove/UI/ImmersiveView.swift:11) Presents UI and forwards gestures or lifecycle events. View
AppModel (TinyTreasureTrove/AppModel.swift:12) Owns feature state and domain transitions. Concrete framework collaborators
GameObjectSystem (TinyTreasureTrove/Gameplay/GameObjectSystem.swift:10) Updates entities matching a RealityKit component query. System
AnimationCallbackComponent (TinyTreasureTrove/ECS/AnimationCallbackComponent.swift:10) Stores RealityKit entity data. Component
ChestComponent (TinyTreasureTrove/ECS/ChestComponent.swift:10) Stores RealityKit entity data. Component
GameObjectComponent (TinyTreasureTrove/ECS/GameObjectComponent.swift:11) Stores RealityKit entity data. Component
KeyComponent (TinyTreasureTrove/ECS/KeyComponent.swift:10) Stores RealityKit entity data. Component

The reviewed source defines no local substitution protocol. Its App, View, delegate, renderer, ARKit, and RealityKit conformances are framework-facing contracts, so this document does not label the whole app protocol-oriented.

Access control

Symbol Access Verified effect Likely rationale
private let gameObjectQuery = EntityQuery(where: .has(GameObjectCompone... (TinyTreasureTrove/Gameplay/GameObjectSystem.swift:12) private Use stays inside the declaration and same-file extensions permitted by Swift. Inference: Hide lifecycle-sensitive state or an implementation step.
public let realityKitContentBundle = Bundle.module (Packages/RealityKitContent/Sources/RealityKitContent/RealityKitContent.swift:10) public The symbol is available to importing modules, subject to its containing type’s visibility. Inference: Expose an intentional package or cross-target surface.

No reviewed Swift access example uses fileprivate, private(set), open; declarations without a modifier are internal.

Reference code

TinyTreasureTrove/Gameplay/GameObjectSystem.swift:12 — representative visibility boundary.

    private let gameObjectQuery = EntityQuery(where: .has(GameObjectComponent.self))
    
    init(scene: RealityKit.Scene) {
        GameObjectComponent.registerComponent()
    }

Logic ownership and placement

Logic Owning type or file Placement rationale
Application/command lifecycle TinyTreasureTroveApp The executable boundary determines app, controller, scene, or command lifetime.
Presentation and user intent ImmersiveView The view/controller forwards input and owns only presentation-local work.
Shared feature state and commands AppModel A stable owner prevents view recomputation or callbacks from duplicating transitions.
Framework/session/render coordination GameObjectSystem The role-named collaborator isolates long-lived or per-frame work from presentation code.
Per-entity data and repeated behavior TinyTreasureTrove/Gameplay/GameObjectSystem.swift:10 Components carry data; the system queries and updates matching entities.
Authored scene composition Packages/RealityKitContent/Package.realitycomposerpro/ProjectData/main.json Reality Composer Pro owns hierarchy, materials, animation, or behavior that Swift loads and coordinates.

Design patterns

Pattern Source evidence Purpose or tradeoff
SwiftUI scene composition TinyTreasureTrove/TinyTreasureTroveApp.swift:11 Keeps windows, volumes, and immersive presentation visible at the app boundary.
UI–RealityKit bridge TinyTreasureTrove/UI/ImmersiveView.swift:18 Builds or hosts the RealityKit entity graph from SwiftUI or a platform view/controller lifecycle.
Observable state owner TinyTreasureTrove/AppModel.swift:12 Keeps shared feature state in a stable owner rather than in transient view values.
Entity-component-system TinyTreasureTrove/Gameplay/GameObjectSystem.swift:10 Stores per-entity data in components and advances repeated behavior in registered systems.
Authored-content boundary TinyTreasureTrove/Gameplay/GameLogic.swift:66 Treats Reality Composer Pro assets as implementation and keeps Swift responsible for loading and runtime coordination.
Environment-aware ECS TinyTreasureTrove/Gameplay/GameObjectSystem.swift:10 Keeps per-object state on entities and centralizes repeated interaction with reconstructed surroundings in a system.

Naming conventions

  • Role suffixes are evidence, not decoration: App: TinyTreasureTroveApp; Model: AppModel; System: GameObjectSystem; Component: AnimationCallbackComponent, ChestComponent, GameObjectComponent, KeyComponent, ThrowableComponent; View: ContentView, ImmersiveView.
  • ECS names pair data and behavior: components AnimationCallbackComponent, ChestComponent, GameObjectComponent, KeyComponent, ThrowableComponent; systems GameObjectSystem.
  • Protocols: no app-defined protocol in the reviewed source.
  • Commands use verb-led methods: update, addCallback, handleEvent, playAnimation, runOnCompletion, setUpGameEntity.
  • Files and folders use primary-type or responsibility names such as Views, Models, Rendering, Components, Systems, Packages, or feature names where those boundaries exist.

Architecture takeaways

  • Treat TinyTreasureTroveApp as the owner of executable or scene lifecycle, not automatically as the owner of every entity created later.
  • Keep view/controller input near presentation, but move sessions, reconstruction, capture, rendering, shared game state, or documents into stable owners when their lifetime exceeds one callback or render pass.
  • Use components for per-entity data and systems for repeated simulation instead of centralizing every update in a view model or controller.
  • Count authored Reality Composer Pro assets as implementation; the Swift shell is not the complete architecture.

Source map

Source file Relevant symbols
TinyTreasureTrove/AppModel.swift:12 AppModel, ImmersiveSpaceState, GameSetUpState
TinyTreasureTrove/Gameplay/GameObjectSystem.swift:10 GameObjectSystem
TinyTreasureTrove/UI/ImmersiveView.swift:11 ImmersiveView
TinyTreasureTrove/TinyTreasureTroveApp.swift:11 TinyTreasureTroveApp
TinyTreasureTrove/ECS/AnimationCallbackComponent.swift:10 AnimationCallbackComponent
TinyTreasureTrove/ECS/ChestComponent.swift:10 ChestComponent
TinyTreasureTrove/ECS/GameObjectComponent.swift:11 GameObjectComponent