问题
Since yesterday, my unity game is locked 30fps on an android device. Before it was like 150fps.. I disabled v-sync. This is the only script I added yesterday:
public static class SaveLoadProgress {
public static void Save()
{
LevelManager levelManager = GameObject.Find ("GameManager").GetComponent<LevelManager> ();
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create (Application.persistentDataPath + "/savedProgress.1912");
bf.Serialize(file, levelManager.levelReached);
file.Close();
}
public static void Load()
{
LevelManager levelManager = GameObject.Find ("GameManager").GetComponent<LevelManager> ();
if(File.Exists(Application.persistentDataPath + "/savedProgress.1912"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/savedProgress.1912", FileMode.Open);
levelManager.levelReached = (int)bf.Deserialize(file);
file.Close();
}
}
}
I don't this can be the problem. I added this to the GameManager:
void Awake()
{
Application.targetFrameRate = -1;
QualitySettings.vSyncCount = 0;
}
The profiler shows this:
What should I do to unlock it from 30fps?
回答1:
By default, Unity locks fps for specific values, according to the current platform. Documentation says that the default for Android is 30, which is used by most Android devices. Also it says:
The default targetFrameRate is a special value -1, which indicates that the game should render at the platform's default frame rate.
Which is 30 for mobile as it's said in the article.
Also, you can have as much fps as possible while running your game in editor, it maybe 150 or more, but it's really bad practice to have more than 60 (max value for iPhones and some Androids). If you have more than 60 fps you can't actually see the difference because device's display can't actually render more than 60 frames per second, but it takes battery and CPU resources and all those calculations (for extra 60+ fps) are useless and even bad.
To make your fps more than 30, you can set it for 60 as you are doing in your code:
Application.targetFrameRate = 60;
Also v-sync is mostly don't work on mobile, so you don't need to bother with this.
回答2:
Not an answer but more of an extended comment to @vmchar's answer:
I disagree that VSync doesn't work on mobile. We had Application.targetFrameRate
set to 60, however, we still had an issue with the CPU trashing the GPU. Setting VSync forces it to wait. Framerate went from https://imgur.com/a/lE0LedF to https://imgur.com/a/EPq6q7T.
Unity don't recommend using both at once though:
Application.targetFrameRate is a "soft" vsync and is useful to limit simulation rate without hardware vsync set (e.g. on servers). It is not desirable to use targegFrameRate with vSyncCount set, as CPU frame might drift and cause rendercommands to be scheduled later and miss next vsync.
VSync settings aren't respected in the Editor though, so you can still have issues there.
Full thread: https://forum.unity.com/threads/gfx-presentframe-taking-4x-as-long-but-only-for-intermittent-periods.538254
回答3:
As stated in other answer, if Unity locks your framerate to 30 or something. Mine was locked at 30 FPS, Just add Application.targetFrameRate = 60;
in your Start()
method, or whenever you want to set the Framerate in the application lifetime.
void Start()
{
Application.targetFrameRate = 60;
}
来源:https://stackoverflow.com/questions/47031279/unity-mobile-device-30fps-locked