GUIConsole from uMMORPG

This commit is contained in:
vis2k 2020-10-13 17:24:05 +02:00
parent 47dd2ef663
commit 7e33203737
3 changed files with 133 additions and 0 deletions

View File

@ -0,0 +1,106 @@
// People should be able to see and report errors to the developer very easily.
//
// Unity's Developer Console only works in development builds and it only shows
// errors. This class provides a console that works in all builds and also shows
// log and warnings in development builds.
//
// Note: we don't include the stack trace, because that can also be grabbed from
// the log files if needed.
//
// Note: there is no 'hide' button because we DO want people to see those errors
// and report them back to us.
//
// Note: normal Debug.Log messages can be shown by building in Debug/Development
// mode.
using UnityEngine;
using System.Collections.Generic;
struct LogEntry
{
public string message;
public LogType type;
public LogEntry(string message, LogType type)
{
this.message = message;
this.type = type;
}
}
public class GUIConsole : MonoBehaviour
{
public int height = 150;
// only keep the recent 'n' entries. otherwise memory would grow forever
// and drawing would get slower and slower.
public int maxLogCount = 50;
// log as queue so we can remove the first entry easily
Queue<LogEntry> log = new Queue<LogEntry>();
// hotkey to show/hide at runtime for easier debugging
// (sometimes we need to temporarily hide/show it)
// => F12 makes sense. nobody can find ^ in other games.
public KeyCode hotKey = KeyCode.F12;
// GUI
bool visible;
Vector2 scroll = Vector2.zero;
#if !UNITY_EDITOR
void Awake()
{
Application.logMessageReceived += OnLog;
}
#endif
// OnLog logs everything, even Debug.Log messages in release builds
// => this makes a lot of things easier. e.g. addon initialization logs.
// => it's really better to have than not to have those
void OnLog(string message, string stackTrace, LogType type)
{
// is this important?
bool isImportant = type == LogType.Error || type == LogType.Exception;
// use stack trace only if important
// (otherwise users would have to find and search the log file.
// seeing it in the console directly is way easier to deal with.)
// => only add \n if stack trace is available (only in debug builds)
if (isImportant && !string.IsNullOrWhiteSpace(stackTrace))
message += "\n" + stackTrace;
// add to queue
log.Enqueue(new LogEntry(message, type));
// respect max entries
if (log.Count > maxLogCount)
log.Dequeue();
// become visible if it was important
// (no need to become visible for regular log. let the user decide.)
if (isImportant)
visible = true;
// auto scroll
scroll.y = float.MaxValue;
}
void Update()
{
if (Input.GetKeyDown(hotKey))
visible = !visible;
}
void OnGUI()
{
if (!visible) return;
scroll = GUILayout.BeginScrollView(scroll, "Box", GUILayout.Width(Screen.width), GUILayout.Height(height));
foreach (LogEntry entry in log)
{
if (entry.type == LogType.Error || entry.type == LogType.Exception)
GUI.color = Color.red;
else if (entry.type == LogType.Warning)
GUI.color = Color.yellow;
GUILayout.Label(entry.message);
GUI.color = Color.white;
}
GUILayout.EndScrollView();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 9021b6cc314944290986ab6feb48db79
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -134,6 +134,7 @@ GameObject:
m_Component:
- component: {fileID: 88936777}
- component: {fileID: 88936776}
- component: {fileID: 88936774}
m_Layer: 0
m_Name: Main Camera
m_TagString: MainCamera
@ -141,6 +142,21 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &88936774
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 88936773}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9021b6cc314944290986ab6feb48db79, type: 3}
m_Name:
m_EditorClassIdentifier:
height: 150
maxLogCount: 50
hotKey: 293
--- !u!20 &88936776
Camera:
m_ObjectHideFlags: 0