Unchecking Run in background causes pointers to remain visible upon going to shell and back
Describe the bug
Hand rays getting stuck if you go out of the app to shell and going back if the application has "Run in background" unchecked. Old hand rays get stuck and new hand rays pop up. So you can see 3 or more hand rays at the same time. Also seems grabbing stops working, not always but sometimes. But if far interaction pointers are visible, they always get stuck upon going to shell.
This ticked is reopening of https://github.com/microsoft/MixedRealityToolkit-Unity/issues/4196 since the problem was discovered why our app behaves differently than MRTK example app. The difference was Run in Background being unchecked in ours and checked in MRTK.
We would prefer for our app to not have to Run in Background, to not drain battery. But if this is required it should be documented somewhere.
To reproduce
Reproducible with standard MRTK example build. Before building uncheck "Run in Background" in "Resolution and Presentation" part of the "Player Settings". Then on device in application if you hit the home button with hand rays visible and then go to shell via Home Button on Start menu. Wait couple of seconds (~10s). And open the app the previous hand rays are visible and new ones come up.
Expected behavior
Only new hand rays are visible
Your Setup (please complete the following information)
- Reproduced with mrtk_development branch, commit a84a09c2eac3b51c0ef67fa96545ca068168170b
- Unity Version 2018.3.9f1 IL2CPP
- Master build
- Only Change in that commit is unchecking the Run in Background
Target Platform (please complete the following information)
- HoloLens 2 - Fast Ring
Reproed! Using:
- Latest from mrtk_development (bffa2f2345c1bf531c3b09f79c49bcfa53b22d08)
- Unity 2018.4.1f
- Windows SDK 18315
- Windows 18362.1063 on the HoloLens 2 device
- Running the HandInteractionExamples in a Master IL2CPP build without "Run in Background" checked
I'll dig into it next and keep this issue updated.
Just realized that the required Windows SDK version is 18362+. Will update and try again.
Still reproing with SDK 18362
I believe this comes down to InteractionManager not raising source lost events for the hands in view when the app enters the shell. I've created a bug for Unity to look into it.
@mgrman , as a workaround you can try adding the code below to WindowsMixedRealityDeviceManager to clear out existing controllers when the application looses focus. Bear in mind that this will cause issues in WMR devices as the app looses focus when you remove the headset but the 6 DOF controllers are still there, so they will be cleared out and never regained.
public override void Enable()
{
// ...
#if !UNITY_EDITOR
Application.focusChanged += OnFocusChanged;
#endif
}
public override void Disable()
{
#if !UNITY_EDITOR
Application.focusChanged -= OnFocusChanged;
#endif
// ...
}
#if !UNITY_EDITOR
private void OnFocusChanged(bool focus)
{
// Remove all active controllers when losing focus
if (!focus && !Application.runInBackground)
{
IMixedRealityInputSystem inputSystem = Service as IMixedRealityInputSystem;
foreach (var keyValue in activeControllers)
{
var controller = keyValue.Value as BaseWindowsMixedRealitySource;
if (controller != null)
{
inputSystem?.RaiseSourceLost(controller.InputSource, controller);
foreach (IMixedRealityPointer pointer in controller.InputSource.Pointers)
{
if (pointer != null)
{
pointer.Controller = null;
}
}
}
}
activeControllers.Clear();
}
}
#endif // !UNITY_EDITOR
I'm removing the GA blocker tag given that this seems like a Unity bug and we've posted a workaround here.
Closing this issue and posted the workaround to Stack overflow https://stackoverflow.com/questions/57174772/hand-rays-getting-stuck-if-you-go-out-of-the-app-to-shell-and-going-back-if-the/57174812#57174812