美文网首页
Unity3D 在Preview 中打印日志

Unity3D 在Preview 中打印日志

作者: UnityAsk | 来源:发表于2019-08-15 22:27 被阅读0次

Preview窗口除了可以预览模型之外,我们还可以做别的操作。
今天我们来写个小工具在Preview窗口中显示调试信息。
可以看下面的图,同样是打印 health 和 power 的日志,在 Preview 中显示比在 Console 中显示舒服多了。
左边是Console中显示,右边是Preview窗口中显示。

创建Editor目录,然后把下面的脚本放进去

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的输出内容。

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

相关文章

  • Unity3D 在Preview 中打印日志

    Preview窗口除了可以预览模型之外,我们还可以做别的操作。今天我们来写个小工具在Preview窗口中显示调试信...

  • Nacos 大量日志、服务端频繁打印日志、客户端频繁打印日志、日

    日志打印频繁的问题 在老的 Nacos 版本中,往往会有大量的无效日志打印,这些日志的打印会迅速占用完用户的磁盘空...

  • apollo配置日志不生效(springboot项目中)

    问题原因: 在apollo配置文件中配置了日志等级日志打印后,启动项目发现日志没有把对应等级log打印出来; 解决...

  • swift自定义log

    日志的作用 在开发过程中,在代码行间打印日志,可以帮助开发者快速定位想要的信息,需要注意的是打印日志是非常消耗性能...

  • 使用filter_parameter_logging过滤敏感日志

    在rails的日志打印中,我们经常需要对信息进行日志打印过滤,如密码一类的信息。 下面是正常的日志输出: 当我们对...

  • 使用log4js打印日志

    使用log4js打印日志 不管在做任何开发的时候,打印日志都是很重要的功能。在Node.js中,最普通的打日志的方...

  • iOS Log打印

    简述 iOS调试中打印日志是一种比较常见的手段,一般都是使用系统的打印日志的方法NSLog,但是这种打印日志的方法...

  • 在学习swift的一些笔记(一)

    -objective-c与swift打印日志的区别 objective-c中的日志打印代码: 第一行代码是打印的格...

  • Log4j

    类中添加属性 作用 打印日志时显示日志所在的类

  • Swift日志打印的正确姿势

    打印日志是程序员调试代码的无上利器,俗话说“不会打印日志的开发不是好码农”。但在发布环境中,日志打印又会影响代码性...

网友评论

      本文标题:Unity3D 在Preview 中打印日志

      本文链接:https://www.haomeiwen.com/subject/sbivjctx.html