轉(zhuǎn)載請(qǐng)注明地址:涼鞋的筆記
為毛要實(shí)現(xiàn)這個(gè)工具?
??1.在我小時(shí)候,每當(dāng)游戲在真機(jī)運(yùn)行時(shí),我們看到的日志是這樣的。
沒高亮啊,還有亂七八糟的堆棧信息,好干擾日志查看,好影響心情精堕。
2.還有就是必須始終連著usb線啊孵淘,我想要想躺著測試。歹篓。瘫证。
以上種種原因,QConsole誕生了揉阎。
如何使用?
使用方式和QLog一樣,在初始化出調(diào)用,簡單的一句。
QConsole.Instance();
就好了,使用之后效果是這樣的背捌。
在Editor模式下,F1控制開關(guān)毙籽。
在真機(jī)上需要在屏幕上同時(shí)按下五個(gè)手指就可以控制開關(guān)了。(本來考慮11個(gè)手指萌一下的)毡庆。
實(shí)現(xiàn)思路:
??1.首先要想辦法獲取Log,這個(gè)和上一篇介紹的QLog一樣,需要使用Application.logMessageReceived這個(gè)api坑赡。
??2.獲取到的Log信息要存在一個(gè)Queue或者List中,然后把Log輸出到屏幕上就ok了。
??3.輸出到屏幕上使用的是OnGUI回調(diào)和 GUILayout.Window這個(gè)api, 總共三步么抗。
貼上代碼:
QConsole實(shí)現(xiàn)
sing UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;
using System;
using System.Collections.Generic;
namespace QFramework {
/// <summary>
/// 控制臺(tái)GUI輸出類
/// 包括FPS毅否,內(nèi)存使用情況,日志GUI輸出
/// </summary>
public class QConsole : QSingleton<QConsole>
{
struct ConsoleMessage
{
public readonly string message;
public readonly string stackTrace;
public readonly LogType type;
public ConsoleMessage (string message, string stackTrace, LogType type)
{
this.message = message;
this.stackTrace = stackTrace;
this.type = type;
}
}
/// <summary>
/// Update回調(diào)
/// </summary>
public delegate void OnUpdateCallback();
/// <summary>
/// OnGUI回調(diào)
/// </summary>
public delegate void OnGUICallback();
public OnUpdateCallback onUpdateCallback = null;
public OnGUICallback onGUICallback = null;
/// <summary>
/// FPS計(jì)數(shù)器
/// </summary>
private QFPSCounter fpsCounter = null;
/// <summary>
/// 內(nèi)存監(jiān)視器
/// </summary>
private QMemoryDetector memoryDetector = null;
private bool showGUI = true;
List<ConsoleMessage> entries = new List<ConsoleMessage>();
Vector2 scrollPos;
bool scrollToBottom = true;
bool collapse;
bool mTouching = false;
const int margin = 20;
Rect windowRect = new Rect(margin + Screen.width * 0.5f, margin, Screen.width * 0.5f - (2 * margin), Screen.height - (2 * margin));
GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
GUIContent scrollToBottomLabel = new GUIContent("ScrollToBottom", "Scroll bar always at bottom");
private QConsole()
{
this.fpsCounter = new QFPSCounter(this);
this.memoryDetector = new QMemoryDetector(this);
// this.showGUI = App.Instance().showLogOnGUI;
QApp.Instance().onUpdate += Update;
QApp.Instance().onGUI += OnGUI;
Application.logMessageReceived += HandleLog;
}
~QConsole()
{
Application.logMessageReceived -= HandleLog;
}
void Update()
{
#if UNITY_EDITOR
if (Input.GetKeyUp(KeyCode.F1))
this.showGUI = !this.showGUI;
#elif UNITY_ANDROID
if (Input.GetKeyUp(KeyCode.Escape))
this.showGUI = !this.showGUI;
#elif UNITY_IOS
if (!mTouching && Input.touchCount == 4)
{
mTouching = true;
this.showGUI = !this.showGUI;
} else if (Input.touchCount == 0){
mTouching = false;
}
#endif
if (this.onUpdateCallback != null)
this.onUpdateCallback();
}
void OnGUI()
{
if (!this.showGUI)
return;
if (this.onGUICallback != null)
this.onGUICallback ();
if (GUI.Button (new Rect (100, 100, 200, 100), "清空數(shù)據(jù)")) {
PlayerPrefs.DeleteAll ();
#if UNITY_EDITOR
EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
windowRect = GUILayout.Window(123456, windowRect, ConsoleWindow, "Console");
}
/// <summary>
/// A window displaying the logged messages.
/// </summary>
void ConsoleWindow (int windowID)
{
if (scrollToBottom) {
GUILayout.BeginScrollView (Vector2.up * entries.Count * 100.0f);
}
else {
scrollPos = GUILayout.BeginScrollView (scrollPos);
}
// Go through each logged entry
for (int i = 0; i < entries.Count; i++) {
ConsoleMessage entry = entries[i];
// If this message is the same as the last one and the collapse feature is chosen, skip it
if (collapse && i > 0 && entry.message == entries[i - 1].message) {
continue;
}
// Change the text colour according to the log type
switch (entry.type) {
case LogType.Error:
case LogType.Exception:
GUI.contentColor = Color.red;
break;
case LogType.Warning:
GUI.contentColor = Color.yellow;
break;
default:
GUI.contentColor = Color.white;
break;
}
if (entry.type == LogType.Exception)
{
GUILayout.Label(entry.message + " || " + entry.stackTrace);
} else {
GUILayout.Label(entry.message);
}
}
GUI.contentColor = Color.white;
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
// Clear button
if (GUILayout.Button(clearLabel)) {
entries.Clear();
}
// Collapse toggle
collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
scrollToBottom = GUILayout.Toggle (scrollToBottom, scrollToBottomLabel, GUILayout.ExpandWidth (false));
GUILayout.EndHorizontal();
// Set the window to be draggable by the top title bar
GUI.DragWindow(new Rect(0, 0, 10000, 20));
}
void HandleLog (string message, string stackTrace, LogType type)
{
ConsoleMessage entry = new ConsoleMessage(message, stackTrace, type);
entries.Add(entry);
}
}
}
QFPSCounter
using UnityEngine;
using System.Collections;
namespace QFramework {
/// <summary>
/// 幀率計(jì)算器
/// </summary>
public class QFPSCounter
{
// 幀率計(jì)算頻率
private const float calcRate = 0.5f;
// 本次計(jì)算頻率下幀數(shù)
private int frameCount = 0;
// 頻率時(shí)長
private float rateDuration = 0f;
// 顯示幀率
private int fps = 0;
public QFPSCounter(QConsole console)
{
console.onUpdateCallback += Update;
console.onGUICallback += OnGUI;
}
void Start()
{
this.frameCount = 0;
this.rateDuration = 0f;
this.fps = 0;
}
void Update()
{
++this.frameCount;
this.rateDuration += Time.deltaTime;
if (this.rateDuration > calcRate)
{
// 計(jì)算幀率
this.fps = (int)(this.frameCount / this.rateDuration);
this.frameCount = 0;
this.rateDuration = 0f;
}
}
void OnGUI()
{
GUI.color = Color.black;
GUI.Label(new Rect(80, 20, 120, 20),"fps:" + this.fps.ToString());
}
}
}
QMemoryDetector
using UnityEngine;
using System.Collections;
namespace QFramework {
/// <summary>
/// 內(nèi)存檢測器蝇刀,目前只是輸出Profiler信息
/// </summary>
public class QMemoryDetector
{
private readonly static string TotalAllocMemroyFormation = "Alloc Memory : {0}M";
private readonly static string TotalReservedMemoryFormation = "Reserved Memory : {0}M";
private readonly static string TotalUnusedReservedMemoryFormation = "Unused Reserved: {0}M";
private readonly static string MonoHeapFormation = "Mono Heap : {0}M";
private readonly static string MonoUsedFormation = "Mono Used : {0}M";
// 字節(jié)到兆
private float ByteToM = 0.000001f;
private Rect allocMemoryRect;
private Rect reservedMemoryRect;
private Rect unusedReservedMemoryRect;
private Rect monoHeapRect;
private Rect monoUsedRect;
private int x = 0;
private int y = 0;
private int w = 0;
private int h = 0;
public QMemoryDetector(QConsole console)
{
this.x = 60;
this.y = 60;
this.w = 200;
this.h = 20;
this.allocMemoryRect = new Rect(x, y, w, h);
this.reservedMemoryRect = new Rect(x, y + h, w, h);
this.unusedReservedMemoryRect = new Rect(x, y + 2 * h, w, h);
this.monoHeapRect = new Rect(x, y + 3 * h, w, h);
this.monoUsedRect = new Rect(x, y + 4 * h, w, h);
console.onGUICallback += OnGUI;
}
void OnGUI()
{
GUI.Label(this.allocMemoryRect,
string.Format(TotalAllocMemroyFormation, Profiler.GetTotalAllocatedMemory() * ByteToM));
GUI.Label(this.reservedMemoryRect,
string.Format(TotalReservedMemoryFormation, Profiler.GetTotalReservedMemory() * ByteToM));
GUI.Label(this.unusedReservedMemoryRect,
string.Format(TotalUnusedReservedMemoryFormation, Profiler.GetTotalUnusedReservedMemory() * ByteToM));
GUI.Label(this.monoHeapRect,
string.Format(MonoHeapFormation, Profiler.GetMonoHeapSize() * ByteToM));
GUI.Label(this.monoUsedRect,
string.Format(MonoUsedFormation, Profiler.GetMonoUsedSize() * ByteToM));
}
}
}
注意事項(xiàng):
1.和上一篇介紹的QLog一樣,需要依賴上上篇文章介紹的QApp螟加。
??2.QConsole初步實(shí)現(xiàn)來自于開源Unity插件Unity-WWW-Wrapper中的Console.cs.在此基礎(chǔ)上添加了ScrollToBottom選項(xiàng)。因?yàn)檫@個(gè)插件的控制臺(tái)不支持滾動(dòng)顯示Log,需要拖拽右邊的scrollBar,很不方便吞琐。
??3.Unity-WWW-wrapper非常不穩(wěn)定,建議大家不要使用捆探。倒是感興趣的同學(xué)可以研究下實(shí)現(xiàn),貼上地址:https://www.assetstore.unity3d.com/en/#!/content/19116。
歡迎討論!