[Unity] Display Debug.Log on TextMesh Pro

2 minute read

Overview

This is the TextMeshPro version of the previously created “[Unity] Debug.Log is displayed in Text”.
It can be used for both TextMesh Pro and TextMesh Pro UGUI. (Click here for the gist page](https://gist.github.com/udonba/c944254217dbb5cf2a170b453b9e222e))
Image from Gyazo

function

–Display the output of Debug.Log in Text of UI
–Automatically scrolls (always displays the latest logs and erases old ones)
-(ON / OFF possible) Display with a time stamp at the beginning of the log
-(ON / OFF possible) Change the color according to the log type (error, warning, normal)
-(ON / OFF possible) Do not display log messages containing a specific character string

(Same as [Unity] Display Debug.Log in Text)

Introduction

  1. Create ** LogPrompter.cs ** in your project
  2. Attach LogPrompter.cs to a TextMeshPro (or TextMeshProUGUI) object
    image.png

Usage / Caution

–Add a string to Ignore Phrases to hide logs that contain a specific string.
image.png

–Since the font asset (LiberationSans) that comes with TextMeshPro cannot display Japanese,
If you want to display Japanese, create a font asset by referring to “Procedure for using Japanese with Unity Text Mesh Pro assets” etc. ..

Commentary

–Automatic scrolling
Scrolls by adding the new log to the end of the string and removing the old log from the beginning of the string.

–Judgment of whether it is out of the frame
You can get the index of the first character that runs out from the firstOverflowCharacterIndex property.
LogPrompter determines if this is -1 or not.
The S with a light blue cross in the figure below is the character calculated back from the firstOverflowCharacterIndex.
image.png

  • ** This index is not a text string index, but CharactorInfo field (array) of TMP_TextInfo class index **. Since “\ <color = red> \ </color>” etc. are not included in characterInfo, it is often not possible to use it like textMeshPro.text [firstOverflowCharacterIndex].

–Count the number of protruding logs
In order to delete the beginning of the character string so that the character string fits neatly in the frame, count how many logs the character string in the protruding part is made up of.
However, ** TextInfo does not leave the information of the original string, so even if you know which character is out of the way, there is no way to know where it corresponds to the original string **.
So, it’s a little hard work … Count the number of logs with the line feed code in charactorInfo. Even if the original log does not contain a line feed code, you can calculate the number of logs backwards because you added the line feed code yourself when expanding to TextMeshPro.text.
image.png

reference

-[Unity] How to get text excluding rich text with TextMesh Pro

History

–Created by 2020-09-04