温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Preview中怎么利用Unity3D打印日志

发布时间:2021-06-16 14:57:47 来源:亿速云 阅读:177 作者:Leah 栏目:编程语言

Preview中怎么利用Unity3D打印日志,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

看完上述内容,你们掌握Preview中怎么利用Unity3D打印日志的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI