简体   繁体   中英

Call Java function from Unity3D

I have an android application developed by Unity3D written by C# and I wan to hide the bottom bar when running my app. So I searched around and found that I need to use setSystemUiVisibility function from Java. Here is the code I found:

    using UnityEngine;

public class DisableSystemUI
{
static AndroidJavaObject activityInstance;
static AndroidJavaObject windowInstance;
static AndroidJavaObject viewInstance;

public delegate void RunPtr();

public static void Run()
{
    if (viewInstance != null) {
       viewInstance.Call("setSystemUiVisibility", 2);
    }
}

static DisableSystemUI()
{
    if (Application.platform != RuntimePlatform.Android)
       return;
    DisableNavUI();
}

static void DisableNavUI()
{
    if (Application.platform != RuntimePlatform.Android)
        return;

    using (AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        activityInstance = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
        windowInstance = activityInstance.Call<AndroidJavaObject>("getWindow");
        viewInstance = windowInstance.Call<AndroidJavaObject>("getDecorView");

        AndroidJavaRunnable RunThis;
        RunThis = new AndroidJavaRunnable(new RunPtr(Run));
        activityInstance.Call("runOnUiThread", RunThis);
    }
}
}

I tried to call DisableSystemUI.Run(); in my main scene. I rooted my device but the app quits after I run it. Not sure what's wrong with it? Thanks for help.

it would be better to understand if you post the java code i too had a same problem

try this

viewInstance.Call("setSystemUiVisibility", "SYSTEM_UI_FLAG_FULLSCREEN");

or this

viewInstance.Call("setSystemUiVisibility", "SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN");

should work....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM