服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity3D在Preview中打印日志的方法

Unity3D在Preview中打印日志的方法

2022-08-05 11:25UnityAsk C#

这篇文章主要为大家详细介绍了Unity3D在Preview中打印日志的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Preview窗口除了可以预览模型之外,我们还可以做别的操作。

今天我们来写个小工具在Preview窗口中显示调试信息。

可以看下面的图,同样是打印 health 和 power 的日志,在 Preview 中显示比在 Console 中显示舒服多了。

左边是Console中显示,右边是Preview窗口中显示。

Unity3D在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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/piai9568/article/details/99660327

延伸 · 阅读

精彩推荐
  • C#Unity实现物体左右移动效果

    Unity实现物体左右移动效果

    这篇文章主要为大家详细介绍了Unity实现物体左右移动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    _April_10672022-08-03
  • C#深入浅出23种设计模式

    深入浅出23种设计模式

    本文给大家详解23种设计模式,理解设计模式有助于在程序开发过程中灵活应用,需要的朋友可以参考下...

    编程技术网10742021-10-22
  • C#C#实现缩放和剪裁图片的方法示例

    C#实现缩放和剪裁图片的方法示例

    这篇文章主要介绍了C#实现缩放和剪裁图片的方法,结合实例形式分析了C#针对图片属性的缩放、裁剪等相关操作技巧,需要的朋友可以参考下...

    songkexin5142022-01-11
  • C#10个C#程序员经常用到的实用代码片段

    10个C#程序员经常用到的实用代码片段

    如果你是一个C#程序员,那么本文介绍的10个C#常用代码片段一定会给你带来帮助,从底层的资源操作,到上层的UI应用,这些代码也许能给你的开发节省不...

    C#教程网4362021-10-27
  • C#一个状态机的实现

    一个状态机的实现

    本文主要介绍了C#实现一个状态机的思路与方法,具有很好的参考价值,下面跟着小编一起来看下吧...

    诺贝尔8222021-12-22
  • C#Winform界面中实现通用工具栏按钮的事件处理方法

    Winform界面中实现通用工具栏按钮的事件处理方法

    下面小编就为大家分享一篇Winform界面中实现通用工具栏按钮的事件处理方法,具有很好的参考价值,希望对大家有所帮助...

    伍华聪12092022-02-13
  • C#解析C#的扩展方法

    解析C#的扩展方法

    在本文中,主要对扩展方法进行了一些规则说明、声明方式,使用方式,以及对扩展方法的意义和扩展方法的原理进行了简单的解答。并在本文的最后给了...

    彭泽090211622021-12-13
  • C#C#开源的AOP框架--KingAOP基础

    C#开源的AOP框架--KingAOP基础

    这篇文章主要介绍了一款C#开源的AOP框架--KingAOP框架的基础知识,对于想学习AOP的小伙伴来说,非常不错,希望大家能够喜欢。...

    JackWang-CUMT3812021-11-05