Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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,983
    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:
    60
    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,983
    @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,983
    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,983
    Please measure on a device. Editor performance will mostly like be very different and is not indicative of performance on the device.
     
    antonio_iliev 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:

  15. JuaanPB

    JuaanPB

    Joined:
    Jun 22, 2021
    Posts:
    1
    Thank you, works perfectly.
     
  16. unity_Nlu_2cJ-yGFJ6w

    unity_Nlu_2cJ-yGFJ6w

    Joined:
    Jan 6, 2022
    Posts:
    1
    THANK YOU MAN YOU'RE A GOOOOOD, works perfectly on android love ya man
     
  17. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    Hi, does this script somehow enhances the frame performances compared to if I just edited the 2 settings that everyone is talking about
    1. QualitySettings.vSyncCount = 0;
    2. Application.targetFrameRate = 60;

    I am curious by how is it working perfectly compared to the standard approach.

    Thank you.
     
  18. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Sometimes there are issues on android if you do stuff in awake in the first scene (I guess some native android stuff aren't ready and the calls don't work), so that script was re-setting those values after awake.
     
    Brother_77 likes this.
  19. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    Interesting, but it creates a while infinite loop that keeps setting the frames ? Wouldn't that interrupt the Unity's built in frame rate calculation? (Sorry if this is stupid, just have a bad feeling about this :D)

    Also, you said "do stuff in awake in the first scene", so I would need this script only in the scene with build index 0 ?

    Thank you.
     
  20. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    I wouldn’t use that script, it seems overkill to me.
     
  21. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    If you have time what would be the good solution to this problem ? The more I read about it the more confused i feel.

    Would using the simple steps of
    1. QualitySettings.vSyncCount = 0;
    2. Application.targetFrameRate = 60;
    be enough ?
     
  22. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    That is the actual solution. But there are other elements that affect depending of the project:
    • Optimize Frame Pacing: if it's enabled, those lines may not work properly depending on the device.
    • Whether these lines are on Start() or Awake().
     
    Brother_77 likes this.
  23. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    The solution we have in our project is to set this as part of the init of our post effects script, which runs OnEnable on every scene and we just do.
    Code (csharp):
    1.  
    2.         if  (Quality.limit30fps) Application.targetFrameRate = 30;
    3.         else                     Application.targetFrameRate = highFPSRate;
    (Quality is our own settings class, it's not Unity's built-in Quality settings)
     
    Brother_77 likes this.
  24. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
    Thank you for the insight, and how is highFPSRate variable value decided ?
     
  25. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    It's 60 by default and 120 on iOS. We probably need to rework it a bit.
     
    Brother_77 likes this.
  26. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    456
    Wasn't it 30 FPS by default? A last build without forcing the framerate at 60 felt laggy, 30 FPS laggy. That's 2019.14 LTS though, perhaps things have been pushed further on newer versions of Unity?
     
  27. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    I am talking about the highfps variable in my own script which two lines of I posted above.

    Application targetframerate is 30 by default on mobiles, yeah.
     
  28. link-kaian

    link-kaian

    Joined:
    Mar 11, 2015
    Posts:
    1
    Fiz uma build com uma cena vazia no unity 2019
    E sem unidade 2021.

    Unidade 2019 = 60 FPS
    Unidade 2021 = 20 FPS

    Eu parece que a unidade 2019. Está melhor para criação de jogos mobile.
     
    leni8ec and mfakkaya like this.
  29. Nileshk1080

    Nileshk1080

    Joined:
    Jul 4, 2022
    Posts:
    3
    Thank you so much you make my life better
     
  30. mazen1243

    mazen1243

    Joined:
    Apr 26, 2021
    Posts:
    2
    MY ADVICE DONT USE TERRAIN IN PHONE GAME
     
    Alvarden likes this.
  31. Alvarden

    Alvarden

    Joined:
    Feb 22, 2019
    Posts:
    60
    I'm with you. I wish there was an optimized version of Terrain to use it on phone games.
     
  32. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Here is how framerate is decided for our games now (we want to allow limit to 30, 60 and unlimited since a lot of devices support over 60fps these days)

    Code (csharp):
    1. // Quality is our own settings stuff
    2. if      (Quality.framerate == 0) {
    3.     Application.targetFrameRate = 30;
    4. }
    5. else if (Quality.framerate == 1) {
    6.     Application.targetFrameRate = 60;
    7. }
    8. else {
    9.     #if UNITY_ANDROID
    10.     Resolution[] allResolutions = Screen.resolutions;
    11.     int maxRefresh = 60;
    12.     for (int i = 0; i < allResolutions.Length; i++) {
    13.         maxRefresh = max(maxRefresh, allResolutions[i].refreshRate);
    14.     }
    15.     int highFPSRate = maxRefresh;
    16.      
    17.     #else
    18.  
    19.    // This works for iOS but maybe the Android solution is more robust?
    20.     int highFPSRate = Screen.currentResolution.refreshRate;
    21.     #endif
    22.  
    23.    // Clamp just in case bogus values are returned.
    24.     highFPSRate = clamp(highFPSRate, 60, 300);
    25.  
    26.     Application.targetFrameRate = highFPSRate;
    27. }
     
    Last edited: Dec 30, 2022
    Brother_77 likes this.
  33. mavv

    mavv

    Joined:
    Feb 4, 2014
    Posts:
    29
    So 30 & 60 are safe values? All android devices should be having it? Can i hardcode it?
    I tried to list refresh rates by
    Code (CSharp):
    1.  Resolution[] allResolutions = Screen.resolutions;
    and result was interesting, because there are 30, 48, 50, 60, 90, 96, 120. I want to create option, that user decide which framerate he wants to use, of course not all of them. Personally I thought about 30, 60 and max (if device is capable of)
    But there is another problem. I disabled 60+ fps in system configuration, but how I can know that in Unity game? This option should be not active then. What happen if user still wants to use it?
     
  34. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    "All Android devices" is a big ask, there probably exist some super weird android devices, but "almost all" sounds about right.
    We "solved" the issue by calling the option "FPS Limit" and then the options for it: 30 60 and Off. Presumably if you have disabled higher than 60 refresh rates, they also won't be included in the returned array? (although I'm guessing here)
     
  35. mavv

    mavv

    Joined:
    Feb 4, 2014
    Posts:
    29
    They are included, that's why i'm asking and be confused :)
    In android preferences I have disabled 120Hz, only 60. But Unity listed 90 and 120 anyway.
     
  36. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    F*** my life. Setting Application.targetFrameRate to one higher than what you want can make a difference.

    On a 60hz screen in Update():
    Application.targetFrameRate = 60
    will bounce between 59/60.
    Application.targetFrameRate = 61
    will lock it to 60.
    Application.targetFrameRate = 90
    will lock it to 60.

    I hate Unity.
     
    Last edited: Mar 23, 2023
    Ryiah likes this.
  37. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Ugh...

    Is this with optimize frame pacing on or off?
     
  38. Brother_77

    Brother_77

    Joined:
    Feb 8, 2019
    Posts:
    233
  39. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    Off.

    On: Multithreading, Graphics Jobs, IL2CPP ARM64, Vulkan, Static Batching.
    Off: Incremental GC, Frame Pacing

    I have been having some major frame-rate issues in a basic scene, but I'm trying to figure out if it's the Linear/Post Stack/Standard shader, or a combination of these Player settings.
     
  40. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    In the couple of Android devices we have access to it seems to work ok (they are both Pixel devices though...), I don't think we have a released build that has the setting on though.
     
    Brother_77 likes this.
  41. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Thanks.

    Graphics Jobs eh? It used to be a minefield of issues, is that one working okay for you?
     
  42. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,910
    I'll do more testing if the FPS dips again. I had that enabled in an older version of the project (this project is a ground-up remake) and it was fine, although multithreaded was disabled.
     
    AcidArrow likes this.
  43. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    456
    Just out of curiosity, I suppose you do the tests on real Pixel devices, not simulators?
     
  44. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    Not sure if you were asking me, but we have a Pixel 6 Pro and a Pixel 3 that we test stuff on.
     
  45. Starbox

    Starbox

    Joined:
    Sep 17, 2014
    Posts:
    456
    Yup, that was meant for you. A year ago, the bots that logged results in the report for one the releases specifically pulled cases from Pixel devices that were experiencing such major issues.
    Considering how the Pixel devices represented a very small niche in the Android market from what I had found back then, I simply filtered them out instead of taking the risk of having someone downloading the app and then complaining that it runs terribly on his device.
     
    Last edited: Mar 4, 2024
  46. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,631
    We like Pixel devices for testing, because we can get OS updates faster than all other devices and sometimes we can get in front of issues that new Android OS versions introduce.

    We don't have issues with optimised frame pacing on a pixel 6 pro, but our game can't quite stress the Pixel 6, so that could be why we're not seeing a lot of issues.
     
  47. NoobGeek

    NoobGeek

    Joined:
    Mar 2, 2021
    Posts:
    2
    Thanks a lot for the tip to disable Optimize Frame Pacing. It took me 2 months to figure out what the problem was. I created a TargetFrameRate script that ran every time the game started. I displayed the fps value on the screen. The game was giving 60 fps, but the problem was that it actually felt like 30 fps. I was even more puzzled when the problem was solved by minimizing the game once. Apparently the Optimize Frame Pacing option sets the frame rate value when the game starts and blocks the targetFrameRate setting. Very strange behavior, but I'm glad I managed to fix this problem thanks to you. I will probably make a separate post about this problem later, there is very little information about it on the internet
     
  48. ATHARV1705

    ATHARV1705

    Joined:
    Oct 16, 2021
    Posts:
    1
    thanks a lot it worked perfectly :)