A use-case for extending the custom Unity activity is to pass start-up command-line arguments to Unity. For information on the command-line arguments available, see Command-line arguments.
To specify start-up arguments for your Android application:
String UnityPlayerActivity.updateUnityCommandLineArguments(String cmdLine)
method.cmdLine
argument with your own start-up arguments then return the result.
Important: The cmdLine
argument can be an empty string or null so make sure your code handles these possible values.The following example shows how to specify start-up arguments to select the graphics API based on the current device:
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() {
// Google Pixel デバイスで Vulkan を使用
if (Build.MANUFACTURER.equals("Google") && Build.MODEL.startsWith("Pixel"))
return true;
else
return false;
}
private boolean preferES2() {
// Android 5.1 以前を使うデバイスで OpenGL ES 2.0 を使用
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; // Unity に Player 設定に基づくグラフィックス API を選択させます
}
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}