日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unity 游戏框架搭建 (九) 减少加班利器-QConsole

發布時間:2023/12/6 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unity 游戏框架搭建 (九) 减少加班利器-QConsole 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為毛要實現這個工具?

  • 在我小時候,每當游戲在真機運行時,我們看到的日志是這樣的。

    沒高亮啊,還有亂七八糟的堆棧信息,好干擾日志查看,好影響心情。

  • 還有就是必須始終連著usb線啊,我想要想躺著測試。。。 以上種種原因,QConsole誕生了。

  • 如何使用?

    使用方式和QLog一樣,在初始化出調用,簡單的一句。

    QConsole.Instance(); 復制代碼

    就好了,使用之后效果是這樣的。

    在Editor模式下,F1控制開關。

    在真機上需要在屏幕上同時按下五個手指就可以控制開關了。(本來考慮11個手指萌一下的)。

    實現思路:

    1.首先要想辦法獲取Log,這個和上一篇介紹的QLog一樣,需要使用Application.logMessageReceived這個api。

    2.獲取到的Log信息要存在一個Queue或者List中,然后把Log輸出到屏幕上就ok了。

    3.輸出到屏幕上使用的是OnGUI回調和 GUILayout.Window這個api, 總共三步。

    貼上代碼:

    QConsole實現

    sing UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif using System.Collections; using System; using System.Collections.Generic;namespace QFramework {/// <summary>/// 控制臺GUI輸出類/// 包括FPS,內存使用情況,日志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回調/// </summary>public delegate void OnUpdateCallback();/// <summary>/// OnGUI回調/// </summary>public delegate void OnGUICallback();public OnUpdateCallback onUpdateCallback = null;public OnGUICallback onGUICallback = null;/// <summary>/// FPS計數器/// </summary>private QFPSCounter fpsCounter = null;/// <summary>/// 內存監視器/// </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_EDITORif (Input.GetKeyUp(KeyCode.F1))this.showGUI = !this.showGUI;#elif UNITY_ANDROIDif (Input.GetKeyUp(KeyCode.Escape))this.showGUI = !this.showGUI;#elif UNITY_IOSif (!mTouching && Input.touchCount == 4){mTouching = true;this.showGUI = !this.showGUI;} else if (Input.touchCount == 0){mTouching = false;}#endifif (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), "清空數據")) {PlayerPrefs.DeleteAll ();#if UNITY_EDITOREditorApplication.isPlaying = false;#elseApplication.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 entryfor (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 itif (collapse && i > 0 && entry.message == entries[i - 1].message) {continue;}// Change the text colour according to the log typeswitch (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 buttonif (GUILayout.Button(clearLabel)) {entries.Clear();}// Collapse togglecollapse = 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 barGUI.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>/// 幀率計算器/// </summary>public class QFPSCounter{// 幀率計算頻率private const float calcRate = 0.5f;// 本次計算頻率下幀數private int frameCount = 0;// 頻率時長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){// 計算幀率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>/// 內存檢測器,目前只是輸出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";// 字節到兆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));}}} 復制代碼

    注意事項:

  • 和上一篇介紹的QLog一樣,需要依賴上上篇文章介紹的QApp。

  • QConsole初步實現來自于開源Unity插件Unity-WWW-Wrapper中的Console.cs.在此基礎上添加了ScrollToBottom選項。因為這個插件的控制臺不支持滾動顯示Log,需要拖拽右邊的scrollBar,很不方便。

  • Unity-WWW-wrapper非常不穩定,建議大家不要使用。倒是感興趣的同學可以研究下實現,貼上地址:https://www.assetstore.unity3d.com/en/#!/content/19116。

  • 歡迎討論!

    相關鏈接:

    我的框架地址:https://github.com/liangxiegame/QFramework

    教程源碼:https://github.com/liangxiegame/QFramework/tree/master/Assets/HowToWriteUnityGameFramework/

    QFramework&游戲框架搭建QQ交流群: 623597263

    轉載請注明地址:涼鞋的筆記http://liangxiegame.com/

    微信公眾號:liangxiegame

    如果有幫助到您:

    如果覺得本篇教程對您有幫助,不妨通過以下方式贊助筆者一下,鼓勵筆者繼續寫出更多高質量的教程,也讓更多的力量加入 QFramework 。

    • 購買 gitchat 話題《Unity 游戲框架搭建:資源管理 與 ResKit 精講》
      • 價格: 6 元,會員免費
      • 地址: http://gitbook.cn/gitchat/activity/5b29df073104f252297a779c
    • 給 QFramework 一個 Star
      • 地址: https://github.com/liangxiegame/QFramework
    • 給 Asset Store 上的 QFramework 并給個五星(需要先下載)
      • 地址: http://u3d.as/SJ9
    • 購買 gitchat 話題《Unity 游戲框架搭建:我所理解的框架》
      • 價格: 6 元,會員免費
      • 地址: http://gitbook.cn/gitchat/activity/5abc3f43bad4f418fb78ab77
    • 購買同名電子書 :https://www.kancloud.cn/liangxiegame/unity_framework_design( 29.9 元,內容會在 2018 年 10 月份完結)

    總結

    以上是生活随笔為你收集整理的Unity 游戏框架搭建 (九) 减少加班利器-QConsole的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。