Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by HZ51 · Jun 17, 2017 at 11:19 AM · fpslimit

How to limit FPS?

According to this article: https://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html I've put this code:

 void Awake() {
     QualitySettings.vSyncCount = 1;
     Application.targetFrameRate = 30;
 }

into my only one camera's script. In the stats of the Game window I see 96-110 FPS. How to limit it to 30? I use the latest Unity on the latest MacOSX.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Xarbrough · Mar 02, 2018 at 02:46 PM

As others have mentioned, you disable vSync and set the targetFrameRate like this:

 using UnityEngine;
 
 public class SetTargetFrameRate : MonoBehaviour 
 {
     public int targetFrameRate = 30;
 
     private void Start()
     {
         QualitySettings.vSyncCount = 0;
         Application.targetFrameRate = targetFrameRate;
     }
 }

However, you have to test the result on device to see if it works. The documentation doesn't say anything about the game view render update in the editor, but it does say that the results are different per platform.

I've tested the code in the editor and it was working, but we need to understand that it's not a hard limit, but just a recommendation. When I set it to render at 30, I get results up to 40 frames per second displayed in the stats panel. This may be because the stats panel doesn't measure fps accurately or because the final frame rate may be a little higher on standalone. In any case, I would measure fps on device with one of the official profiling tools, e.g. Apple's XCode for iOS devices. There, the limit of 30 vs 60 fps works accurately (at least when I tested last).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
7

Answer by Brogan89 · Jun 17, 2017 at 12:15 PM

Why would you ever want to limit FPS?

EDIT To be more helpful, and actually provide an answer :)

  public class FPSTarget : Monobehaviour
  {
      
      public int target = 30;
      
      void Awake()
      {
          QualitySettings,vSyncCount = 0;
          Application.targetFrameRate = target;
      }
      
      void Update()
      {
          if(Application.targetFrameRate != target)
              Application.targetFrameRate = target;
      }
  }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HZ51 · Jun 17, 2017 at 12:47 PM 3
Share

To decrease battery usage on mobile devices if I don't need fps to be around 60. Or I can use dynamic FPS ins$$anonymous$$d.

avatar image VinnieH01 · Jun 17, 2017 at 12:47 PM 4
Share

This isn't an answer, please remove it and post it as a comment ins$$anonymous$$d :)

avatar image Brogan89 · Jun 17, 2017 at 11:28 PM 0
Share

yea my bad. Settings targetFrameRate should be they way though.

Do something like this.

 public class FPSTarget : $$anonymous$$onobehaviour
 {
     
     public int target = 30;
     
     void Awake()
     {
         QualitySettings,vSyncCount = 0;
         Application.targetFrameRate = target;
     }
     
     void Update()
     {
         if(Application.targetFrameRate != target)
             Application.targetFrameRate = target;
     }
 }


might be over kill checking every frame but it will work

avatar image BOLL7708 · Mar 02, 2018 at 11:40 AM 2
Share

As a side note, there is a comma on line 8 that should be a period. Other than that this works fine :) Cheers!

avatar image unity_D0627D6934FD85302027 · Dec 28, 2021 at 01:59 AM 0
Share

this surely worked for $$anonymous$$e I wanted to set the fps around 60 so I made the target FPS to 70

Show more comments
avatar image
5

Answer by Max_Bol · May 23, 2021 at 04:02 PM

For those who still wonders how the targetFrameRate works, it's not an absolute value.

There's a lot of information on the API description here.


For those who still don't get it, the value may be partially or fully ignored for multiple reasons. First, in case you're enabling VSync (above 0), the framerate will be ignoring the targetFrameRate value (as explained here) and will, instead, use the system default framerate. If your device has a default FPS of 120 with VSync at 1, it will try to render at 120 fps regardless of targetFrameRate. If you set VSync to 1-4 (max value applicable is 4), the fps will be equal to the device's default framerate divided by the VSync value. So, if you set it to 2 and your device has a default framerate of 120, it will be render at 60 fps. At the same time, if it's a default 30 fps, it will drop at a max of 15 fps.


PCs default framerates are determined by the GPU driver and can be accessed by the player/user through their GPU interface. You can't change it from within the game so, if you allow VSync in some options menu, it's a good practice to put some sort of notice/info panel when VSync is enabled to make the player know that he or she has to do the change while the game is not running. On mobile device, 95% of the devices doesn't allow the user to change the default framerate. Some manufacturers will allows it through the device's screen refresh rate in the devices' screen advanced settings. In the case of consoles (for those who wonders), both the Xbox X and S have default framerate of 60 while the PS4 is set to 60 and PS5 is set to 120. It's possible to make the Xbox X render at 120, but it requires a particular plugin and I'm not sure if it's compatible with the way Unity determines the device's default framerate.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

Answer by VinnieH01 · Jun 17, 2017 at 11:31 AM

Set the vSyncCount to 0

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HZ51 · Jun 17, 2017 at 11:53 AM 1
Share

Unfortunately it doesn't work. QualitySettings.vSyncCount = 0; Application.targetFrameRate = 60; gives me 90-95 fps

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

targetFrameRate & vSync for iOS 3 Answers

How to limit FPS on non-retina devices? 0 Answers

One scipt to cover all guns 1 Answer

Raycast Shooting 1 Answer

FPS audio problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges