8 public abstract class Console : MonoBehaviour
23 [Tooltip(
"If the console must be displayed or not.")]
24 [SerializeField]
protected bool show =
true;
29 [Tooltip(
"Should the console toggle on and off pressing the 'Toggle key'?")]
35 [Tooltip(
"Key to toggle on-off the in-game console.")]
36 [SerializeField]
public KeyCode
toggleKey = KeyCode.Space;
41 [Tooltip(
"Should the logs be saved to a file in the desktop?")]
47 [Tooltip(
"Maximum amount of characters displayed by the console.")]
57 protected void OnDisable() { Application.logMessageReceived -=
Log; }
95 private void Log(
string logString,
string stackTrace, LogType type)
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";
117 System.IO.File.AppendAllText(
filename, logString +
"\n\n");
121 Debug.LogError($
"Console logs could not be saved:\n{e.Message}");
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.