您好,登錄后才能下訂單哦!
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打印日志的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。