Repository navigation
fix(ios): find the host window under the UIScene life cycle - #193
Draft
AsterFerre wants to merge 2 commits into
Draft
AsterFerre wants to merge 2 commits into
AsterFerre wants to merge 2 commits into
Conversation
unloadUnity() and the old-arch view manager looked up the host window via UIApplication.delegate.window. Under the UIScene life cycle (required by the iOS 27 SDK, default in Expo SDK 58) the window belongs to the SceneDelegate and delegate.window is nil, so unloadUnity() skipped unloadApplication entirely and Unity stayed resident. RNUnityHostWindow() resolves the host window from the view itself, then the foreground scene's delegate window or key window, then the app delegate as before. unloadUnity() now unloads even when no host window is found.
<UnityView> calls unloadUnity on unmount. With the host-window fix the unload now really happens under the UIScene life cycle, but Fabric recycles the view and _unloaded stayed YES, so the next mount never restarted Unity. Clear it in prepareForRecycle. The host-window scene lookup also accepts foreground-inactive scenes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
unloadUnity()resolves the host window throughUIApplication.sharedApplication.delegate.window, and so doesRNUnityViewManager'sview.Under the UIScene life cycle that property is
nil. The window belongs to theSceneDelegate. Apple requires the scene-based life cycle for apps built with the iOS 27 SDK (Apple guide), and Expo SDK 58 enables it by default (expo.fyi/ios-scene-lifecycle).Because the whole body of
unloadUnity()sits insideif (main != nil),unloadApplicationis never called in that setup. There is no error:onPlayerUnloadnever fires and Unity stays resident.Fix
A new helper,
RNUnityHostWindow(view, exclude), looks for the host window in this order:window;UIWindowScene, via its delegate'swindowand then its key window;delegate.window, as before.Unity's own
appController.windowis excluded.unloadUnity()still makes the host window key when it finds one. It now callsunloadApplicationregardless.RNUnityViewManager(old architecture) uses the same helper.Remount after unload.
<UnityView>callsunloadUnityon unmount, so once the unload really runs, Fabric hands the recycled view instance to the next mount with_unloadedstillYES, and Unity never restarts.prepareForRecyclenow clears it. (Second commit; non-scene apps hit the same path today.)Apps without scenes behave as before: the first two lookups return the same window
delegate.windowused to.Known limit: with several scenes on iPad and a view already detached from its window, the fallback can pick another scene's window.
view.windowcovers the normal command path.Testing
build-iosCI job currently fails atpod install(boost) before compiling, so CI can't confirm it either. That is why this is a draft. I'll mark it ready once it has been verified on a device:<UnityView>, callunloadUnity(), and confirmonPlayerUnloadfires.Feedback on the approach is welcome before then.