-
C#教程之Unity3D在Preview中打印日志的方法
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
Preview窗口除了可以预览模型之外,我们还可以做别的操作。
今天我们来写个小工具在Preview窗口中显示调试信息。
可以看下面的图,同样是打印 health 和 power 的日志,在 Preview 中显示比在 Console 中显示舒服多了。
左边是Console中显示,右边是Preview窗口中显示。
创建Editor目录,然后把下面的脚本放进去
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
using UnityEngine; using UnityEditor; [CustomEditor( typeof (Object), true )] public class PreviewGUIEditor : Editor { /** Update every 15th frame. */ private const int updateOnFrame = 15; private GUIStyle _previewLabelStyle; private GUIStyle previewLabelStyle { get { if (_previewLabelStyle == null ) { _previewLabelStyle = new GUIStyle( "PreOverlayLabel" ) { richText = false , alignment = TextAnchor.UpperLeft, fontStyle = FontStyle.Normal }; // Try to get a fixed-width font on macOS. var font = Font.CreateDynamicFontFromOSFont( "Monaco" , 12); // Failing that, try to get a fixed-width font on Windows. if (font == null ) font = Font.CreateDynamicFontFromOSFont( "Lucida Console" , 12); // XXX What fixed-width font should I request if we're on Linux? if (font != null ) _previewLabelStyle.font = font; // Debug.Log("Fonts: \n" + string.Join("\n", Font.GetOSInstalledFontNames())); } return _previewLabelStyle; } } public override bool HasPreviewGUI() { return Application.isPlaying; } public override bool RequiresConstantRepaint() { // Only repaint on the nth frame. return Application.isPlaying && Time.frameCount % updateOnFrame == 0; } public override void OnPreviewGUI(Rect rect, GUIStyle background) { string str = target.ToString(); GUI.Label(rect, str, previewLabelStyle); } } |
在我们需要打印日志的类里面 重载ToString()函数,返回需要在preview中输出的内容。
下面是上面截图的示例,一个Player类,在ToString()函数中返回了 health 和 power的输出内容。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
using UnityEngine; public class Player : MonoBehaviour { public int health = 10; public int power = 10; // Use this for initialization void Start () { } // Update is called once per frame void Update () { health += 1; power += 2; Debug.LogError( "health = " + health); Debug.LogError( "power = " + power); } public override string ToString() { return "health = " + health+ "\n" + "power = " + power; } } |
栏目列表
最新更新
C# 面向对象
假设客车的座位数是9行4列,使用二维数
C#基于接口设计三层架构Unity篇
C#线程 入门
C#读取静态类常量属性和值
C# 插件式编程
C# 委托与事件有啥区别?
C#队列学习笔记:队列(Queue)和堆栈(Stack
linq 多表分组左连接查询查询统计
C#队列学习笔记:MSMQ入门一
C# 在Word中添加Latex 数学公式和符号
inncheck命令 – 检查语法
基于UDP的服务器端和客户端
再谈UDP和TCP
在socket编程中使用域名
网络数据传输时的大小端问题
socket编程实现文件传输功能
如何优雅地断开TCP连接?
图解TCP四次握手断开连接
详细分析TCP数据的传输过程
SqlServer 利用游标批量更新数据
BOS只读状态修改
SQL Server等待事件—PAGEIOLATCH_EX
数据库多行转换为单一列
获取数据表最后最后访问,修改,更新,
计算经历的时间
SQL查询结果自定义排序
修改数据库默认位置
日期简单加或减
从日期获取年,月或日