Using SwiftUI together with Unity
On visionOS, it is possible to use SwiftUI together with a Unity application. The Samples contain a SwiftUI
sample that demonstrates one approach.
For detailed information, please review the comments in the following files in the sample:
SwiftUIDriver.cs- A MonoBehaviour that drives the interop with SwiftUI. It uses
DllImportto access methods defined in Swift.
- A MonoBehaviour that drives the interop with SwiftUI. It uses
SwiftUISamplePlugin.swift- Swift code that interfaces with
SwiftUIDriverto provide the ability to pass information back and forth between C# and Swift.
- Swift code that interfaces with
SwiftUISampleInjectedScene.swift- A Swift type that defines a SwiftUI
Scenethat is injected into the top-level App's Scenes. Swift files ending in...InjectedScene.swiftare automatically injected, removing the need to modify the generated the top-level App.
- A Swift type that defines a SwiftUI
HelloWorldContentView.swift- A Swift
Viewthat is used by theWindowGroupdefined inSwiftUISampleInjectedScene. This file is in a directory calledSwiftAppSupport. All Swift files under aSwiftAppSupportdirectory will be made available in the top-level App (instead ofUnityFrameworkin the Xcode project).
- A Swift
By using an approach similar to the sample, you can take advantage of SwiftUI for platform-native UI in separate windows while displaying 3D content from Unity in a volumetric window.
Mixing SwiftUI elements with Unity-managed content in a single volumetric window is not supported.