Unity3D在Preview中打印日志的方法

 更新时间:2019年09月17日 17:22:54   作者:UnityAsk  
这篇文章主要为大家详细介绍了Unity3D在Preview中打印日志的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

您可能感兴趣的文章:

相关文章

  • C#使用自定义的泛型节点类实现二叉树类

    C#使用自定义的泛型节点类实现二叉树类

    这篇文章主要为大家详细介绍了C#如何使用自定义的泛型节点类 Node<T>实现二叉树类BinaryTree<T>及其方法,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-03-03
  • c#高效的线程安全队列ConcurrentQueue<T>的实现

    c#高效的线程安全队列ConcurrentQueue<T>的实现

    这篇文章主要介绍了c#高效的线程安全队列ConcurrentQueue<T>的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • C#使用 Salt + Hash 来为密码加密

    C#使用 Salt + Hash 来为密码加密

    本文主要介绍了几种常见的破解密码的方法,为密码加盐(Salt)以及在.NET中的实现等。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C# winform实现右下角弹出窗口结果的方法

    C# winform实现右下角弹出窗口结果的方法

    这篇文章主要介绍了C# winform实现右下角弹出窗口结果的方法,结合实例形式分析了C#窗口操作的相关技巧,需要的朋友可以参考下
    2017-06-06
  • C#扩展方法实例分析

    C#扩展方法实例分析

    这篇文章主要介绍了C#扩展方法,结合实例形式分析了C#扩展方法的功能、使用方法及相关注意事项,需要的朋友可以参考下
    2017-06-06
  • 如何用C#实现SAGA分布式事务

    如何用C#实现SAGA分布式事务

    大家好,本篇文章主要讲的是如何用C#实现SAGA分布式事务,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • C#中使用HttpPost调用WebService的方法

    C#中使用HttpPost调用WebService的方法

    这篇文章介绍了C#中使用HttpPost调用WebService的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-03-03
  • C#中is和as用法实例分析

    C#中is和as用法实例分析

    这篇文章主要介绍了C#中is和as用法实例分析,需要的朋友可以参考下
    2014-08-08
  • C#二分查找算法

    C#二分查找算法

    这篇文章介绍了C#中的二分查找算法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05
  • C#操作注册表之RegistryKey类

    C#操作注册表之RegistryKey类

    这篇文章介绍了C#操作注册表之RegistryKey类,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论