Android UnityPlayerActivity动作栏

时间:2015-11-06 02:39:52

标签: java android unity3d

我正在构建一个包含Unity 3d交互式体验的Android应用程序。

我已将Unity项目导入Android Studio,但在启动时,活动是全屏的,并未显示Android操作栏。

我该怎么做?

整合步骤

  • 创建新的Unity项目。
  • 导出"谷歌Android项目"来自Unity。
  • 将项目导入Android Studio。

尝试解决方案

  • 更改清单中的主题。
  • 在UnityPlayerActivity Java类中设置主题。
  • 通过在Unity目录/ Assets / Plugins / Android中放置包含更新主题的清单来覆盖Unity Android清单。
  • 更改UnityPlayerActivity以扩展AppCompatActivity。这将显示操作栏,但它与状态栏之间存在空白。
  • 设置" Screen.fullScreen = false;"在Unity SceneManager中。这将删除沉浸式模式,以便可以看到Android状态栏。
  • 关闭"状态栏隐藏"在播放器设置中。似乎没有效果。
  • 在FrameLayout中包装UnityPlayer。这允许我将Unity的大小调整为视图。

的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); }
}

3 个答案:

答案 0 :(得分:6)

对于Unity版本5.5,在Android中避免沉浸模式的最佳方法是传入构造函数而不是Activity,而是将ApplicationContext强制转换为Wrapper Context。

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

这是有效的,因为在混淆的UnityPlayer.class(在unity-classes.jar中)有一个实例检查。

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

因此,如果它不是活动,则UnityPlayer.class不会设置标记。

答案 1 :(得分:4)

我想我找到了解决办法 改变那一行:

mUnityPlayer = new UnityPlayer(this);

所以它创建了你自己的UnityPlayer子类,它覆盖了setFullscreen方法(有点hacky):

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

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

此外,请删除该行:requestWindowFeature(Window.FEATURE_NO_TITLE);

答案 2 :(得分:0)

Piotr的答案适用于旧版本,Lorenzo DM的答案也有效,但

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

无法在某些设备上使用。所以最后我修改UnityPlayerActivity这里是新的解决方案



public class UnityPlayerActivity extends AppCompatActivity
{
    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
    ActionBar actionBar;
    private Toolbar toolbar;
    private FrameLayout unityContainer;

    // Setup activity layout
    @Override protected void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mUnityPlayer = new UnityPlayer(this);
        setContentView(R.layout.activity_unity_player);
        mappingWidgets();
        init();

    }

    void  mappingWidgets(){
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        unityContainer = (FrameLayout) findViewById(R.id.unity_container);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        unityContainer.addView(mUnityPlayer.getView(), 0, layoutParams);
        mUnityPlayer.requestFocus();

    }
    void init(){
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                onDestroy();
            }
        });
        setTitle(getString(R.string.app_name));


    }


    @Override protected void onNewIntent(Intent intent)
    {
        setIntent(intent);
    }

    // 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();
    }

    // Low Memory Unity
    @Override public void onLowMemory()
    {
        super.onLowMemory();
        mUnityPlayer.lowMemory();
    }

    // Trim Memory Unity
    @Override public void onTrimMemory(int level)
    {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_RUNNING_CRITICAL)
        {
            mUnityPlayer.lowMemory();
        }
    }

    // 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); }
    // Pass any events not handled by (unfocused) views straight to UnityPlayer
    @Override public boolean onKeyUp(int keyCode, KeyEvent event)     {
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            onDestroy();
            return true;
        }
        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); }
}