Set screen refresh rate on Android 11

Try this:

1. Create file “MainActivity.java” in Assets folder.
8164832--1062107--upload_2022-5-29_13-19-27.png

2. Open this file and paste code:

package com.*YOUR COMPANY NAME*.*YOUR PRODUCT NAME*;
import com.unity3d.player.UnityPlayerActivity;
import android.os.Bundle;
import android.os.Build;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends UnityPlayerActivity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            Window w = getWindow();
            WindowManager.LayoutParams p = w.getAttributes();
            Display.Mode[] modes = getDisplay().getSupportedModes();
            //find display mode with max hz
            int maxMode = 0;
            float maxHZ = 60f;
            for(Display.Mode m:modes) {
                if (maxHZ < m.getRefreshRate()) {
                    maxHZ = m.getRefreshRate();
                    maxMode = m.getModeId();
                }
            }
            p.preferredDisplayModeId = maxMode;
            w.setAttributes(p);
        }
  }
}

3. Change YOUR COMPANY NAME and YOUR PRODUCT NAME to Company Name and Product Name from Project Settings → Player.
8164832--1062113--upload_2022-5-29_13-22-45.png
4. Go to Project Settings → Player → Publishing Settings. Tick “Custom Main Manifest”.
8164832--1062116--upload_2022-5-29_13-24-17.png

5. Open file “AndroidManifest.xml” in “*Assets\Plugins\Android*”.
6. Change android:name=“com.unity3d.player.UnityPlayerActivity” to android:name=“com.YOUR COMPANY NAME.YOUR PRODUCT NAME.MainActivity”.

7. Be sure to call “Application.targetFrameRate = Screen.currentResolution.refreshRate;” in any script!
Update: fixed bug in Android 12.

2 Likes