이 페이지는 다음의 섹션으로 구성됩니다.
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 문서를 참조하십시오.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:
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:
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 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.
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);
}
}
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: