Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Unity iOS OnApplicationFocus not running code inside

Discussion in 'iOS and tvOS' started by coldenwebdev, Sep 24, 2023.

  1. coldenwebdev

    coldenwebdev

    Joined:
    Oct 11, 2021
    Posts:
    21
    Hello everyone,

    in my game I need to implement that some parts of the game are not visible when focus is lost. Mainly I am talking about "multitasking" menu on iOS and Android when screen is visible but game is paused. I tried using this code:
    Code (CSharp):
    1.     void OnApplicationFocus(bool isApplicationHasFocus) {
    2.        
    3.         if (isApplicationHasFocus == false) {
    4.             pauseCanvas.SetActive(true);
    5.             Debug.Log("Paused");
    6.         } else if (isApplicationHasFocus == true) {
    7.             pauseCanvas.SetActive(false);
    8.             Debug.Log("Unpaused");
    9.         }
    10.  
    11.     }
    When testing in Editor everything works as expected. When running on iPhone connected via Xcode the logs "Paused" and "Unpaused" are working correctly but any other part of code inside is not run. I made sure that pauseCanvas is not undefined, I am getting no errors and in Editor everything works as expected.

    Unity version: 2021.3.25f1
    Testing device: iPhone 12 Pro - iOS 15.6

    Did I miss anything or misunderstand how this functionality works? Any help is appreciated, thank you!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,717
    Then why are you setting them visible when you don't have focus?

     
    MaskedMouse likes this.
  3. coldenwebdev

    coldenwebdev

    Joined:
    Oct 11, 2021
    Posts:
    21
    Not sure if I understand what you mean - this code works correct in Editor. I am not making the text visible, I am making the pause canvas visible which in this case is just a gray screen. The issue is, outside of editor the part of code with SetActive() - does not matter if true or false - is simply not executed at all.
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    You're making the pause canvas visible when you go out of application focus.
    You're hiding the pause canvas when you get focus again.
    What do you expect to see?
    Rendering stops when it goes to the background.
    Rendering starts when it gets focus again but you hide the canvas so nothing is shown in the end.

    Have you actually tried a
    Debug.Log($"OnApplicationPause - {isApplicationHasFocus)}", this);

    inside the method?
    Does it log anything to xcode?
    If it does, then you know it is working fine.
    If it doesn't, I question whether the game object with the component attached is actually active in the scene.
    If it is, then you might want to try updating to the latest LTS version of 2021.
    If that doesn't work perhaps try 2022 LTS.
    If that doesn't work, send a bug report.
    But as far as I am aware,
    OnApplicationFocus
    works just fine.
     
  5. coldenwebdev

    coldenwebdev

    Joined:
    Oct 11, 2021
    Posts:
    21
    Thanks for your reply, logs are working as expected in Xcode, meaning the event is triggered correctly and returns expected bool but none of the other code is run. I just tested on Android, code works as expected, I get a log and pauseCanvas is activated. Android and Editor activate the pauseCanvas before actually going into background, iOS doesn't - not sure if it just works that way and I have to figure out something else or if this is a bug/my mistake. I will try to update my Unity version and see what happens there.
     
  6. coldenwebdev

    coldenwebdev

    Joined:
    Oct 11, 2021
    Posts:
    21
    Just to be very clear, this is what I am talking about - first video is Android recording (sorry for the quality my Android device is really old) where it works as expected, when entering the multitasking menu screen goes gray. Second video is iOS recording with exactly same code implemented but in multitasking menu it just stay the same.

     
  7. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    282
    Have you enabled "Render Extra Frame on Pause" in the iOS settings?
     
    coldenwebdev likes this.
  8. coldenwebdev

    coldenwebdev

    Joined:
    Oct 11, 2021
    Posts:
    21
    Oh wow, I never heard about this setting, funny how one checkbox can solve 2 days of troubleshooting.. Thank you so much, everything works as expected after enabling the "Render Extra Frame on Pause", this solved my issue!