-
-
Notifications
You must be signed in to change notification settings - Fork 998
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ConsoleViewer): include in VRTK namespace
The Console Viewer script should also be part of the VRTK namespace to conform with the other VRTK prefab scripts.
- Loading branch information
1 parent
a8821a9
commit 6f45bf2
Showing
1 changed file
with
90 additions
and
87 deletions.
There are no files selected for viewing
177 changes: 90 additions & 87 deletions
177
Assets/VRTK/Prefabs/Resources/Scripts/VRTK_ConsoleViewer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,113 +1,116 @@ | ||
// Console Viewer Canvas|Prefabs|0060 | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// This canvas adds the unity console log to a world game object. To use the prefab, it simply needs to be placed into the scene and it will be visible in world space. It's also possible to child it to other objects such as the controller so it can track where the user is. | ||
/// </summary> | ||
/// <remarks> | ||
/// It's also recommended to use the Simple Pointer and UI Pointer on a controller to interact with the Console Viewer Canvas as it has a scrollable text area, a button to clear the log and a checkbox to toggle whether the log messages are collapsed. | ||
/// </remarks> | ||
public class VRTK_ConsoleViewer : MonoBehaviour | ||
namespace VRTK | ||
{ | ||
[Tooltip("The size of the font the log text is displayed in.")] | ||
public int fontSize = 14; | ||
[Tooltip("The colour of the text for an info log message.")] | ||
public Color infoMessage = Color.black; | ||
[Tooltip("The colour of the text for an assertion log message.")] | ||
public Color assertMessage = Color.black; | ||
[Tooltip("The colour of the text for a warning log message.")] | ||
public Color warningMessage = Color.yellow; | ||
[Tooltip("The colour of the text for an error log message.")] | ||
public Color errorMessage = Color.red; | ||
[Tooltip("The colour of the text for an exception log message.")] | ||
public Color exceptionMessage = Color.red; | ||
|
||
private Dictionary<LogType, Color> logTypeColors; | ||
private ScrollRect scrollWindow; | ||
private RectTransform consoleRect; | ||
private Text consoleOutput; | ||
private const string NEWLINE = "\n"; | ||
private int lineBuffer = 50; | ||
private int currentBuffer; | ||
private string lastMessage; | ||
private bool collapseLog = false; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// The SetCollapse method determines whether the console will collapse same message output into the same line. A state of `true` will collapse messages and `false` will print the same message for each line. | ||
/// This canvas adds the unity console log to a world game object. To use the prefab, it simply needs to be placed into the scene and it will be visible in world space. It's also possible to child it to other objects such as the controller so it can track where the user is. | ||
/// </summary> | ||
/// <param name="state">The state of whether to collapse the output messages, true will collapse and false will not collapse.</param> | ||
public void SetCollapse(bool state) | ||
/// <remarks> | ||
/// It's also recommended to use the Simple Pointer and UI Pointer on a controller to interact with the Console Viewer Canvas as it has a scrollable text area, a button to clear the log and a checkbox to toggle whether the log messages are collapsed. | ||
/// </remarks> | ||
public class VRTK_ConsoleViewer : MonoBehaviour | ||
{ | ||
collapseLog = state; | ||
} | ||
[Tooltip("The size of the font the log text is displayed in.")] | ||
public int fontSize = 14; | ||
[Tooltip("The colour of the text for an info log message.")] | ||
public Color infoMessage = Color.black; | ||
[Tooltip("The colour of the text for an assertion log message.")] | ||
public Color assertMessage = Color.black; | ||
[Tooltip("The colour of the text for a warning log message.")] | ||
public Color warningMessage = Color.yellow; | ||
[Tooltip("The colour of the text for an error log message.")] | ||
public Color errorMessage = Color.red; | ||
[Tooltip("The colour of the text for an exception log message.")] | ||
public Color exceptionMessage = Color.red; | ||
|
||
/// <summary> | ||
/// The ClearLog method clears the current log view of all messages | ||
/// </summary> | ||
public void ClearLog() | ||
{ | ||
consoleOutput.text = ""; | ||
currentBuffer = 0; | ||
lastMessage = ""; | ||
} | ||
private Dictionary<LogType, Color> logTypeColors; | ||
private ScrollRect scrollWindow; | ||
private RectTransform consoleRect; | ||
private Text consoleOutput; | ||
private const string NEWLINE = "\n"; | ||
private int lineBuffer = 50; | ||
private int currentBuffer; | ||
private string lastMessage; | ||
private bool collapseLog = false; | ||
|
||
private void Awake() | ||
{ | ||
logTypeColors = new Dictionary<LogType, Color>() | ||
/// <summary> | ||
/// The SetCollapse method determines whether the console will collapse same message output into the same line. A state of `true` will collapse messages and `false` will print the same message for each line. | ||
/// </summary> | ||
/// <param name="state">The state of whether to collapse the output messages, true will collapse and false will not collapse.</param> | ||
public void SetCollapse(bool state) | ||
{ | ||
collapseLog = state; | ||
} | ||
|
||
/// <summary> | ||
/// The ClearLog method clears the current log view of all messages | ||
/// </summary> | ||
public void ClearLog() | ||
{ | ||
consoleOutput.text = ""; | ||
currentBuffer = 0; | ||
lastMessage = ""; | ||
} | ||
|
||
private void Awake() | ||
{ | ||
logTypeColors = new Dictionary<LogType, Color>() | ||
{ | ||
{ LogType.Assert, assertMessage }, | ||
{ LogType.Error, errorMessage }, | ||
{ LogType.Exception, exceptionMessage }, | ||
{ LogType.Log, infoMessage }, | ||
{ LogType.Warning, warningMessage } | ||
}; | ||
scrollWindow = transform.FindChild("Panel/Scroll View").GetComponent<ScrollRect>(); | ||
consoleRect = transform.FindChild("Panel/Scroll View/Viewport/Content").GetComponent<RectTransform>(); | ||
consoleOutput = transform.FindChild("Panel/Scroll View/Viewport/Content/ConsoleOutput").GetComponent<Text>(); | ||
|
||
consoleOutput.fontSize = fontSize; | ||
ClearLog(); | ||
} | ||
|
||
private void OnEnable() | ||
{ | ||
Application.logMessageReceived += HandleLog; | ||
} | ||
scrollWindow = transform.FindChild("Panel/Scroll View").GetComponent<ScrollRect>(); | ||
consoleRect = transform.FindChild("Panel/Scroll View/Viewport/Content").GetComponent<RectTransform>(); | ||
consoleOutput = transform.FindChild("Panel/Scroll View/Viewport/Content/ConsoleOutput").GetComponent<Text>(); | ||
|
||
private void OnDisable() | ||
{ | ||
Application.logMessageReceived -= HandleLog; | ||
consoleRect.sizeDelta = Vector2.zero; | ||
} | ||
consoleOutput.fontSize = fontSize; | ||
ClearLog(); | ||
} | ||
|
||
private string GetMessage(string message, LogType type) | ||
{ | ||
var color = ColorUtility.ToHtmlStringRGBA(logTypeColors[type]); | ||
return "<color=#" + color + ">" + message + "</color>" + NEWLINE; | ||
} | ||
private void OnEnable() | ||
{ | ||
Application.logMessageReceived += HandleLog; | ||
} | ||
|
||
private void HandleLog(string message, string stackTrace, LogType type) | ||
{ | ||
var logOutput = GetMessage(message, type); | ||
private void OnDisable() | ||
{ | ||
Application.logMessageReceived -= HandleLog; | ||
consoleRect.sizeDelta = Vector2.zero; | ||
} | ||
|
||
if (!collapseLog || lastMessage != logOutput) | ||
private string GetMessage(string message, LogType type) | ||
{ | ||
consoleOutput.text += logOutput; | ||
lastMessage = logOutput; | ||
var color = ColorUtility.ToHtmlStringRGBA(logTypeColors[type]); | ||
return "<color=#" + color + ">" + message + "</color>" + NEWLINE; | ||
} | ||
|
||
consoleRect.sizeDelta = new Vector2(consoleOutput.preferredWidth, consoleOutput.preferredHeight); | ||
scrollWindow.verticalNormalizedPosition = 0; | ||
currentBuffer++; | ||
if (currentBuffer >= lineBuffer) | ||
private void HandleLog(string message, string stackTrace, LogType type) | ||
{ | ||
var lines = Regex.Split(consoleOutput.text, NEWLINE).Skip(lineBuffer / 2); | ||
consoleOutput.text = string.Join(NEWLINE, lines.ToArray()); | ||
currentBuffer = lineBuffer / 2; | ||
var logOutput = GetMessage(message, type); | ||
|
||
if (!collapseLog || lastMessage != logOutput) | ||
{ | ||
consoleOutput.text += logOutput; | ||
lastMessage = logOutput; | ||
} | ||
|
||
consoleRect.sizeDelta = new Vector2(consoleOutput.preferredWidth, consoleOutput.preferredHeight); | ||
scrollWindow.verticalNormalizedPosition = 0; | ||
currentBuffer++; | ||
if (currentBuffer >= lineBuffer) | ||
{ | ||
var lines = Regex.Split(consoleOutput.text, NEWLINE).Skip(lineBuffer / 2); | ||
consoleOutput.text = string.Join(NEWLINE, lines.ToArray()); | ||
currentBuffer = lineBuffer / 2; | ||
} | ||
} | ||
} | ||
} |