Search Unity

Detecting an incomming call in Unity App and pausing a game.

Discussion in 'Scripting' started by indiana_toms, Mar 19, 2018.

  1. indiana_toms

    indiana_toms

    Joined:
    Nov 18, 2017
    Posts:
    4
    I am trying to make my game pause on incomming phone call. I an wondering if any of this functions which i used can do this. I have used them in my source code but none of them worked.


    Code (CSharp):
    1.  
    2. void OnApplicationPause(bool paused)
    3. {
    4.    if (paused == true)
    5.    {
    6.        if (!isPaused)
    7.        PauseResume(true);
    8.    }
    9. }
    10.  
    11. void OnApplicationFocus(bool hasFocus)
    12. {
    13.    if (hasFocus == false)
    14.    {
    15.        if (!isPaused)
    16.            PauseResume(true);
    17.    }
    18. }
    19.  

    Also i had found Application.runInBackground() but it is mentioned in documentation that "Note: This property is ignored on Android and iOS".
     
    RARgames likes this.
  2. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Hm, from what I can find, the answer will be very OS specific. Try running some tests like:
    Code (CSharp):
    1. void OnApplicationFocus(bool hasFocus) {
    2.     if (hasFocus) Debug.Log("Has Focus");
    3.     else Debug.Log("Lost Focus");
    4. }
    That should tell you if the phone call is actually triggering OnApplicationFocus method (if you have the debug build enabled). Then, if it is, try replacing those Debug statements with:
    Code (CSharp):
    1. if (hasFocus) Time.timeScale = 1;
    2. else Time.timeScale = 0;
    This will stop all time based operations, like physics, FixedUpdate, or deltaTime scaled updates. But the game will remain responsive, so you can have other things continue.
     
  3. indiana_toms

    indiana_toms

    Joined:
    Nov 18, 2017
    Posts:
    4
    I'm testing it on android 8 and it seems that my app is not losing focus on call (not even when I answer it)
    To lose focus I need to answer the call and than go to "call" window, otherwise the "Lost focus" is not logged
     
  4. CDMcGwire

    CDMcGwire

    Joined:
    Aug 30, 2014
    Posts:
    133
    Hmm... My first thought is to access the OS's api directly. In particular, there's this Java Listener class that would normally be extended (as seen here). However, that's in Java land, and these scripts are obviously not that.

    One option for that would be to build a plugin (probably wouldn't have to be very complicated either) in Java, ala these pages of the manual. This would be the most robust and clean way to implement it. Though probably the most involved.

    Essentially, you'd extend the Java that's already being generated by Unity when you build the game, then Unity's plugin framework would expose that to C#. In your plugin/extension you'd create the listener class from that first link and setup some method that "hopefully" let you assign a C# callback to the OS's event. Otherwise, you may need to leave the events on the Java side, and simply update a primitive that C# can check.

    I didn't get too deep as for what the interface limitations are for plugins.

    This page has additional insights that you might find useful as well.