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

calling setSystemUiVisibility

Discussion in 'Android' started by Matkins, Jun 11, 2012.

  1. Matkins

    Matkins

    Joined:
    Aug 24, 2009
    Posts:
    152
    Hi,

    On Ice Cream Sandwich there is a set of on-screen controls which are always there stealing screen space even when inside a fullscreen game. They can be made to be less obtrusive by setting the view's ui visibility to "low profile mode", like this:

    Code (csharp):
    1.  
    2. getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
    3.  
    My question is; how can I do this from within Unity? I've looked at AndroidJavaClass and AndroidJavaObject but it's not clear to me how (or if) I can get reference to the correct view instance like above.
     
  2. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    I got a bit of help on this one, thanks Erik!

    The buildVersion check makes my device crash, not sure what went wrong there, so I took it out. I need to test it on a <11 device.

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class ICSDim : MonoBehaviour
    4. {
    5. #if UNITY_ANDROID  !UNITY_EDITOR
    6.  
    7.     void Start()
    8.     {
    9.         Dim();
    10.     }
    11.    
    12.     void OnApplicationFocus(bool focus)
    13.     {
    14.         if (focus)
    15.         {
    16.             Dim();
    17.         }
    18.     }
    19.  
    20.     private void Dim()
    21.     {
    22.         //var buildVersion = new AndroidJavaClass("android.os.Build.VERSION");
    23.         //if (buildVersion.GetStatic<int>("SDK_INT") >= 11)
    24.         {
    25.             using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    26.             {
    27.                 using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    28.                 {
    29.                     activity.Call("runOnUiThread", new AndroidJavaRunnable(DimRunable));
    30.                 }
    31.             }
    32.         }
    33.     }
    34.  
    35.     public static void DimRunable()
    36.     {
    37.         using (var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    38.         {
    39.             using (var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
    40.             {
    41.                 using (var window = activity.Call<AndroidJavaObject>("getWindow"))
    42.                 {
    43.                     using (var view = window.Call<AndroidJavaObject>("getDecorView"))
    44.                     {
    45.                         const int SYSTEM_UI_FLAG_LOW_PROFILE = 1;
    46.  
    47.                         view.Call("setSystemUiVisibility", SYSTEM_UI_FLAG_LOW_PROFILE);
    48.                     }
    49.                 }
    50.             }
    51.         }
    52.     }
    53.  
    54. #endif //UNITY_ANDROID  !UNITY_EDITOR
    55. }
    56.  
     
    zeh likes this.
  3. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    It's not pretty, but it doesn't crash pre 11

    Code (csharp):
    1. E/Unity   ( 9086): getMethodID("setSystemUiVisibility", "(I)V") FAILED!
    2.  
    3. D/dalvikvm( 9086): GetMethodID: method not found: Lcom/android/internal/policy/impl/PhoneWindow$DecorView;.setSystemUiVisibility:(I)V
    4.  
    5. I/Unity   ( 9086): JNI: Unable to find method id for 'setSystemUiVisibility'
    6.  
    7. I/Unity   ( 9086):  
    8.  
    9. I/Unity   ( 9086): (Filename: ./Runtime/ExportGenerated/AndroidManaged/UnityEngineDebug.cpp Line: 43)
    10.  
    11. I/Unity   ( 9086):
    12.  
     
  4. elixir-bash

    elixir-bash

    Joined:
    Mar 27, 2012
    Posts:
    23
    Thanks for posting this!! This works for me on SONY Xperia SP :)
     
  5. DanTreble

    DanTreble

    Joined:
    Aug 31, 2010
    Posts:
    590
    It is built in to Unity 4 and up :)
     
  6. rejwan1

    rejwan1

    Joined:
    Jul 3, 2012
    Posts:
    40
    Hey, I know this is an old thread, but if anyone can help me out that would be great.

    Using DanTreble's code gave me this error:
    I/Unity (30153): Exception: No such proxy method: UnityEngine.AndroidJavaRunnableProxy.run()
    I/Unity (30153): at UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
    I/Unity (30153): at UnityEngine.AndroidJavaProxy.Invoke (System.String methodName, UnityEngine.AndroidJavaObject[] javaArgs) [0x00000] in <filename unknown>:0
    I/Unity (30153): at UnityEngine._AndroidJNIHelper.InvokeJavaProxyMethod (UnityEngine.AndroidJavaProxy proxy, IntPtr jmethodName, IntPtr jargs) [0x00000] in <filename unknown>:0

    However, calling the setSystemUiVisibility directly (without running it on the UI thread) worked, but I got an exception that it shouldn't be called from the Unity thread and the game got stuck.

    Any ideas?
     
  7. Agent_007

    Agent_007

    Joined:
    Dec 18, 2011
    Posts:
    899
  8. pukai2

    pukai2

    Joined:
    Feb 4, 2017
    Posts:
    1
    rejwan1 ,I have the same problem as you,How did you to solve the problem?