Last active
April 26, 2018 17:13
-
-
Save aberloni/bdf1cec83d6e4e2913ca2bc733adf9d1 to your computer and use it in GitHub Desktop.
Simple FPS experimentations for unity framerate FPS / vsync
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
/// <summary> | |
/// | |
/// https://docs.unity3d.com/ScriptReference/Application-targetFrameRate.html | |
/// | |
/// On standalone platforms the default frame rate is maximum achievable frame rate. | |
/// | |
/// On mobile platforms the default frame rate is less than the maximum achievable frame rate due to need to conserve battery power. | |
/// Typically on mobile platforms the default frame rate is 30 frames per second. | |
/// | |
/// Additionally if the QualitySettings.vSyncCount property is set, the targetFrameRate will be ignored and | |
/// instead the game will use the vSyncCount and the platform's default render rate to determine the target frame rate. | |
/// For example, if the platform's default render rate is 60 frames per second and vSyncCount is set to 2, | |
/// the game will target 30 frames per second. | |
/// | |
/// </summary> | |
public class Framerate : MonoBehaviour | |
{ | |
public float prevTime = 0f; | |
public float delta = 0f; | |
float guiDelta = 0; | |
int updateDisplayFrameCount = 0; | |
private void Start() | |
{ | |
prevTime = Time.time; | |
//Application.targetFrameRate = 60; | |
} | |
private void Update() | |
{ | |
if (Input.GetMouseButtonUp(1)) | |
{ | |
if (touchCorner()) | |
{ | |
QualitySettings.vSyncCount++; | |
if (QualitySettings.vSyncCount > 2) QualitySettings.vSyncCount = 0; | |
} | |
} | |
else if (Input.GetMouseButtonUp(0)) | |
{ | |
if (touchCorner()) | |
{ | |
if (Application.targetFrameRate < 0) Application.targetFrameRate = 60; | |
else Application.targetFrameRate = -1; | |
} | |
} | |
delta = Time.time - prevTime; | |
prevTime = Time.time; | |
if (updateDisplayFrameCount < 0) | |
{ | |
guiDelta = 1f / delta; | |
updateDisplayFrameCount = 5; | |
} | |
else updateDisplayFrameCount--; | |
} | |
GUIStyle style; | |
private void OnGUI() | |
{ | |
if (style == null) | |
{ | |
style = new GUIStyle(); | |
style.fontSize = (int)(Screen.width / 20f); | |
style.richText = true; | |
style.alignment = TextAnchor.UpperRight; | |
} | |
string ct = ""; | |
int target = Application.targetFrameRate; | |
string color = guiDelta > target * 0.95f ? "green" : (guiDelta > target * 0.5f) ? "orange" : "red"; | |
ct += "<color="+color+"><b>"+Mathf.CeilToInt(guiDelta)+"</b></color> / "+Application.targetFrameRate+" / vs"+QualitySettings.vSyncCount; | |
ct += "\n" + delta; | |
int width = 400; | |
Rect rec = new Rect(Screen.width - width, 0, width, width); | |
//GUI.TextField(rec, ct, style); | |
GUI.Label(rec, ct, style); | |
} | |
bool touchCorner() | |
{ | |
Vector2 pos = getTouchScreenPosition(); | |
if (Input.mousePosition.x > Screen.width - 100f && Input.mousePosition.y > Screen.height - 100f) return true; | |
return false; | |
} | |
static public Vector3 getTouchScreenPosition() | |
{ | |
if (Input.touches.Length > 0) | |
{ | |
Touch t = Input.touches[0]; | |
return t.position; | |
} | |
return Input.mousePosition; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment