本页面包含以下部分:
When you develop a Unity Android application, you can use plug-ins to extend the standard UnityPlayerActivity
class (the primary Java class for the Unity Player on Android, similar to AppController.mm
on Unity iOS). An application can override all basic interactions between the Android OS and the Unity Android application.
要覆盖默认活动,请执行以下操作:
UnityPlayerActivity
派生的 Activity
(请参阅 Android 文档中有关活动 (Activity) 的部分);Activity
作为应用程序的入口点。The easiest way to achieve this is to export your project from Unity, then make the necessary modifications to the UnityPlayerActivity
class in Android Studio. Or, you can create a new class, extend it, modify AndroidManifest.xml
in the unityLibrary project, and then replace UnityPlayerActivity
with your class.
To create a plug-in with your new Activity and add it to your Project, you must perform the following steps: 1. Extend the UnityPlayerActivity file. This is best done in Android Studio after exporting project from Unity. There are several options: * The .java or .kt file containing your activity class can put put into Unity project * Create Java library containing your class, compile it and put resulting .jar file into Unity project * Create Android library containint your class; this library can be put into Unity project in source code from (by naming the folder to have .androidlib “extension”) or it can be compiled and resulting .aar be put into Unity project
To create a plug-in with your new Activity
and add it to your Project, perform the following steps:
Extend the UnityPlayerActivity file. The best way to do this is in Android Studio after exporting your project from Unity. You then have the following options:
macOS:
/Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidPlayer/src/com/unity3d/player
Windows:
C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\src\com\unity3d\player
Create a new Android manifest to set the new Activity
as the entry point of your application, then place the AndroidManifest.xml
file in the Assets/Plugins/Android
folder of your Project.
扩展 UnityPlayerActivity
时,可覆盖 String UnityPlayerActivity.updateUnityCommandLineArguments
(String cmdLine) 以将启动参数传递给 Unity。
UnityPlayerActivity
会在启动期间调用此方法。它接受当前的命令行参数(这些参数可为 null 或为空),并返回新的命令行参数字符串以传递给 Unity。
有关 Unity 命令行界面的一般概述,请参阅命令行参数。
下面的示例演示了如何使用该文件根据当前设备选择图形 API:
package com.company.product;
import com.unity3d.player.UnityPlayerActivity;
import android.os.Bundle;
import android.os.Build;
public class OverrideExample extends UnityPlayerActivity {
private boolean preferVulkan() {
// 在 Google Pixel 设备上使用 Vulkan
if (Build.MANUFACTURER.equals("Google") && Build.MODEL.startsWith("Pixel"))
return true;
else
return false;
}
private boolean preferES2() {
// 在运行 Android 5.1 或更早版本的设备上使用 OpenGL ES 2.0
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1)
return true;
else
return false;
}
private String appendCommandLineArgument(String cmdLine, String arg) {
if (arg == null || arg.isEmpty())
return cmdLine;
else if (cmdLine == null || cmdLine.isEmpty())
return arg;
else
return cmdLine + " " + arg;
}
@Override protected String updateUnityCommandLineArguments(String cmdLine)
{
if (preferVulkan())
return appendCommandLineArgument(cmdLine, "-force-vulkan");
else if (preferES2())
return appendCommandLineArgument(cmdLine, "-force-gles20");
else
return cmdLine; // 让 Unity 根据 PlayerSettings 选择图形 API
}
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
The following is an example of a UnityPlayerActivity
file:
OverrideExample.java:
package com.company.product;
import com.unity3d.player.UnityPlayerActivity;
import android.os.Bundle;
import android.util.Log;
public class OverrideExample extends UnityPlayerActivity {
protected void onCreate(Bundle savedInstanceState) {
// 调用 UnityPlayerActivity.onCreate()
super.onCreate(savedInstanceState);
// 将调试消息打印至 logcat
Log.d("OverrideActivity", "onCreate called!");
}
public void onBackPressed()
{
// 不调用 UnityPlayerActivity.onBackPressed(),而是直接忽略 Back 按钮事件
// super.onBackPressed();
}
}
The corresponding AndroidManifest.xml
might look like the following:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.product">
<application android:icon="@drawable/app_icon" android:label="@string/app_name">
<activity android:name="com.YourPackage.name.OverrideExample"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
To create a plug-in with your new Activity and add it to your Project, you must perform the following steps: 1. Extend the UnityPlayerActivity file. This is best done in Android Studio after exporting project from Unity. There are several options: * The .java or .kt file containing your activity class can put put into Unity project * Create Java library containing your class, compile it and put resulting .jar file into Unity project * Create Android library containint your class; this library can be put into Unity project in source code from (by naming the folder to have .androidlib “extension”) or it can be compiled and resulting .aar be put into Unity project 2. Create a new Android Manifest to set the new Activity as the entry point of your application, then place the AndroidManifest.xml file in the Assets/Plugins/Android folder of your Project.