Version: 2022.2
Language : English
Create a custom activity
Integrating Unity into Android applications

Specify Unity start-up arguments

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:

  1. Create a custom activity and set it as the application entry point.
  2. In the custom activity, override the String UnityPlayerActivity.updateUnityCommandLineArguments(String cmdLine) method.
  3. In the method, concatenate the 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() {
        // 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);
    }
}
Create a custom activity
Integrating Unity into Android applications