UnityEssentials
Small but useful tools and features for Unity
ConsoleInGame/Console.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace UnityEngine
4 {
8  public abstract class Console : MonoBehaviour
9  {
13  internal string fullLog = "";
14 
18  private string filename = "";
19 
23  [Tooltip("If the console must be displayed or not.")]
24  [SerializeField] protected bool show = true;
25 
29  [Tooltip("Should the console toggle on and off pressing the 'Toggle key'?")]
30  [SerializeField] protected bool enableKeyToggle = true;
31 
35  [Tooltip("Key to toggle on-off the in-game console.")]
36  [SerializeField] public KeyCode toggleKey = KeyCode.Space;
37 
41  [Tooltip("Should the logs be saved to a file in the desktop?")]
42  [SerializeField] public bool saveToFile = false;
43 
47  [Tooltip("Maximum amount of characters displayed by the console.")]
48  [SerializeField] public int maxDisplayedChars = 700;
49 
53  protected void OnEnable() { Application.logMessageReceived += Log; UpdateVisuals(); }
57  protected void OnDisable() { Application.logMessageReceived -= Log; }
61  protected void Awake() { UpdateVisuals(); }
62 
66  public void ToggleShow()
67  {
68  SetShow(!show);
69  }
70 
75  public void SetShow(bool newValue)
76  {
77  show = newValue;
78  UpdateVisuals();
79  }
80 
84  private void Update()
85  {
86  if (enableKeyToggle && Input.GetKeyDown(toggleKey))
87  {
88  ToggleShow();
89  }
90  }
91 
95  private void Log(string logString, string stackTrace, LogType type)
96  {
97  // Keep track of the full log
98  fullLog = fullLog + "\n" + logString;
99  if (fullLog.Length > maxDisplayedChars)
100  {
101  fullLog = fullLog.Substring(fullLog.Length - maxDisplayedChars);
102  }
103 
104 
105  // Save the log in a file
106  if (saveToFile)
107  {
108  if (filename == "")
109  {
110  string desktopFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop) + "/Unity_Console_Logs";
111  System.IO.Directory.CreateDirectory(desktopFolder);
112  string date = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
113  filename = desktopFolder + $"/log_{Utils.GetProjectName()}_{date}.txt";
114  }
115  try
116  {
117  System.IO.File.AppendAllText(filename, logString + "\n\n");
118  }
119  catch (Exception e)
120  {
121  Debug.LogError($"Console logs could not be saved:\n{e.Message}");
122  }
123  }
124 
125  UpdateVisuals();
126  }
127 
131  protected abstract void UpdateVisuals();
132 
136  public void Clear()
137  {
138  fullLog = "";
139  filename = "";
140  UpdateVisuals();
141  }
142  }
143 
144 }
Base class to handle the console display in-game
void OnDisable()
Unregisters the console from the handling of the any log messages.
bool show
If the console must be displayed or not.
bool enableKeyToggle
Should the console toggle on and off pressing the toggleKey?"
void OnEnable()
Registers the console to handle any log messages received and updates the visuals calling UpdateVisua...
int maxDisplayedChars
Maximum amount of characters displayed by the console.
KeyCode toggleKey
Key to toggle on-off the in-game console.
void SetShow(bool newValue)
Displays or not the console in-game
string fullLog
The log messages of the console.
void ToggleShow()
Inverts the "show" state of the console. If it was being displayed, it will no longer be....
void Clear()
Clears the console and starts a new file for the saved logs.
void Log(string logString, string stackTrace, LogType type)
Handles the logging of any log messages
abstract void UpdateVisuals()
Updates the visuals of the console
void Update()
Handles the toggling ON and OFF of the console
void Awake()
Updates the console's visuals calling UpdateVisuals
bool saveToFile
Should the logs be saved to a file in the desktop?
string filename
The filename that will store the logs of the console.