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 根据 PlayerSettings 选择图形 API
}
@Override protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}