Version: 2020.3
JAR 플러그인
Android용 Native(C++) 플러그인

UnityPlayerActivity Java 코드의 확장

이 페이지는 다음의 섹션으로 구성됩니다.

UnityPlayerActivity 파일 확장

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 문서를 참조하십시오.
  • Android 매니페스트를 수정하여 새로운 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:

  1. 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:

    • Put the .java or .kt file that contains your activity class directly into Unity project By default, the file is located at:

    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 Java library that contains your class, compile it and put the resulting .jar file into your Unity project
    • Create an Android library that contains your class. You can then put this library into your Unity project in your source code. To do this, give your folder a name with an .androidlib extension, or it can be compile it and put the resulting .aar file in your 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.

Specifying Unity startup arguments from a custom UnityPlayerActivity file

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() {
        // Use Vulkan on Google Pixel devices
        if (Build.MANUFACTURER.equals("Google") && Build.MODEL.startsWith("Pixel"))
            return true;
        else
            return false;
    }

    private boolean preferES2() {
        // Use OpenGL ES 2.0 on devices that run Android 5.1 or older
        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; // let Unity pick the Graphics API based on PlayerSettings
    }

    @Override protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }
}

UnityPlayerActivity 예제 파일

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) {
    // call UnityPlayerActivity.onCreate()
    super.onCreate(savedInstanceState);
    // print debug message to logcat
    Log.d("OverrideActivity", "onCreate called!");
  }
  public void onBackPressed()
  {
    // instead of calling UnityPlayerActivity.onBackPressed() we just ignore the back button event
    // 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.

  • 2019.2에서 새로운 코드 샘플 추가
  • Unity 2019.3 버전 이상을 위한 AndroidManifest 예제 업데이트함
JAR 플러그인
Android용 Native(C++) 플러그인
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961