I am trying to create a graphical display for the FPS count. It [should] draw(s) about 20 lines horizontally. The bigger the fps drop, the higher the lines. However, I don’t want all lines to move in unison. If the fps drops, the only line that should have a different height is the current one being drawn. Pretty much the same as the MineCraft fps counter:
Here is my current code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FPSCounter : MonoBehaviour {
float deltaTime = 0.0f;
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle ();
bool notCalled = true;
float OldFPS = 0;
float diff = 0;
int fCount;
int wait;
float[] pos = new float[20];
void Update () {
deltaTime += (Time.unscaledDeltaTime - deltaTime) * 0.1f;
}
void OnGUI () {
FPStext ();
DrawFPSLine ();
}
void FPStext () {
Rect rect = new Rect (0, 0, w, h * 2 / 100);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 100;
style.normal.textColor = new Color (0.0f, 0.0f, 0.5f, 1.0f);
float msec = deltaTime * 1000.0f;
float fps = 1.0f / deltaTime;
string text = string.Format ("{0:0.0} ms ({1:0.} fps)", msec, fps);
GUI.Label (rect, text, style);
}
void DrawFPSLine () {
int maxW = 95;
float NewFPS = 1.0f / deltaTime;
diff = Mathf.Round (NewFPS - OldFPS * 10f) / 100f;
for (int i = 0; i < pos.Length; i++) {
pos[i] = 0 + diff;
}
while (fCount == 20) {
foreach (float f in pos) {
Drawing.DrawLine (new Vector2 (100, 400), new Vector2 (100, Mathf.Floor (300 - diff)), new Color (0.1f, 0.3f, 0.7f, 1.0f), 5);
}
fCount = 0;
}
OldFPS = NewFPS;
fCount++;
}
}
Is this possible to do?
Dream
FYI: The Drawing.DrawLine();
function is a simple 2D line drawer I found online. I had a search for it now, but can’t find it again. If I do, I will add it. Here is the premise though (taken from the script):
//****************************************************************************************************
// static function DrawLine(rect : Rect) : void
// static function DrawLine(rect : Rect, color : Color) : void
// static function DrawLine(rect : Rect, width : float) : void
// static function DrawLine(rect : Rect, color : Color, width : float) : void
// static function DrawLine(Vector2 pointA, Vector2 pointB) : void
// static function DrawLine(Vector2 pointA, Vector2 pointB, color : Color) : void
// static function DrawLine(Vector2 pointA, Vector2 pointB, width : float) : void
// static function DrawLine(Vector2 pointA, Vector2 pointB, color : Color, width : float) : void
//
// Draws a GUI line on the screen.
//
// DrawLine makes up for the severe lack of 2D line rendering in the Unity runtime GUI system.
// This function works by drawing a 1x1 texture filled with a color, which is then scaled
// and rotated by altering the GUI matrix. The matrix is restored afterwards.
//****************************************************************************************************