Search Unity

Set the target frame rate to 60 on android

Discussion in 'Android' started by Gonzasmpl, Aug 1, 2019.

  1. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Hi, i been trying to set the target frame rate to 60 but it seems imposible. i'm aware that vSync must be deactivated and indeed i'm disabling it through the editor and code but i can't get it to work.
    I'm running this code:
    Code (CSharp):
    1.     private void Start()
    2.     {
    3.         QualitySettings.vSyncCount = 0;
    4.         Application.targetFrameRate = 60;
    5.     }
    The strange thing is that the fps counter gives its max at the very beggining and it has a relation with the target frame rate. For instance, if i set it to 60 frames, it gives 60 frames but goes back to 30 a frame after the application launchs. If i set it to be the maximum posible it gives a number arround 250 and back to 30.

    If you know what can be the problem here or have any suggestion i would be grateful.
    Thanks!
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,174
    Hi!
    This looks correct.
    How do you count FPS?
     
  3. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    I made my own counter, then i download graphy from the assets store, mainly to contrast both results and indeed both were giving the same number.
    This happened from build to build as i made tests builds periodically. In previous builds i was getting 60 frames per second and in the last one it gets capped to 30 haven't done any significant change.
    So i conclude i might have change something in projects settings or so, but i don't remember me tweaking some parameter. After checking everything seems ok.
    I finally came to a solution creating a new project and importing all my assets to this new project, it took 15 minutes or so because almost everything gets created on the go. I'm a little afraid this happening again in this "new project" though i can keep creating new projects if it's the case haha, hopefully not.
     
  4. werner002timo

    werner002timo

    Joined:
    Mar 20, 2020
    Posts:
    2
    same issue man
     
  5. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    25
    Yeah, i have the same issue too. At first, i though it was because another script unintentionally used either the vsync or the targetframerate line without knowing them that limited the framerate, but after several searches and changes, it was still locked to 30fps. Some suggest attaching the script that uses the targateframerate line to the camera, but i haven't tested it yet, so i can't confirm for now.

    And if anyone asks, for the frame rate measurement, i used both the remote profiling and a Frame Rate Counter that is included in the TextMeshPro examples.

    Update: Something really strange happened. I asked some to test the app on their phones to check performance and, surprisingly, all of them run the app on a (almost) consistent 60fps, while i was the only one that run it at 30 fps, which puzzled me heavily. I suggest check your own phone and see if there is a setting that is limiting the performance of your game (normally to preserve battery life, i'm still searching what's that setting on my phone).

    2nd Update: Try moving the code from Start() to Awake(). It worked for me at least. Also, disable Optimize Frame Pacing, since that also limits your Frame Rate (depending of your phone).
     
    Last edited: Aug 2, 2021
  6. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    Hi, Aleksandrk! I want to know if there is a default target framerate for Android since unity 2020. I just updated my editor to Unity 2020. 2 and all my Android projects default frame rate is 60. Is it normal?
     
    soleron likes this.
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,174
    @Achie1 and what was the frame rate before the update?
     
  8. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    It alternates between 150 FPS and 114 FPS.
    I made a simple test. I created a new project in which there is the Unity Sample Scene. That's the same framerate.
     

    Attached Files:

    Last edited: Feb 7, 2021
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,174
    Are you measuring this on a device or in the Editor?
     
  10. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    On the Editor
     
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,174
    Please measure on a device. Editor performance will mostly like be very different and is not indicative of performance on the device.
     
    unity_ye0nFU97f0spdQ and Achie1 like this.
  12. Achie1

    Achie1

    Joined:
    Jan 14, 2018
    Posts:
    26
    OK. I will
     
  13. davidgorenc10

    davidgorenc10

    Joined:
    May 16, 2020
    Posts:
    1
    Omg, I didn't know you could literally set framerate to 60 with code. Thank you sooooo much I was soo frustrated that my game was stuck in 30 fps.
     
  14. lanyerataca21

    lanyerataca21

    Joined:
    May 31, 2020
    Posts:
    1
    i get this solution for this problem feel free to use, just add an empty object in your scene and put the code, then just change the variable TargetFrameRate.

    -----------------------CODE---------------------------

    using System.Collections;
    using System.Threading;
    using UnityEngine;
    public class FrameRateManager : MonoBehaviour
    {
    [Header("Frame Settings")]
    int MaxRate = 9999;
    public float TargetFrameRate = 60.0f;
    float currentFrameTime;
    void Awake()
    {
    QualitySettings.vSyncCount = 0;
    Application.targetFrameRate = MaxRate;
    currentFrameTime = Time.realtimeSinceStartup;
    StartCoroutine("WaitForNextFrame");
    }
    IEnumerator WaitForNextFrame()
    {
    while (true)
    {
    yield return new WaitForEndOfFrame();
    currentFrameTime += 1.0f / TargetFrameRate;
    var t = Time.realtimeSinceStartup;
    var sleepTime = currentFrameTime - t - 0.01f;
    if (sleepTime > 0)
    Thread.Sleep((int)(sleepTime * 1000));
    while (t < currentFrameTime)
    t = Time.realtimeSinceStartup;
    }
    }
    }
     

    Attached Files:

unityunity