I noticed that in the Swift UI sample’s SwiftUIDriver, the CallbackFromNative method has this small if-statement:
if (command == "closed") {
self.m_SwiftUIWindowOpen = false;
return;
}
But I don’t see anything in the native Swift code actually sending a “closed”-command. Hasn’t this been implemented? I would like to see an example of receiving a command when the native SwiftUI window is closed so I can react to that in Unity.
This looks like an oversight to me. Nothing currently sends the “closed” message, and SwiftUIDriver itself sets the m_SwiftUIWindowOpen state when it opens or closes the window via OpenSwiftUIWindow/CloseSwiftUIWindow. However, note that HelloWorldContentView.swift sends an “appeared” message in the onAppear handler. You could add the “closed” message in an onDisappear handler:
// ...
VStack {
// ...
}
.onAppear {
// Call the public function that was defined in SwiftUISamplePlugin
// inside UnityFramework
CallCSharpCallback("appeared")
// ...
}
.onDisappear {
CallCSharpCallback("closed")
}
We can add that to the sample for a future release.
In my testing, I found that this works in PolySpatial 2.X/visionOS 2.0 beta, but not in PolySpatial 1.X/visionOS 1.0. So, we added it to that version only. I didn’t find any alternative means of getting a callback in 1.0, but if you find one, definitely let us know–and/or report the issue to Apple via their Feedback Assistant.