Anonymous edits have been disabled on the wiki. If you want to contribute please login or create an account.


Warning for game developers: PCGamingWiki staff members will only ever reach out to you using the official press@pcgamingwiki.com mail address.
Be aware of scammers claiming to be representatives or affiliates of PCGamingWiki who promise a PCGW page for a game key.

Difference between revisions of "Engine:Unity/Unlock FPS"

From PCGamingWiki, the wiki about fixing PC games
(Created page with "'''General information''' {{mm}} [https://docs.unity3d.com/ScriptReference/QualitySettings-vSyncCount.html Unity Document of <code>QualitySettings.vSyncCount</code>] {{mm}} [h...")
 
 
(2 intermediate revisions by 2 users not shown)
Line 4: Line 4:
  
  
Unity games usually lock FPS with <code>QualitySettings.vSyncCount</code> and <code>Application.targetFrameRate</code>.If we can change the parameters of these two methods, FPS can be easily unlocked.
+
Unity games usually lock FPS with <code>QualitySettings.vSyncCount</code> and <code>Application.targetFrameRate</code>. If we can change the parameters of these two methods, FPS can be easily unlocked.
 
{{++}} Most Unity games run perfect after FPS unlocked. Only a few Unity games contain logics tied to framerate.
 
{{++}} Most Unity games run perfect after FPS unlocked. Only a few Unity games contain logics tied to framerate.
 
{{--}} There is currently no method to restore IL2CPP to compileable original code, which makes it difficult to modify code of Unity games using IL2CPP.
 
{{--}} There is currently no method to restore IL2CPP to compileable original code, which makes it difficult to modify code of Unity games using IL2CPP.
 +
 +
 +
==Determine whether game uses Mono or IL2CPP==
 +
It is easy to determine by files in {{P|game}}.
 +
 +
====Mono only files====
 +
* {{folder|GameName_Data/Mono}} or {{folder|MonoBleedingEdge}}
 +
* {{file|GameName_Data/Managed/Assembly-CSharp.dll}}
 +
 +
====IL2CPP only files====
 +
* {{file|GameName_Data/il2cpp_data/Metadata/global-metadata.dat}}
 +
* {{file|GameAssembly.dll}}
 +
* {{file|baselib.dll}}
 +
 +
==Modify game code==
 +
 +
===Decompile game code===
 +
If the game is using Mono, we can decompile with [https://github.com/dnSpyEx/dnSpy dnSpy]{{note|dnSpyEx is the most active fork after original dnSpy archived.}}.
 +
# Open {{file|GameName_Data/Managed/Assembly-CSharp.dll}} in dnSpy.
 +
# File -> Export to Project, ensure Language is set to C#, then select a folder to export
 +
# Open the export folder, then open {{file|Assembly-CSharp.sln}} with Visual Studio, you should see game code
 +
{{ii}} Sometimes the code we need to modify is in other dll files as some libraries used in game may modify FPS limit. Just decompile those dll files in {{folder|GameName_Data/Managed}} in the same way.
 +
 +
===Find FPS lock code===
 +
# After {{file|Assembly-CSharp.sln}} opened in Visual Studio, use the key {{key|Ctrl|Shift|F}} to open global search tool.
 +
{{ii}} {{key|Ctrl|Shift|F}} is also the key to change between Simplfied Chinese and Traditional Chinese of Microsoft Pinyin, which is the default input method for Chinese language pack since Windows 10. If you are using Chinese input on Windows 10 or above, don't forget to change to English input.
 +
# Type <code>vSyncCount</code> or <code>targetFrameRate</code> then click 'Find All'.
 +
# You can see code that limit FPS now.
 +
{{ii}} If you cannot find any code limit FPS, the game may use {{file|globalgamemanagers}} configs to limit FPS. You can refer to [[Engine:Unity#Graphical_presets|graphics preset]].
 +
 +
===Modify code to unlock FPS===
 +
You should first record the highest locked FPS before modification. This can be used to find the code needs to modify. Do not simply modify all code in search result, which may break game on specific scenes.{{note|Some games limit FPS to video framerate when playing videos in game. Modify such code may break video playback.}}
 +
 +
# Navigate to the class with FPS lock code in dnSpy and find the code.
 +
# Right click the code and select 'Edit Method (C#)...' or use {{key|Ctrl|Shift|E}}.
 +
# Modify the code according to description below.
 +
# Click 'Compile' button.
 +
# File -> Save Module... -> OK
 +
 +
{{ii}}Always remember to backup original file before modification. Some games have anti tampering and refuse to run after modification.
 +
 +
====<code>QualitySettings.vSyncCount</code>====
 +
This function controls [[Glossary:Vertical_sync_(Vsync)|vertical sync]]. The value can be 0, 1, 2, 3, 4.
 +
 +
When set to 0, VSync is off and framerate is unlimited unless <code>Application.targetFrameRate</code> is set.
 +
 +
When set to 1-4, VSync is on and framerate limited to <code>Refresh Rate / Value</code>, and <code>Application.targetFrameRate</code> is ignored. For example, value 3 on a 240Hz display means FPS is locked to 240 / 3 = 80 FPS.
 +
 +
====<code>Application.targetFrameRate</code>====
 +
This function simply limit FPS if <code>QualitySettings.vSyncCount</code> is set to 0.
 +
 +
When set to -1, FPS is unlimited.
 +
 +
When a positive integer is set, FPS is locked to this integer.
 +
 +
==References==
 +
<references group="Note"/>

Latest revision as of 23:00, 13 May 2024

General information

Unity Document of QualitySettings.vSyncCount
Unity Document of Application.targetFrameRate


Unity games usually lock FPS with QualitySettings.vSyncCount and Application.targetFrameRate. If we can change the parameters of these two methods, FPS can be easily unlocked.

Most Unity games run perfect after FPS unlocked. Only a few Unity games contain logics tied to framerate.
There is currently no method to restore IL2CPP to compileable original code, which makes it difficult to modify code of Unity games using IL2CPP.


Determine whether game uses Mono or IL2CPP

It is easy to determine by files in <path-to-game>.

Mono only files

  • GameName_Data/Mono or MonoBleedingEdge
  • GameName_Data/Managed/Assembly-CSharp.dll

IL2CPP only files

  • GameName_Data/il2cpp_data/Metadata/global-metadata.dat
  • GameAssembly.dll
  • baselib.dll

Modify game code

Decompile game code

If the game is using Mono, we can decompile with dnSpy[Note 1].

  1. Open GameName_Data/Managed/Assembly-CSharp.dll in dnSpy.
  2. File -> Export to Project, ensure Language is set to C#, then select a folder to export
  3. Open the export folder, then open Assembly-CSharp.sln with Visual Studio, you should see game code
Sometimes the code we need to modify is in other dll files as some libraries used in game may modify FPS limit. Just decompile those dll files in GameName_Data/Managed in the same way.

Find FPS lock code

  1. After Assembly-CSharp.sln opened in Visual Studio, use the key Ctrl+ Shift+F to open global search tool.
Ctrl+ Shift+F is also the key to change between Simplfied Chinese and Traditional Chinese of Microsoft Pinyin, which is the default input method for Chinese language pack since Windows 10. If you are using Chinese input on Windows 10 or above, don't forget to change to English input.
  1. Type vSyncCount or targetFrameRate then click 'Find All'.
  2. You can see code that limit FPS now.
If you cannot find any code limit FPS, the game may use globalgamemanagers configs to limit FPS. You can refer to graphics preset.

Modify code to unlock FPS

You should first record the highest locked FPS before modification. This can be used to find the code needs to modify. Do not simply modify all code in search result, which may break game on specific scenes.[Note 2]

  1. Navigate to the class with FPS lock code in dnSpy and find the code.
  2. Right click the code and select 'Edit Method (C#)...' or use Ctrl+ Shift+E.
  3. Modify the code according to description below.
  4. Click 'Compile' button.
  5. File -> Save Module... -> OK
Always remember to backup original file before modification. Some games have anti tampering and refuse to run after modification.

QualitySettings.vSyncCount

This function controls vertical sync. The value can be 0, 1, 2, 3, 4.

When set to 0, VSync is off and framerate is unlimited unless Application.targetFrameRate is set.

When set to 1-4, VSync is on and framerate limited to Refresh Rate / Value, and Application.targetFrameRate is ignored. For example, value 3 on a 240Hz display means FPS is locked to 240 / 3 = 80 FPS.

Application.targetFrameRate

This function simply limit FPS if QualitySettings.vSyncCount is set to 0.

When set to -1, FPS is unlimited.

When a positive integer is set, FPS is locked to this integer.

References

  1. dnSpyEx is the most active fork after original dnSpy archived.
  2. Some games limit FPS to video framerate when playing videos in game. Modify such code may break video playback.