- Home /
How to limit frame rate in Unity Editor
Is there any way I can limit the framerate of my game in Unity Editor?
Answer by ScroodgeM · Aug 12, 2012 at 10:24 PM
http://docs.unity3d.com/Documentation/ScriptReference/Application-targetFrameRate.html
http://docs.unity3d.com/Documentation/ScriptReference/QualitySettings-vSyncCount.html
Thanks Scroodge, it worked for me.
the Application-targetFrameRate document said "targetFrameRate is ignored in the editor." is incorrect.
hmm 8) often used targetFrameRate in editor and just now noticed that it should not work 8)
mark answer as correct to close question
This one is worked for me. $$anonymous$$y situation is , physics in editor is not same in stand alone built. So I try to find fixing the frame rate. I changed target frame rate to 60 and my problem is solved for now. I didnt test it in other devices, or frame drop situations.
Note that this will only work when VSYNC is disabled in editor :)
Application.targetFrameRate does not work for me, even with VSync off.
Answer by Huacanacha · Oct 21, 2013 at 11:18 PM
There are two ways to limit the frame rate of your game. Both work in the Game view of the Unity Editor:
void Awake () {
QualitySettings.vSyncCount = 0; // VSync must be disabled
Application.targetFrameRate = 45;
}
Or:
void Awake () {
// 0 for no sync, 1 for panel refresh rate, 2 for 1/2 panel rate
QualitySettings.vSyncCount = 2;
}
To limit the rate ONLY when playing via the Unity editor:
void Awake () {
#if UNITY_EDITOR
QualitySettings.vSyncCount = 0; // VSync must be disabled
Application.targetFrameRate = 45;
#endif
}
Here is a brief explanation of each option:
Set it to any value you desire, in frames per second. Not a guaranteed but 'best effort' depending on the hardware capabilities etc.
VSync must be disabled for this to work (set vSyncCount to 0)
Set it to 0,1 or 2 only... this is the number of screen/panel refreshes between each Unity frame. For a 60Hz screen 1=60fps, 2=30fps, 0=don't wait for sync.
Application.targetFrameRate DOES work in the editor. I don't know where you got the information that it doesn't, but try it. I assure you it works.
@Vox Nephilia: http://docs.unity3d.com/Documentation/ScriptReference/Application-targetFrameRate.html
targetFrameRate is ignored in the editor.
Guess the docs are wrong if it works.
The docs aren't exactly wrong, they're just a bit unclear. By "the editor" they don't mean the game view in the editor. They mean the editor itself. You can run your own code in the editor by using ExecuteInEdit$$anonymous$$ode, but this will ignore these framerate settings.
In the editor's game view, these settings are applied without a problem.
For the record, in Unity 4.5.x Application.targetFrameRate works ONLY when VSync Count is set to 'Don't sync'. If VSYNC is set to any other value, the editor won't respect the value in Application.targetFrameRate
Answer by Jaroslav-Stehlik · Jan 03, 2015 at 03:16 PM
Hello
I just wrote this frame limiter. It is straight forward. It does slowsdown your game to achieve desired framrate in editor. Use it only for testing.
using UnityEngine;
using System.Collections;
using System;
public class FrameLimiter : MonoBehaviour {
public int desiredFPS = 60;
void Awake()
{
Application.targetFrameRate = -1;
QualitySettings.vSyncCount = 0;
}
void Update()
{
long lastTicks = DateTime.Now.Ticks;
long currentTicks = lastTicks;
float delay = 1f / desiredFPS;
float elapsedTime;
if (desiredFPS <= 0)
return;
while (true)
{
currentTicks = DateTime.Now.Ticks;
elapsedTime = (float)TimeSpan.FromTicks(currentTicks - lastTicks).TotalSeconds;
if(elapsedTime >= delay)
{
break;
}
}
}
}
After trying this out, i had to delete it cause it slowed everything down in unity and now i get a bunch of errors.
Answer by mayorc1978 · Jan 12, 2014 at 01:39 PM
Actually for some reason the Application.targetFrameRate doesn't work in the GameView in the editor if you just put that in the Awake or Start Methods
But, from my tests, if you put that in Update() it works, but better executing it only once with a delay:
using UnityEngine;
using System.Collections;
public class FrameRateManager : MonoBehaviour {
public int frameRate = 60;
void Start() {
StartCoroutine(changeFramerate());
}
IEnumerator changeFramerate() {
yield return new WaitForSeconds(1);
Application.targetFrameRate = frameRate;
}
}
I don't have to do a coroutine to do it. Works perfectly fine putting it in start for me.
Doesn't work for me without it, probably depends on hardware speed, on $$anonymous$$e it's like it gets executed faster than it gets applied, and I need to delay it to make it work.
Seems I'm subbed to this thread so just as an update. I'm using unity 4.3.0f4 On the PC editor I cannot get Application.targetFrameRate to have any impact on the editor game view when testing. No matter where or how I call it.
The best chance people have if they can't get it to work will be using custom timed coroutines to control their logic. The only downside to this is that it won't set the frame rate of embedded systems such as the animation playback (which you could simulate with playback speed settings, although its not quite the same thing).
@FatWednesday -- I'm using the exact same build as you (Unity Pro 4.3.0f4) and just tried: QualitySettings.vSyncCount = 2 - was a total success and allowed me to reproduce some animation issues we are having with our game on iOS.
Give it another crack, P$$anonymous$$ me or post back here if it is still not working with relevant code
My high cpu went down instantly... but I never attached the script to a GameObject in any scene. I use your script only changing 60 to 30... What gives? I thought I was gonna have to attach it to something in the scene...
Your answer
Follow this Question
Related Questions
Run Update at a constant framerate in a single script 3 Answers
Unity game on Android capped at 30 fps 1 Answer
Can I output at a different rate than rendering? 0 Answers
Physics behaviour changing with framerate - Is FixedUpdate() actually working properly? 3 Answers
iOS Keyboard makes performance suffer 3 Answers