您好,登錄后才能下訂單哦!
Preview中怎么利用Unity3D打印日志,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xià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打印日志的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。