gpt4 book ai didi

java - Android UnityPlayerActivity 操作栏

转载 作者:搜寻专家 更新时间:2023-10-30 20:01:02 25 4
gpt4 key购买 nike

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

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

我该怎么做?

集成步骤

  • 创建新的 Unity 项目。
  • 从 Unity 导出“Google Android 项目”。
  • 将项目导入 Android Studio。

尝试的解决方案

  • 更改 list 中的主题。
  • 在 UnityPlayerActivity Java 类中设置主题。
  • 通过在 Unity 目录/Assets/Plugins/Android 中放置具有更新主题的 list 来覆盖 Unity Android list 。
  • 更改 UnityPlayerActivity 以扩展 AppCompatActivity。这将显示操作栏,但它和状态栏之间有一个白色间隙。
  • 设置“Screen.fullScreen = false;”在 Unity 场景管理器中。这会移除沉浸式模式,以便 Android 状态栏可见。
  • 在播放器设置中关闭“隐藏状态栏”。似乎没有效果。
  • 将 UnityPlayer 包装在 FrameLayout 中。这允许我调整 Unity 作为 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); }
}

最佳答案

对于 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);
}
}

因此,如果它不是 Activity,则 UnityPlayer.class 不会设置标志。

关于java - Android UnityPlayerActivity 操作栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33558564/

25 4 0