创建自定义活动
Extend the default Unity activity to control interactions between Unity and Android.
阅读时间6 分钟最后更新于 1 个月前
要扩展默认的 Unity 活动,您可以创建自己的自定义活动并将其设置为应用程序的入口点。执行此操作的过程如下:
-
创建一个扩展类的新活动。
UnityPlayerActivity注意:如果您使用的是 GameActivity 应用程序入口点创建新活动,则需要扩展类。确保根据在 Player 设置中设置的应用程序入口点扩展正确的类。UnityPlayerGameActivity -
创建一个插件以将新活动传递给最终的 Unity Android 应用程序。
-
通过覆盖 Android App Manifest,将您的新设置为应用程序的入口点。
activity
完成此操作后,您可以在练习中实现自定义行为,以控制 Unity 和 Android 之间的交互。
创建新活动
要创建新活动,请执行以下操作:
-
在 Assets 文件夹中,创建新的 Java () 或 Kotlin (
.java) 文件。.kt -
在新文件中,创建一个扩展的类。
UnityPlayerActivity注意:如果您使用的是 GameActivity 应用程序入口点,则需要创建一个扩展类的类。UnityPlayerGameActivity -
在新类中,重写各种基础活动的各种方法,以实现您希望的自定义行为。有关更多信息,请参阅 Android 的 Activity 文档。
示例活动
以下代码示例显示了覆盖多个函数的示例活动。
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) { // Calls UnityPlayerActivity.onCreate() super.onCreate(savedInstanceState); // Prints debug message to Logcat Log.d("OverrideActivity", "onCreate called!"); } public void onBackPressed() { // Instead of calling UnityPlayerActivity.onBackPressed(), this example ignores the back button event // super.onBackPressed(); }}
使用 GameActivity 应用程序入口点的示例活动
以下代码示例显示了 GameActivity 应用程序入口点覆盖多个函数的示例活动。
package com.company.product;import com.unity3d.player.UnityPlayerGameActivity;import android.os.Bundle;import android.util.Log;public class OverrideExample extends UnityPlayerGameActivity { protected void onCreate(Bundle savedInstanceState) { // Calls UnityPlayerGameActivity.onCreate() super.onCreate(savedInstanceState); // Prints debug message to Logcat Log.d("OverrideActivity", "onCreate called!"); } public void onBackPressed() { // Instead of calling UnityPlayerGameActivity.onBackPressed(), this example ignores the back button event // super.onBackPressed(); }}
为活动创建插件
要将自定义活动用于 Unity Android 应用程序,您需要创建一个插件来包含该活动。活动可以用 Java 或 Kotlin 编写,因此您需要选择以下类型的插件之一:
如果要为单个项目创建自定义活动,请使用 Java 和 Kotlin 源代码插件。要创建源插件,请将源文件直接放在项目的 Assets 文件夹中。
如果要在多个项目中重用该活动或将其分发给其他人,请使用 Android Archive (AAR) 或 JAR 插件。管理一个 AAR 或 JAR 插件文件可以更方便地在多个项目中交付功能。
在开发自定义插件时,建议使用 Android Library Project。完成开发后,可将其编译为 Android Archive (AAR) 插件,以便在多个项目中复用或分发给其他开发者。
创建插件后,将活动文件添加到其中。
将新活动设置为应用程序入口点
以下代码示例展示了如何执行此操作:
<?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:theme="@style/UnityThemeSelector" 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>
以下 Android 清单示例展示了如何使用 GameActivity 应用程序入口点对自定义活动执行此操作:
<?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:theme="@style/BaseUnityGameActivityTheme" 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> <meta-data android:name="android.app.lib_name" android:value="game" /> </activity> </application></manifest>