Version: 2022.1
Create a custom activity
将 Unity 集成到 Android 应用程序中

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() {
        // 在 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);
    }
}
Create a custom activity
将 Unity 集成到 Android 应用程序中
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961