You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Logger : MonoBehaviour
{
[Range(1, 5)]
public float duration = 3.0f;
public Color color = Color.white;
List<KeyValuePair<string, float>> log_history = new List<KeyValuePair<string, float>>();
List<int> out_of_date;
GUIStyle style;
float line_height;
public float updateInterval = 0.1f;
private double lastInterval;
private int frames = 0;
private float currFPS;
private bool viewFPS = true;
public bool enableFPS
{
set{
viewFPS = !viewFPS;
}
get{
return viewFPS;
}
}
private void Start()
{
log_history = new List<KeyValuePair<string, float>>();
out_of_date = new List<int>();
style = new GUIStyle();
lastInterval = Time.realtimeSinceStartup;
frames = 0;
}
private void Update()
{
line_height = Screen.height * 0.05f;
style.fontSize = (int)line_height;
out_of_date.Clear();
for (int i = log_history.Count - 1; i >= 0; i--)
{
if (Time.time - log_history[i].Value > duration)
out_of_date.Add(i);
}
foreach (var i in out_of_date)
{
log_history.RemoveAt(i);
}
CountFPS();
}
void OnEnable()
{
Application.logMessageReceived += HandleLog;
}
void OnDisable()
{
Application.logMessageReceived -= HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type)
{
if(log_history == null || logString == null)
{
Debug.Log("");
}
log_history.Add(new KeyValuePair<string, float>(logString, Time.time));
}
void CountFPS()
{
++frames;
float timeNow = Time.realtimeSinceStartup;
if (timeNow > lastInterval + updateInterval)
{
currFPS = (float)(frames / (timeNow - lastInterval));
frames = 0;
lastInterval = timeNow;
}
}
void OnGUI()
{
//if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button"))
//print("You clicked the button!");
float offset = 15.0f;
Rect position = new Rect(offset, Screen.height - line_height * 2.0f, Screen.width - offset * 2.0f, line_height);
if (viewFPS)
{
style.normal.textColor = new Color(color.r, color.g, color.b, 1);
GUI.Label(position, string.Format("fps: {0}", currFPS.ToString("f2")), style);
position.y = position.y - line_height;
}
for (int i = log_history.Count - 1; i >= 0; i--)
{
var ls = log_history[i];
float a = 1.0f - Mathf.Pow((Time.time - ls.Value) / duration, 2.2f);
style.normal.textColor = new Color(color.r, color.g, color.b, a);
GUI.Label(position, ls.Key + string.Format(" {0}", Time.time - ls.Value), style);
position.y = position.y - line_height;
}
}
}