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

为毛要实现这个工具?

  1. 在我小时候,每当游戏在真机运行时,我们看到的日志是这样的。

    没高亮啊,还有乱七八糟的堆栈信息,好干扰日志查看,好影响心情。

  2. 还有就是必须始终连着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));}}}
复制代码

注意事项:

  1. 和上一篇介绍的QLog一样,需要依赖上上篇文章介绍的QApp。

  2. QConsole初步实现来自于开源Unity插件Unity-WWW-Wrapper中的Console.cs.在此基础上添加了ScrollToBottom选项。因为这个插件的控制台不支持滚动显示Log,需要拖拽右边的scrollBar,很不方便。

  3. 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 月份完结)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/282764.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android蓝牙多次后,android – 如何防止BluetoothGattCallback一次多次执行

我的服务有一个BluetoothGattCallback实例public class MyService extends Service {private BluetoothGattCallback callback;Overridepublic void onCreate() {super.onCreate();callback new BluetoothGattCallback() {Overridepublic synchronized void onConnectionState…

美观又实用,10 款强大的开源 Javascript 图表库

2019独角兽企业重金招聘Python工程师标准>>> 随着发展&#xff0c;现代 Web 设计在改善体验和功能的同时&#xff0c;对于美观的追求也越来越高&#xff0c;可视化、交互式、动态等元素和效果似乎已成为标配。 以下是为开发者推荐的 10 款开源 Javascript 图表库&am…

EF CORE 7 RC1 发布

原文链接&#xff1a;https://devblogs.microsoft.com/dotnet/announcing-ef7-rc1/[1]原文作者&#xff1a;Jeremy Likness翻译&#xff1a;沙漠尽头的狼(谷歌翻译加持)Entity Framework Core 7 (EF7) Release Candidate 1 已发布&#xff01;该团队专注于解决缺陷、小幅改进以…

0 重新学习Ubuntu -- 这一段没怎么学习

在完成了前面的几个学习后&#xff0c;再没有进行系统的学习。 虽然在真机上安装系统&#xff0c;每天都打开&#xff0c;完成以下的工作&#xff1a; 升级软件用来查看相关的网站在Ubuntu上&#xff0c;现在可以完成办公、上网、娱乐。 但专业的学习&#xff0c;例如编程方面进…

自定义地图怎么做成html,自定义html为谷歌地图制作标记

好吧&#xff0c;似乎Custom Overlays会做我想要的。这是ping层&#xff1a;function PingLayer(bounds, map) {this.bounds bounds;this.setMap(map);}PingLayer.prototype new google.maps.OverlayView();PingLayer.prototype.onAdd function() {var div document.create…

HDU5248:序列变换(二分)

序列变换 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1348 Accepted Submission(s): 593Problem Description给定序列A{A1,A2,...,An}, 要求改变序列A中的某些元素&#xff0c;形成一个严格单调的序列B&am…

微服务太分散?使用Fundebug集中式bug监控

摘要&#xff1a; 微服务日志分散&#xff0c;可以使用Fundebug的异常监控将它们集中起来。 当一个项目复杂到一定程度&#xff0c;功能越来越多&#xff0c;随之对应的模块也越来越多。 如果都放在一个大的项目下面&#xff0c;共同开发&#xff0c;整合发布&#xff0c;那么会…

html404页面怎么添加,网站要如何设置自定义404页面?

之前我们讲述过网站设置404页面对于优化或是用户体验的重要意义&#xff0c;大家可移步到《网站为什么要设置404页面》查看&#xff0c;今天我们讲解的是网站要如何设置自己的404页面。现在大多数空间商都有了404设置的功能&#xff0c;我们可将404页面上传至空间里面&#xff…

设计模式之——工厂方法模式

1、工厂方法模式&#xff08;Factory Method&#xff09;工厂方法模式分为三种&#xff1a;11、普通工厂模式&#xff0c;就是建立一个工厂类&#xff0c;对实现了同一接口的一些类进行实例的创建。首先看下关系图&#xff1a;举例如下&#xff1a;&#xff08;我们举一个发送邮…

记一次性能故障排查

最近一次公司服务出了一些性能的问题&#xff0c;主要是内存不释放。领到任务后就开始展开工作。项目是用.net core 6写的&#xff0c;在框上应该不会有什么问题&#xff0c;这是大背景。另外服务是部署在k8s上的&#xff0c;于是就和性能测试人员&#xff0c;开发人员搭测试环…

html单选框 点击取消选中,radio单选框再点击取消选中

html:html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">单选框选项a选项b选项c选项dcheckradio.js://参数&#xff1a;obj为当前点击的radio对象function onClickRadioStyle(obj){var…

开启AngularJS 1.X的学习之路(1)

概念(1) AngularJS 应用 AngularJS 模块&#xff08;Module&#xff09; 定义了 AngularJS 应用。AngularJS 控制器&#xff08;Controller&#xff09; 用于控制 AngularJS 应用。ng-app指令定义了应用, ng-controller 定义了控制器。eg: <div ng-app"myApp" ng-…

Hello boke!

Hello boke&#xff01;转载于:https://www.cnblogs.com/yikuan-919/p/9319071.html

ASP.NET Core在.NET 7 RC1中的更新

原文链接&#xff1a;https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-rc-1/[1]原文作者&#xff1a;Daniel Roth翻译&#xff1a;沙漠尽头的狼(谷歌翻译加持).NET 7 Release Candidate 1 (RC1) 现已推出[2]&#xff0c;其中包括对 ASP.NET Core 的许…

html5 tab菜单切换页面,11个常用的jQuery TAB切换菜单源码及制作教程

11个常用的jQuery TAB切换菜单源码及制作教程SponsorTAB切换式菜单可以方便为我们减少很多网页布局空间&#xff0c;而且用jQuery的话可以加入一些动画效果&#xff0c;比如渐变&#xff0c;向左右滑动等&#xff0c;提升一定的用户体验&#xff0c;所以TAB菜单目前来说是很流行…

7.16 10.19-10.22

10.19 iptables规则备份和恢复[roothyc-01-01 ~]# service iptables save 保存iptables规则该命令会将规则保存在/etc/sysconfig/iptables将iptables规则备份到一个文件中[roothyc-01-01 ~]# iptables-save>/tmp/ipt.txt将iptables规则备份到ipt.txt文件中从备份规则的文件恢…

走进javascript——不起眼的基础,值和分号

值 有时我很想知道javascript解析引擎是如何区分一个变量的值&#xff0c;比如下面这段代码。 var x javascript; //javascript x "hello"; // hello x 555; //555 x null; //null x a; //a is not defined x true; //true 对于数字是直接赋值的&#xff0c;因…

ConcurrentDictionary字典操作竟然不全是线程安全的?

好久不见&#xff0c;马甲哥封闭居家半个月&#xff0c;记录之前遇到的一件小事。ConcurrentDictionary<TKey,TValue>绝大部分api都是线程安全的[1]&#xff0c;唯二的例外是接收工厂函数的api&#xff1a;AddOrUpdate、GetOrAdd&#xff0c;这两个api不是线程安全的&…

码农小汪-Hibernate学习8-hibernate关联关系注解表示@OneToMany mappedBy @ManyToMany @JoinTable...

近期我也是有点郁闷&#xff0c;究竟是程序中处理关联关系。还是直接使用外键处理关联关系呢&#xff1f;这个的说法不一致&#xff01;程序中处理这样的关联关系的话。自己去维护这样的约束。这样的非常乐观的一种做法&#xff01;或者是直接在数据库中处理这样的直接的外键关…

HTML中弹窗中加入图片,javascript里怎么实现点击图片弹出对话框?

JavaScript中可以使用document.getElementsByTagName方法后去img标签&#xff0c;然后遍历所有img标签并为其添加点击事件实现点击弹出对话框。JavaScript实现点击图片弹出对话框&#xff1a;img {width: 500px;height: 300px;}//获取所有的img标签var imgObjs document.getEl…