要扩展默认的 Unity 活动,您可以创建自己的自定义活动并将其设置为应用程序的入口点。执行此操作的过程如下:
创建一个扩展 UnityPlayerActivity 类的新活动。
注意:如果您使用的是 GameActivity 应用程序入口点创建新活动,则需要扩展 UnityPlayerGameActivity 类。确保根据在 Player 设置中设置的应用程序入口点扩展正确的类。
创建一个插件以将新活动传递给最终的 Unity Android 应用程序。
通过覆盖 Android App Manifest,将您的新 activity 设置为应用程序的入口点。
完成此操作后,您可以在练习中实现自定义行为,以控制 Unity 和 Android 之间的交互。
要创建新活动,请执行以下操作:
在 Assets 文件夹中,创建新的 Java (.java) 或 Kotlin (.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 应用程序入口点覆盖多个函数的示例活动。
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) 插件,以便在多个项目中复用或分发给其他开发者。
创建插件后,将活动文件添加到其中。
创建活动并将其添加到插件后,您可以将其设置为应用程序入口点。为实现这一点,请修改 Android 清单并将活动元素的 name 属性设置为自定义活动的类名称。
以下代码示例展示了如何执行此操作:
<?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>