Using SwiftUI together with Unity
On visionOS, it is possible to use SwiftUI together with a Unity application built
for mixed reality (Immersive Space). 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
DllImport
to access methods defined in Swift.
- A MonoBehaviour that drives the interop with SwiftUI. It uses
SwiftUISamplePlugin.swift
- Swift code that interfaces with
SwiftUIDriver
to 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
Scene
that is injected into the top-level App's Scenes. Swift files ending in...InjectedScene.swift
are automatically injected, removing the need to modify the generated the top-level App.
- A Swift type that defines a SwiftUI
HelloWorldContentView.swift
- A Swift
View
that is used by theWindowGroup
defined inSwiftUISampleInjectedScene
. This file is in a directory calledSwiftAppSupport
. All Swift files under aSwiftAppSupport
directory will be made available in the top-level App (instead ofUnityFramework
in 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.