Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
8
Question by e-bonneville · Feb 08, 2011 at 04:26 PM · framerate

How do I find the frames per second of my game?

Would I use Time.deltaTime somehow? I'd like to find the frames per second of my game in a standalone build, so I can't just look at "Stats" in the Editor.

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

6 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

Answer by taoa · Feb 08, 2011 at 04:33 PM

display the value

1.0f / Time.deltaTime

It'll give you a good approximation of your FPS.

However this will only tell you how many frames per second your frame was running at during the last frame! Consequently, this is very sensitive with even tiny variations in the time it took to render last frame.

Another popular method that might be more accurate, would be to know what was the framerate over a fixed amount of time.

Decide upon this amount of time, and just count how many frames have been rendered during that time:

//Declare these in your class int m_frameCounter = 0; float m_timeCounter = 0.0f; float m_lastFramerate = 0.0f; public float m_refreshTime = 0.5f;

void Update() { if( m_timeCounter < m_refreshTime ) { m_timeCounter += Time.deltaTime; m_frameCounter++; } else { //This code will break if you set your m_refreshTime to 0, which makes no sense. m_lastFramerate = (float)m_frameCounter/m_timeCounter; m_frameCounter = 0; m_timeCounter = 0.0f; } }

then display somewhere the value

m_lastFramerate

which will be the average framerate over the refresh time you'd have chosen (here 0.5 second)

Comment
Add comment · Show 2 · 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 e-bonneville · Feb 08, 2011 at 04:38 PM 0
Share

Thanks, that did the trick.

avatar image $$anonymous$$ · Apr 05, 2014 at 10:51 PM 0
Share

It doesn't seem to be giving me accurate results... Is the code outdated or something. I Entered the code exactly as it is and hooked it up to a .Text$$anonymous$$esh but i'm getting odd results. Nothing incredable extraordinary, but, just inaccurate...

avatar image
16

Answer by ZeroKcm · Aug 01, 2013 at 09:01 PM

More simple:

 void OnGUI()
 {
     GUI.Label(new Rect(0, 0, 100, 100), (int)(1.0f / Time.smoothDeltaTime));        
 }
 
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
6

Answer by TimCoster · Feb 25, 2019 at 01:20 PM

Old topic, but there is a FPSCounter script in the unity standard assets utility folder..

 using System;
 using UnityEngine;
 using UnityEngine.UI;
 
 namespace UnityStandardAssets.Utility
 {
     [RequireComponent(typeof (Text))]
     public class FPSCounter : MonoBehaviour
     {
         const float fpsMeasurePeriod = 0.5f;
         private int m_FpsAccumulator = 0;
         private float m_FpsNextPeriod = 0;
         private int m_CurrentFps;
         const string display = "{0} FPS";
         private Text m_Text;
 
 
         private void Start()
         {
             m_FpsNextPeriod = Time.realtimeSinceStartup + fpsMeasurePeriod;
             m_Text = GetComponent<Text>();
         }
 
 
         private void Update()
         {
             // measure average frames per second
             m_FpsAccumulator++;
             if (Time.realtimeSinceStartup > m_FpsNextPeriod)
             {
                 m_CurrentFps = (int) (m_FpsAccumulator/fpsMeasurePeriod);
                 m_FpsAccumulator = 0;
                 m_FpsNextPeriod += fpsMeasurePeriod;
                 m_Text.text = string.Format(display, m_CurrentFps);
             }
         }
     }
 }
 
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
1

Answer by Rodaraujo · Mar 12, 2013 at 11:15 PM

wouldn't Debug.Log(1/Time.deltaTime); be enough?

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 robertbu · Mar 13, 2013 at 12:08 AM 1
Share

deltaTime can vary a great deal, so the numbers can be all over the place...especially if you have an app under load. I calculate an average similar to above, and I also use Time.smoothDeltaTime for my fps script. Debug.Log() outputting to the console will also slow the app, so it is better to output to the screen.

avatar image Rodaraujo · Mar 13, 2013 at 12:26 AM 1
Share

Oh ok Thanks!

avatar image Nikola Keranovic · Oct 28, 2015 at 04:56 PM -2
Share

Yes maybe, but if you use fixedupdate it should stay on average Time. DeltaTime

avatar image tanoshimi · Oct 28, 2015 at 06:29 PM 0
Share

If you use FixedUpdate() you're not measuring the framerate of your game; you're measuring your physics timestep (which is wholly controllable anyway)....

avatar image Nikola Keranovic tanoshimi · Oct 28, 2015 at 06:38 PM 0
Share

I actually didn't know that. So that means that if I push timestep with FixedUpdatethat some processors on android cant keep up it will have some hick ups?

avatar image tanoshimi Nikola Keranovic · Oct 28, 2015 at 06:49 PM 0
Share

This thread gives a pretty good explanation: http://forum.unity3d.com/threads/the-truth-about-fixedupdate.231637/

avatar image
1

Answer by emmettbebop · May 27, 2021 at 09:01 PM

This is similar to Zero's answer but has smoothing. Less code than the other smoothed examples:

 private float fps = 30f;
 
 void OnGUI()
 {
         float newFPS = 1.0f / Time.smoothDeltaTime;
         fps = Mathf.Lerp(fps, newFPS, 0.0005f);
         GUI.Label(new Rect(0, 0, 100, 100), "FPS: " + ((int)fps).ToString());
 }

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 Fattie · Apr 04 at 04:45 PM 0
Share

FYI perfect code, but, I wouldn't use smoothDeltaTime there, it won't give you what you're after; simply DeltaTime is fine

  • 1
  • 2
  • ›

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

7 People are following this question.

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

Related Questions

General Ways to Optimize Frame Rate? 7 Answers

AddForceAtPosition not working? 1 Answer

iOS VSync always active? 1 Answer

Animations causing big Performance Drop 1 Answer

Framerate Independence for 2D Platformer 4 Answers


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