# カスタムアクティビティの作成

> Extend the default Unity activity to control interactions between Unity and Android.

Unity のデフォルトのアクティビティを拡張するには、独自のカスタムアクティビティを作成し、それをアプリケーションのエントリーポイントに設定します。そのためのプロセスは以下の通りです。

1. `UnityPlayerActivity` クラスを拡張する新しいアクティビティを作成します。

   **ノート**: GameActivity アプリケーションエントリーポイントを使用して新しいアクティビティを作成する場合は、`UnityPlayerGameActivity` クラスを拡張する必要があります。[Player 設定](/engine/6000.0/manual/platform-specific/android/getting-started/class-player-settings.md) で設定したアプリケーションエントリーポイントに従って、正しいクラスを拡張していることを確認します。

2. 新しいアクティビティを最終的な Unity Android アプリケーションに配信するプラグインを作成します。

3. [Android アプリマニフェスト](/engine/6000.0/manual/platform-specific/android/introducing/manifest.md) をオーバーライドして、新しい `activity` をアプリケーションのエントリーポイントとして設定します。

この後、アクティビティにカスタムビヘイビアを実装して、Unity と Android の間の相互作用を制御できます。

## 新しいアクティビティの作成##create-a-new-activity

新しいアクティビティを作成する手順は以下のとおりです。

1. **Assets** フォルダーに、新しい Java (`.java`) または Kotlin (`.kt`) ファイルを作成します。

2. 新しいファイルでは、`UnityPlayerActivity` を拡張するクラスを作成します。

   **ノート**: GameActivity アプリケーションエントリーポイントを使用する場合は、`UnityPlayerGameActivity` クラスを拡張するクラスを作成する必要があります。

3. 新しいクラスでは、アクティビティに持たせたいカスタム動作を実装するために、さまざまな基本アクティビティメソッドをオーバーライドします。詳細については、Android の [Activity](https://developer.android.com/reference/android/app/Activity.html#summary) ドキュメントを参照してください。

### スクリプトサンプル##example-activity

次のコードサンプルは、複数の関数をオーバーライドするアクティビティの例を示しています。

```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) {
    // 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 アプリケーションエントリーポイントを使用したアクティビティの例##example-activity-with-gameactivity-application-entry-point

次のコードサンプルは、複数の関数をオーバーライドする、GameActivity アプリケーションエントリーポイントを使用したアクティビティの例を示しています。

```java
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();
  }
}
```

## アクティビティのプラグインを作成##create-a-plug-in-for-the-activity

Unity Android アプリケーションのカスタムアクティビティを使用するには、アクティビティを含むプラグインを作成する必要があります。アクティビティは Java または Kotlin で記述されているため、以下のいずれかのタイプのプラグインを使用する必要があります。

* [Android Library プロジェクト/Android Archive プラグイン](/engine/6000.0/manual/platform-specific/android/developing/plugins-for/plugin-types/aarplugins.md)
* [JAR プラグイン](/engine/6000.0/manual/platform-specific/android/developing/plugins-for/plugin-types/jarplugins.md)
* [Java と Kotlin のソースプラグイン](/engine/6000.0/manual/platform-specific/android/developing/plugins-for/plugin-types/java-source-plugins.md)

1 つのプロジェクトにカスタムアクティビティを作成する場合は、Java と Kotlin のソースプラグインを使用します。ソースプラグインを作成するには、プロジェクトの **Assets** フォルダーにソースファイルを直接配置します。

アクティビティを複数のプロジェクトで再利用したり、他の人に配布したい場合は、Android Archive (AAR) または JAR プラグインを使用してください。1 つの AAR または JAR プラグインファイルを管理することで、複数のプロジェクトに機能を提供しやすくなります。

プラグインを開発している間は Android Library プロジェクトを使用し、実装が完了し複数のプロジェクトで使用するとき、または他の人に配布するときは Android Archive プラグインにコンパイルします。

プラグインを作成したら、アクティビティファイルを加えます。

## 新しいアクティビティをアプリケーションのエントリーポイントに設定##set-the-new-activity-as-the-application-entry-point

アクティビティを作成してプラグインに加えたら、そのアクティビティをアプリケーションのエントリーポイントとして設定します。これを行うには、[Android マニフェストを変更](/engine/6000.0/manual/platform-specific/android/building-and-delivering/modify-gradle-project-files.md) し、[アクティビティ要素](https://developer.android.com/guide/topics/manifest/activity-element) の `name` 属性をカスタムアクティビティのクラス名に設定します。

以下の Android マニフェストの例は、この方法を示しています：

```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="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
<?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>
```
