Skip to content

Instantly share code, notes, and snippets.

@alerdenisov
Last active August 30, 2017 12:17
Show Gist options
  • Save alerdenisov/d5354ee2afd25b3c4fce046fcf824047 to your computer and use it in GitHub Desktop.
Save alerdenisov/d5354ee2afd25b3c4fce046fcf824047 to your computer and use it in GitHub Desktop.
FPS UI Graph
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;
namespace FpsAnalyzer
{
public class Analyser : MaskableGraphic
{
[SerializeField] private int targetFps = 60;
[SerializeField] private int accumulate = 10;
[SerializeField] private int frames = 50;
[SerializeField] private bool useGradient;
// http://www.brechtos.com/hiding-or-disabling-inspector-properties-using-propertydrawers-within-unity-5/
// [ConditionalHide("useGradient", true, true)]
[SerializeField]
private Color colorHigh;
// http://www.brechtos.com/hiding-or-disabling-inspector-properties-using-propertydrawers-within-unity-5/
// [ConditionalHide("useGradient", true, true)]
[SerializeField]
private Color colorLow;
// http://www.brechtos.com/hiding-or-disabling-inspector-properties-using-propertydrawers-within-unity-5/
//[ConditionalHide("useGradient", true, false)]
[SerializeField]
private Gradient fpsMap;
private List<UIVertex> vertices = new List<UIVertex>();
private float deltaTime;
private int frame;
void Update()
{
//Calculate FPS
deltaTime += (Time.deltaTime - deltaTime) / accumulate;
if (frame % accumulate == 0)
{
Profiler.BeginSample("Analizer.Calculation");
float cfps = 1.0f / deltaTime;
var lfps = 0f;
if (vertices.Count > 0)
{
lfps = vertices[vertices.Count - 1].position.y;
}
var af = frame / accumulate;
var ccolor = CalculateColor(cfps);
var lcolor = CalculateColor(lfps);
vertices.Add(CreateVertex(ccolor, af - 0, cfps));
vertices.Add(CreateVertex(ccolor, af - 0, 0));
vertices.Add(CreateVertex(lcolor, af - 1, 0));
vertices.Add(CreateVertex(lcolor, af - 1, 0));
vertices.Add(CreateVertex(lcolor, af - 1, lfps));
vertices.Add(CreateVertex(ccolor, af - 0, cfps));
if (vertices.Count > frames * 6)
{
vertices.RemoveRange(0, vertices.Count - frames * 6);
}
var count = vertices.Count;
for (int f = 0; f < frames; f++)
{
if (f * 6 + 5 > count)
{
break;
}
MoveVertex(vertices, f * 6 + 0, f + 1);
MoveVertex(vertices, f * 6 + 1, f + 1);
MoveVertex(vertices, f * 6 + 2, f + 0);
MoveVertex(vertices, f * 6 + 3, f + 0);
MoveVertex(vertices, f * 6 + 4, f + 0);
MoveVertex(vertices, f * 6 + 5, f + 1);
}
Profiler.EndSample();
SetAllDirty();
}
frame++;
}
private void MoveVertex(List<UIVertex> list, int index, int x)
{
var vert = list[index];
vert.position.x = x;
list[index] = vert;
}
private UIVertex CreateVertex(Color clr, int frame, float height)
{
var vert = UIVertex.simpleVert;
vert.position = new Vector3(frame, height);
vert.color = clr;
return vert;
}
private Color CalculateColor(float fps)
{
if (useGradient)
{
return fpsMap.Evaluate(Mathf.Clamp01(fps / targetFps)) * color;
}
return Color.Lerp(colorLow, colorHigh, Mathf.Clamp01(fps / targetFps)) * color;
}
/// <summary>
/// Create mesh of graph
/// </summary>
/// <param name="vh"></param>
protected override void OnPopulateMesh(VertexHelper vh)
{
base.OnPopulateMesh(vh);
vh.Clear();
vh.AddUIVertexTriangleStream(vertices);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment