2020-05-31 17:07:22 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using System.Linq;
|
2020-04-25 17:48:35 +00:00
|
|
|
|
using UnityEngine.UI;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SanAndreasUnity.Utilities {
|
|
|
|
|
|
|
|
|
|
public class FPSDisplay : MonoBehaviour {
|
|
|
|
|
|
2020-04-25 19:15:59 +00:00
|
|
|
|
public static FPSDisplay Instance { get; private set; }
|
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
private static int s_fpsTextureWidth = 75;
|
|
|
|
|
private static int s_fpsTextureHeight = 25;
|
|
|
|
|
private static float s_fpsMaximum = 60.0f;
|
|
|
|
|
private Texture2D m_fpsTexture = null;
|
|
|
|
|
private Color[] m_colors = null;
|
|
|
|
|
private float[] m_fpsHistory = new float[s_fpsTextureWidth];
|
|
|
|
|
private int m_fpsIndex = 0;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2020-04-25 17:48:35 +00:00
|
|
|
|
public RawImage fpsImage;
|
|
|
|
|
public Text fpsText;
|
2019-08-30 02:38:09 +00:00
|
|
|
|
|
2020-04-26 14:30:52 +00:00
|
|
|
|
public bool updateFPS = true;
|
2020-04-25 19:15:59 +00:00
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Awake () {
|
|
|
|
|
|
2020-04-25 19:15:59 +00:00
|
|
|
|
if (null == Instance)
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_fpsTexture = new Texture2D(s_fpsTextureWidth, s_fpsTextureHeight, TextureFormat.RGBA32, false, true);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_colors = new Color[m_fpsTexture.width * m_fpsTexture.height];
|
2019-08-30 01:13:31 +00:00
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
this.fpsImage.texture = this.m_fpsTexture;
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Update () {
|
|
|
|
|
|
2020-04-26 14:30:52 +00:00
|
|
|
|
if (this.updateFPS)
|
|
|
|
|
{
|
2020-04-26 14:55:39 +00:00
|
|
|
|
UpdateTexture();
|
2020-04-25 19:20:10 +00:00
|
|
|
|
UpdateText();
|
2020-04-26 14:30:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 01:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 14:55:39 +00:00
|
|
|
|
void UpdateTexture()
|
2019-08-30 01:38:24 +00:00
|
|
|
|
{
|
|
|
|
|
|
2020-04-26 14:55:39 +00:00
|
|
|
|
float fps = 1.0f / Time.unscaledDeltaTime;
|
|
|
|
|
|
2019-08-30 01:38:24 +00:00
|
|
|
|
UnityEngine.Profiling.Profiler.BeginSample("Reset texture pixels");
|
2020-04-25 18:35:36 +00:00
|
|
|
|
int numPixels = m_fpsTexture.width * m_fpsTexture.height;
|
2019-08-30 02:38:09 +00:00
|
|
|
|
Color backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.66f); // Half-transparent background for FPS graph
|
|
|
|
|
for (int i = 0; i < numPixels; i++)
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_colors[i] = backgroundColor;
|
2019-08-30 01:38:24 +00:00
|
|
|
|
UnityEngine.Profiling.Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
UnityEngine.Profiling.Profiler.BeginSample("Set pixels");
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_fpsTexture.SetPixels(m_colors);
|
2019-08-30 01:38:24 +00:00
|
|
|
|
UnityEngine.Profiling.Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
// Append to history storage
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_fpsHistory[m_fpsIndex] = fps;
|
2019-08-30 01:38:24 +00:00
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
int f = m_fpsIndex;
|
2019-08-30 01:38:24 +00:00
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
if (fps > m_fpsHistory.Average())
|
|
|
|
|
s_fpsMaximum = fps;
|
2019-08-30 01:38:24 +00:00
|
|
|
|
|
|
|
|
|
// Draw graph into texture
|
|
|
|
|
UnityEngine.Profiling.Profiler.BeginSample("Set fps history pixels");
|
2020-04-25 18:35:36 +00:00
|
|
|
|
for (int i = m_fpsTexture.width - 1; i >= 0; i--)
|
2019-08-30 01:38:24 +00:00
|
|
|
|
{
|
2020-04-25 18:35:36 +00:00
|
|
|
|
float graphVal = (m_fpsHistory[f] > s_fpsMaximum) ? s_fpsMaximum : m_fpsHistory[f]; //Clamps
|
|
|
|
|
int height = (int)(graphVal * m_fpsTexture.height / (s_fpsMaximum + 0.1f)); //Returns the height of the desired point with a padding of 0.1f units
|
2019-08-30 01:38:24 +00:00
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
float p = m_fpsHistory[f] / s_fpsMaximum,
|
2019-08-30 01:38:24 +00:00
|
|
|
|
r = Mathf.Lerp(1, 1 - p, p),
|
|
|
|
|
g = Mathf.Lerp(p * 2, p, p);
|
|
|
|
|
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_fpsTexture.SetPixel(i, height, new Color(r, g, 0));
|
2019-08-30 01:38:24 +00:00
|
|
|
|
f--;
|
|
|
|
|
|
|
|
|
|
if (f < 0)
|
2020-04-25 18:35:36 +00:00
|
|
|
|
f = m_fpsHistory.Length - 1;
|
2019-08-30 01:38:24 +00:00
|
|
|
|
}
|
|
|
|
|
UnityEngine.Profiling.Profiler.EndSample();
|
|
|
|
|
|
|
|
|
|
// Next entry in rolling history buffer
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_fpsIndex++;
|
|
|
|
|
if (m_fpsIndex >= m_fpsHistory.Length)
|
|
|
|
|
m_fpsIndex = 0;
|
2019-08-30 01:38:24 +00:00
|
|
|
|
|
|
|
|
|
UnityEngine.Profiling.Profiler.BeginSample("Apply texture");
|
2020-04-25 18:35:36 +00:00
|
|
|
|
m_fpsTexture.Apply(false, false);
|
2019-08-30 01:38:24 +00:00
|
|
|
|
UnityEngine.Profiling.Profiler.EndSample();
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-25 18:33:22 +00:00
|
|
|
|
void UpdateText()
|
|
|
|
|
{
|
2020-04-26 14:55:39 +00:00
|
|
|
|
float fps = 1.0f / Time.unscaledDeltaTime;
|
2020-04-25 18:33:22 +00:00
|
|
|
|
string text = string.Format("{0:0.0} fps", fps);
|
|
|
|
|
if (this.fpsText.text != text)
|
|
|
|
|
this.fpsText.text = text;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|