How unity3d prints logs in Preview


In addition to previewing the model, we can also do other operations in the preview window.

Today, let’s write a gadget to display debugging information in the preview window.

You can see the following figure, which also prints the logs of health and power. It is much more comfortable to display them in preview than in console.

On the left is displayed in console and on the right is displayed in preview window.

Create the editor directory and put the following script in it

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

Overload the toString () function in the class where we need to print logs and return the content that needs to be output in the preview.

The following is the example of the screenshot above. A player class returns the output of health and power in the tostring() function.

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;

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]