Here's a type safe approach you can follow to release a framework that can be used in two steps. In the consuming application:
- Create and register a component conforming to a protocol provided by the framework.
- Register the system provided by the framework.
Let's walk through how to accomplish this. Pretend you aim to ship BubbleSystem / BubbleComponent.
Create a Framework, let's name it BubbleFramework, containing a Protocol (which extends Component) and a generic System to leverages that protocol.
Component Protocol
public protocol BubbleComponentProtocol : Component {
var radius:Float {get}
}
System
public struct BubbleSystem<T:BubbleComponentProtocol>: System {
let query = EntityQuery(where: .has(T.self))
public init(scene: RealityKit.Scene) {
}
public func update(context: SceneUpdateContext) {
let bubbles = context.entities(matching: self.query,
updatingSystemWhen: .rendering)
for bubble in bubbles {
guard let component = bubble.components[T.self] else {continue}
// do things with the component...
}
}
}
}
Next, create a new application and add BubbleFramework to the app target's "Frameworks, Libraries, and Embedded Content". Then, create a component named BubbleComponent that conforms to BubbleComponentProtocol in the RealityKitContentPackage associated with the (newly created) application.
import BubbleFramework
public struct BubbleComponent: BubbleComponentProtocol, Component, Codable {
public var radius:Float = 0.1
public init() {
}
}
Note:
-
The RealityKitContentPackage is created for you when you create a visionOS application, but if you are targeting other platforms you will need to create it.
-
You must explicitly extend Component, Codable or the properties won't show up in the Reality Composer Pro inspector.
Finally register the BubbleComponent and BubbleSystem
BubbleComponent()
BubbleSystem<BubbleComponent>.registerSystem()
Profit!