Preview中怎么利用Unity3D打印日志

Preview中怎么利用Unity3D打印日志,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了烏魯木齊免費建站歡迎大家使用!

創(chuàng)建Editor目錄,然后把下面的腳本放進(jìn)去

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()函數(shù),返回需要在preview中輸出的內(nèi)容。

下面是上面截圖的示例,一個Player類,在ToString()函數(shù)中返回了 health 和 power的輸出內(nèi)容。

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

看完上述內(nèi)容,你們掌握Preview中怎么利用Unity3D打印日志的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站欄目:Preview中怎么利用Unity3D打印日志
網(wǎng)頁路徑:http://www.muchs.cn/article38/jpcssp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、網(wǎng)站排名定制網(wǎng)站、App設(shè)計網(wǎng)站策劃、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化