How To Select The Graphics API Based On The Current Android Device Through Custom UnityPlayerActivity File

With more and more Android phones supporting both GLES and Vulkan, mobile game developers often face a problem, can we select which Graphics API to use when the game is launched on a specific device?

The short answer is YES.

Let’s Select the Right Graphics API At Runtime

After Unity 2018.4, Unity provides the function of adding command-line arguments in UnityPlayerActivity. You can add command-line arguments. such as -force-gles20, -force-gles30, -force-gles31, -force-gles31aep and -force-vulkan to force use the selected Graphics API.

You can export the project and open it in Android Studio, then modify the UnityPlayerActivity.java file. There is an example:

String deviceName = android.os.Build.MODEL;

// Setup activity layout
@Override protected void onCreate(Bundle savedInstanceState)
{
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);

    if(deviceName.equals("SM-C9000"))
    {
        this.getIntent().putExtra("unity", "-force-gles30");
    }

    mUnityPlayer = new UnityPlayer(this);
    setContentView(mUnityPlayer);
    mUnityPlayer.requestFocus();
}

Here, I use the relatively old Samsung Device SM-C9000. And build the project with Vulkan and OpenGLES graphics API. I want to select the relatively old Graphics API on this old device, so I set the command-line argument to use the OpenGLES graphics API.

Then, as a result, our game will run on this SM-C9000 device using OpenGLES 3.0. On the other hand, it will use Vulkan when runs on other Android devices.

The Bug & The Fix

Yes, when this feature was first released, there was a bug there(Sound familiar 🙂).

This bug is easy to describe, that is, this function is only effective in the development build.

Simply put, I tested it in Unity2018.4.5 and found that the Graphic API can be selected for the development build. But it does not take effect for the release build.

As you can see, the release build of the same project and the select graphics API command-line argument does not work. It selected the default one, that is, Vulkan.

And the ID of this Bug is 1195061. The good news is this bug is fixed in Unity 2018.4.16f1. YEAH!

Now you can select your desire Graphics API at run-time on Android devices.


Subscribe To Jiadong Chen's Blog

Avatar
Jiadong Chen
Cloud Architect/Senior Developer

Cloud Architect at Company-X | Microsoft MVP, MCT | Azure Certified Solutions Architect & Cybersecurity Architect Expert | Member of .NET Foundation | Packt Author ㅣ Opinions = my own.

comments powered by Disqus

Related