Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android UnityPlayerActivity Action Bar

I am building an Android application which includes a Unity 3d interactive experience.

I have imported the Unity project into Android Studio but when launched the activity is fullscreen and does not show the Android action bar.

How can I do this?

Integration Steps

  • Create new Unity project.
  • Export "Google Android Project" from Unity.
  • Import project into Android Studio.

Attempted Solutions

  • Changing the theme in the manifest.
  • Setting the theme in the UnityPlayerActivity Java class.
  • Overriding the Unity Android Manifest by placing a manifest with an updated theme in the Unity directory /Assets/Plugins/Android.
  • Changing UnityPlayerActivity to extend AppCompatActivity. This will show the action bar but there is a white gap between it and the status bar.
  • Setting "Screen.fullScreen = false;" in Unity SceneManager. This removes the immersive mode so that the Android status bar is visible.
  • Turning off "Status Bar Hidden" in player settings. Appears to have no effect.
  • Wrapping the UnityPlayer in a FrameLayout. This allows me to resize the the Unity as a view.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company.unity.test"
    android:installLocation="preferExternal"
    android:versionCode="1"
    android:versionName="1.0">

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <application
        android:banner="@drawable/app_banner"
        android:debuggable="false"
        android:icon="@drawable/app_icon"
        android:isGame="true"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        <activity
            android:name="com.company.unity.test.UnityPlayerActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="fullSensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>
            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
        </activity>
    </application>

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="23" />

    <uses-feature android:glEsVersion="0x00020000" />
    <uses-feature
        android:name="android.hardware.sensor.accelerometer"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.touchscreen.multitouch.distinct"
        android:required="false" />
</manifest>

UnityPlayerActivity.java

package com.company.unity.test;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;

import com.unity3d.player.UnityPlayer;

public class UnityPlayerActivity extends Activity
{
    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code

    // Setup activity layout
    @Override protected void onCreate (Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy

        mUnityPlayer = new UnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

    // Quit Unity
    @Override protected void onDestroy ()
    {
        mUnityPlayer.quit();
        super.onDestroy();
    }

    // Pause Unity
    @Override protected void onPause()
    {
        super.onPause();
        mUnityPlayer.pause();
    }

    // Resume Unity
    @Override protected void onResume()
    {
        super.onResume();
        mUnityPlayer.resume();
    }

    // This ensures the layout will be correct.
    @Override public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mUnityPlayer.configurationChanged(newConfig);
    }

    // Notify Unity of the focus change.
    @Override public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);
        mUnityPlayer.windowFocusChanged(hasFocus);
    }

    // For some reason the multiple keyevent type is not supported by the ndk.
    // Force event injection by overriding dispatchKeyEvent().
    @Override public boolean dispatchKeyEvent(KeyEvent event)
    {
        if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
            return mUnityPlayer.injectEvent(event);
        return super.dispatchKeyEvent(event);
    }

    // Pass any events not handled by (unfocused) views straight to UnityPlayer
    @Override public boolean onKeyUp(int keyCode, KeyEvent event)     { return mUnityPlayer.injectEvent(event); }
    @Override public boolean onKeyDown(int keyCode, KeyEvent event)   { return mUnityPlayer.injectEvent(event); }
    @Override public boolean onTouchEvent(MotionEvent event)          { return mUnityPlayer.injectEvent(event); }
    /*API12*/ public boolean onGenericMotionEvent(MotionEvent event)  { return mUnityPlayer.injectEvent(event); }
}
like image 662
Morepork Avatar asked Nov 06 '15 02:11

Morepork


2 Answers

for Unity version 5.5 best way to avoid Immersive Mode in Android is to pass in the constructor not an Activity but to cast of the ApplicationContext to the Wrapper Context.

mUnityPlayer = new UnityPlayer((ContextWrapper) getApplicationContext());

That works because in the obfuscated UnityPlayer.class (in unity-classes.jar) there is a check of instance.

 private void h() {
     if(this.h instanceof Activity) {
        ((Activity)this.h).getWindow().setFlags(1024, 1024);
     }
 }

So if it's not an Activity the UnityPlayer.class doesn't set the flag.

like image 116
Lorenzo DM Avatar answered Nov 19 '22 23:11

Lorenzo DM


I think I've found solution.
Change that line:

mUnityPlayer = new UnityPlayer(this);

so it creates your own UnityPlayer subclass, which overrides setFullscreen method (little bit hacky):

public class UnityPlayerWrapper extends UnityPlayer {
    public UnityPlayerWrapper(ContextWrapper contextWrapper) {
        super(contextWrapper);
    }

    @Override
    protected void setFullscreen(boolean b) {
        super.setFullscreen(false);
    }
}

In addition, please remove that line: requestWindowFeature(Window.FEATURE_NO_TITLE);

like image 4
Piotr Avatar answered Nov 19 '22 22:11

Piotr