在开发 Unity Android 应用程序时,可使用插件来扩展标准的 UnityPlayerActivity 类(这是 Android 上的 Unity 播放器的主 Java 类,类似于 Unity iOS 上的 AppController.mm)。应用程序可以覆盖 Android 操作系统和 Unity Android 应用程序之间的任何及所有基本交互。
需要通过两个步骤来覆盖默认活动:
创建从 UnityPlayerActivity 派生的新活动;
修改 Android 清单以将新活动作为应用程序的入口点。
为实现此目的,最简单的方法是导出项目,并在 Android Studio 中对 UnityPlayerActivity 类进行必要的修改。
要用新活动代码创建插件并将其添加到 Unity 项目,必须执行以下步骤:
扩展 UnityPlayerActivity。UnityPlayerActivity.java 文件位于 Mac 上的 /Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidPlayer/src/com/unity3d/player 和 Windows 上的 C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\src\com\unity3d\player。要扩展 UnityPlayerActivity,请找到 Unity 附带的 classes.jar。该文件位于安装文件夹(通常为 C:\Program Files\Unity\Editor\Data(Windows 上)或 /Applications/Unity(Mac 上))的子文件夹中,子文件夹名为 PlaybackEngines/AndroidPlayer/Variations/mono 或者是 il2cpp/Development 或 Release/Classes/。然后将 classes.jar 添加到用于编译新活动的类路径中。编译活动源文件并将其打包到 JAR 或 AAR 包中,然后将其复制到项目文件夹。
创建新的 Android 清单以将新活动设置为应用程序的入口点。将 AndroidManifest.xml 文件放在项目的 Assets/Plugins/Android 文件夹中。
以下是 UnityPlayerActivity 文件的示例:
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();
}
}
以下便是相应 AndroidManifest.xml 文件的内容:
<?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=".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>
2017–05–18 页面已发布但未经编辑审查
5.5 版中的更新功能
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.