日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

preview窗口 unity_Unity3D在Preview中打印日志的方法

發布時間:2024/7/19 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 preview窗口 unity_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;

}

}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

總結

以上是生活随笔為你收集整理的preview窗口 unity_Unity3D在Preview中打印日志的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。