Searched the manuals but haven't found a way to lock the framerate at runtime. I want my app to be running in the background and it doesn't need to sync with the vertical refresh (usually 60fps for LCD's) - I want to limit to say, 25-30 FPS. How would I do this - or is it even possible? thx Shaun
There's this. Someone also had another way of doing it that doesn't involve busy-waiting (so it doesn't take up 100% CPU time), but I'm not sure where that is. --Eric
I think the other solution was calling Thread.Sleep (or whatever is the function name in .NET, don't remember) to maintain more or less constant frame rate.
Thanks guys - the key is using as little CPU as possible. Whenever I run this Unity app on my MacBookPro, my fans start spinning like crazy, it heats up and I generally feel uncomfortable to leave running it in the background (its drawing about 40% cpu avg, gpu - no idea) Which brings up another point - what is actually paused when "RunInBackground' is set to false - do networking or other time critical commands still get processed? Aras - where would one put the Thread.Sleep() statement? And wouldn't it mess with the behavioral processing? How about making this part of UnityEngine? Shaun.
I can tell you that networking doesn't work when paused. If you run a server that is paused, the game updates stop for everyone. Because of that, I would assume it all halts. -Jeremy
Just needed to solve the same issue--funny how that works :roll: This will maintain constant framerate, and reduce cpu load. As for side effects, I don't know yet. I just wrote this based on the busy waiting solution, but switched to sleep based on Aras's idea Code (csharp): using UnityEngine; using System.Collections; using System.Threading; public class FpsClamp : MonoBehaviour { float oldTime = 0.0F; float theDeltaTime= 0.0F; float curTime= 0.0F; float timeTaken = 0.0F; int frameRate = 30; // Use this for initialization void Start () { theDeltaTime = (1.0F /frameRate); oldTime = Time.realtimeSinceStartup; } // Update is called once per frame void LateUpdate () { curTime = Time.realtimeSinceStartup; timeTaken = (curTime - oldTime); if(timeTaken < theDeltaTime){ Thread.Sleep((int)(1000*(theDeltaTime - timeTaken))); } oldTime = Time.realtimeSinceStartup; } }
Nice one - thanks Brian. What would be good is to update the code so that the framerate is reduced to 1FPS when in the background - I guess that could be done with a combination RunInBackground and OnApplicationPause.