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目錄,然后把下面的腳本放進去
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è)資訊頻道,感謝各位的閱讀!