Is there any way I can limit the framerate of my game in Unity Editor?
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.
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;
}
}
}
}
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;
}
}