4

I'm currently developing some Unity project(this is my first one, I'm not familiar with Unity) and what bothers me is that breakpoints are still hitting when I minimize Unity editor window although I have Application.runInBackground == false so the app should be suspended while in background, but it is not ! Only the UI gets frozen and music stops while the actual code is continue executing. So, I'm basically wondering if this is general behaviour or this only happens when you're running in editor to allow debugging and in release builds code execution stops ?

Jim Smith
  • 41
  • 1
  • 2
  • Welcome new user - I'm not sure *precisely* what effect you mean, but yes, unfortunately that sort of thing ***does not work as you would expect in the editor***. try a desktop build? – Fattie Jan 28 '21 at 16:03
  • `Only the UI gets frozen and music stops` .. do you mean you are hitting breakpoints from different threads/async Tasks? You can't avoid them from continuing running! The runInBsakground only affects the Unity "main" thread. – derHugo Jan 28 '21 at 16:09
  • @derHugo breakpoint that is set in `Update()` that is inherited from `MonoBehaviour` is still hitting when the app is minimized and I'm pretty sure that `Update()` is called on the "main" thread – Jim Smith Jan 28 '21 at 16:15
  • So just to be sure you minimize and actually click on a different program on your desktop? – derHugo Jan 28 '21 at 16:18
  • @derHugo I click on Visual Studio, I guess it doesn't really matter if you minimize the app or click on another one since in both cases unity editor loses focus. So, I basically start the app then switch to VS(when I switch to VS UI gets frozen and music stops) then I set breakpoint in `Update()` and it htis – Jim Smith Jan 28 '21 at 16:22
  • Visual Studio might be the exception here though since of course while debugging you want your app to continue .. otherwise the entire execution would be stopped => you couldn't use continue or step over since your code would be paused. Did you try to set a breakpoint and have a different application open? – derHugo Jan 28 '21 at 16:48

2 Answers2

2

I had similar issues. I found that it is because I have an IDE attached to Unity and only then did this happen. When you switch to release mode, everything go as it's supposed to - debug mode in Unity.

ouflak
  • 2,458
  • 10
  • 44
  • 49
Kerwish
  • 21
  • 4
-1

Did you try changing it in the editor? go to Edit -> Project Settings -> Player. The inspector pane will now change to show the player settings. Look for the option that says "Run In Background" and uncheck it. From what I understand Application.runInBackground == false more of pauses it than stops the script from running. If your project is for mobile(or maybe just add this anyway, it might help),try:

RuninBackgorund is ignored for Android and IOS how you can see here

The only and efficient way to run in background on Android is through a Service. You can use OnApplicationPause method to get notified when the game pauses/unpauses.

public void OnApplicationPause(bool paused) {
if(paused) {
// Game is paused, stop service notifications.
} else {
// Game is unpaused, start service to get notifications
}

Here you have a nice tutorial about communication between android and unity

Baby_Boy
  • 346
  • 3
  • 18