-
Notifications
You must be signed in to change notification settings - Fork 66
/
Console.cs
625 lines (518 loc) · 19.5 KB
/
Console.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
using System;
using System.Collections.Generic;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace Consolation
{
/// <summary>
/// A console to display Unity's debug logs in-game.
///
/// Version: 1.4.1
/// </summary>
public class Console : MonoBehaviour
{
#region Inspector Settings
[Tooltip("Hotkey to show and hide the console.")]
#if ENABLE_INPUT_SYSTEM
public Key toggleKey = Key.Backquote;
#else
public KeyCode toggleKey = KeyCode.BackQuote;
#endif
[Tooltip("Whether to open as soon as the game starts.")]
public bool openOnStart;
[Tooltip("Whether to open the window by shaking the device (mobile-only).")]
public bool shakeToOpen = true;
[Tooltip("Whether to require touches while shaking to avoid accidental shakes.")]
public bool shakeRequiresTouch;
[Tooltip("Acceleration (squared) above which to open the console.")]
public float shakeAcceleration = 3f;
[Tooltip("Number of seconds that need to pass between visibility toggles. This threshold prevents closing again while shaking to open.")]
public float toggleThresholdSeconds = .5f;
[Tooltip("Whether to keep a limited number of logs. Useful if memory usage is a concern.")]
public bool restrictLogCount;
[Tooltip("Number of logs to keep before removing old ones.")]
public int maxLogCount = 1000;
[Tooltip("Whether log messages are collapsed by default or not.")]
public bool collapseLogOnStart;
[Tooltip("Font size to display log entries with.")]
public int logFontSize = 12;
[Tooltip("Amount to scale UI by.")]
public float scaleFactor = 1f;
[Tooltip("Custom styles to apply to window.")]
public GUISkin skin;
#endregion
static readonly GUIContent clearLabel = new GUIContent("Clear", "Clear contents of console.");
static readonly GUIContent onlyLastLogLabel = new GUIContent("Only Last Log", "Show only most recent log.");
static readonly GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
static GUIStyle dividerStyle;
const int margin = 20;
const string windowTitle = "Console";
static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>
{
{ LogType.Assert, Color.white },
{ LogType.Error, Color.red },
{ LogType.Exception, Color.red },
{ LogType.Log, Color.white },
{ LogType.Warning, Color.yellow },
};
bool isCollapsed;
bool isOnlyLastLogVisible;
bool isVisible;
float lastToggleTime;
readonly List<Log> logs = new List<Log>();
Vector2 logsScrollPosition;
readonly ConcurrentQueue<Log> queuedLogs = new ConcurrentQueue<Log>();
int? selectedLogIndex;
Vector2 stackTraceScrollPosition;
readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);
Rect windowRect = new Rect(margin, margin, 0, 0);
readonly Dictionary<LogType, bool> logTypeFilters = new Dictionary<LogType, bool>
{
{ LogType.Assert, true },
{ LogType.Error, true },
{ LogType.Exception, true },
{ LogType.Log, true },
{ LogType.Warning, true },
};
#region MonoBehaviour Messages
void OnDisable()
{
Application.logMessageReceivedThreaded -= HandleLogThreaded;
}
void OnEnable()
{
Application.logMessageReceivedThreaded += HandleLogThreaded;
}
void OnGUI()
{
if (!isVisible)
{
return;
}
var previousGUISkin = GUI.skin;
if (skin != null)
{
GUI.skin = skin;
}
GUI.matrix = Matrix4x4.Scale(Vector3.one * scaleFactor);
windowRect.width = (Screen.width / scaleFactor) - (margin * 2);
windowRect.height = (Screen.height / scaleFactor) - (margin * 2);
windowRect = GUILayout.Window(123456, windowRect, DrawWindow, windowTitle);
GUI.skin = previousGUISkin;
}
void Start()
{
if (collapseLogOnStart)
{
isCollapsed = true;
}
if (openOnStart)
{
isVisible = true;
}
if (shakeRequiresTouch)
{
EnableMultiTouch();
}
// Unity complains if we define this style at the point of declaration.
dividerStyle = new GUIStyle
{
fixedHeight = 1,
margin = new RectOffset(0, 0, 4, 4),
normal = { background = Texture2D.whiteTexture },
};
}
void Update()
{
UpdateQueuedLogs();
if (WasToggleKeyPressed())
{
isVisible = !isVisible;
}
if (shakeToOpen &&
Time.realtimeSinceStartup - lastToggleTime >= toggleThresholdSeconds &&
WasShaken() &&
(!shakeRequiresTouch || WasMultiTouchThresholdExceeded()))
{
isVisible = !isVisible;
lastToggleTime = Time.realtimeSinceStartup;
}
}
#endregion
void DrawLog(int logIndex, GUIStyle logStyle)
{
var log = logs[logIndex];
GUI.contentColor = logTypeColors[log.Type];
if (isCollapsed)
{
// Draw collapsed log with badge indicating count.
GUILayout.BeginHorizontal();
{
GUILayout.Label(log.Message, logStyle);
GUILayout.FlexibleSpace();
GUILayout.Label(log.Count.ToString(), GUI.skin.box);
}
GUILayout.EndHorizontal();
DrawLogSelectButton(logIndex);
}
else
{
var labelCount = isOnlyLastLogVisible ? 1 : log.Count;
for (var i = 0; i < labelCount; i += 1)
{
GUILayout.Label(log.Message, logStyle);
DrawLogSelectButton(logIndex);
}
}
GUI.contentColor = Color.white;
}
void DrawLogList()
{
var logStyle = GUI.skin.label;
logStyle.fontSize = logFontSize;
logsScrollPosition = GUILayout.BeginScrollView(logsScrollPosition);
// Used to determine height of accumulated log labels.
GUILayout.BeginVertical();
{
if (isOnlyLastLogVisible)
{
var lastVisibleLogIndex = GetLastVisibleLogIndex();
if (lastVisibleLogIndex.HasValue)
{
DrawLog(lastVisibleLogIndex.Value, logStyle);
}
}
else
{
for (var logIndex = 0; logIndex < logs.Count; logIndex++)
{
if (!IsLogVisible(logIndex))
{
continue;
}
DrawLog(logIndex, logStyle);
}
}
}
GUILayout.EndVertical();
var innerScrollRect = GUILayoutUtility.GetLastRect();
GUILayout.EndScrollView();
var outerScrollRect = GUILayoutUtility.GetLastRect();
// If we're scrolled to bottom now, guarantee that it continues to be in next cycle.
if (Event.current.type == EventType.Repaint && IsScrolledToBottom(innerScrollRect, outerScrollRect))
{
ScrollToBottom();
}
}
void DrawLogSelectButton(int logIndex)
{
var lastRect = GUILayoutUtility.GetLastRect();
if (GUI.Button(lastRect, GUIContent.none, GUIStyle.none))
{
selectedLogIndex = logIndex;
stackTraceScrollPosition = Vector2.zero;
}
}
void DrawStackTrace()
{
if (!selectedLogIndex.HasValue)
{
return;
}
GUILayout.Box(GUIContent.none, dividerStyle);
// GUILayout shrinks the stack trace scroll view every time a new log gets added.
// We seem to need to set a fixed height to work around this.
var scrollViewHeight = windowRect.height / 2;
stackTraceScrollPosition = GUILayout.BeginScrollView(stackTraceScrollPosition, GUILayout.Height(scrollViewHeight));
{
var selectedLog = logs[selectedLogIndex.Value];
GUILayout.Label(selectedLog.Message);
GUILayout.Label(selectedLog.StackTrace);
}
GUILayout.EndScrollView();
if (GUILayout.Button("Hide Stack Trace", GUILayout.ExpandWidth(false)))
{
selectedLogIndex = null;
}
}
void DrawToolbar()
{
GUILayout.BeginHorizontal();
{
if (GUILayout.Button(clearLabel))
{
logs.Clear();
selectedLogIndex = null;
}
foreach (LogType logType in Enum.GetValues(typeof(LogType)))
{
var currentState = logTypeFilters[logType];
var label = logType.ToString();
logTypeFilters[logType] = GUILayout.Toggle(currentState, label, GUILayout.ExpandWidth(false));
GUILayout.Space(20);
}
isCollapsed = GUILayout.Toggle(isCollapsed, collapseLabel, GUILayout.ExpandWidth(false));
isOnlyLastLogVisible = GUILayout.Toggle(isOnlyLastLogVisible, onlyLastLogLabel, GUILayout.ExpandWidth(false));
}
GUILayout.EndHorizontal();
}
void DrawWindow(int windowID)
{
DrawLogList();
DrawStackTrace();
DrawToolbar();
// Allow the window to be dragged by its title bar.
GUI.DragWindow(titleBarRect);
}
void UpdateQueuedLogs()
{
while (queuedLogs.TryDequeue(out var log))
{
ProcessLogItem(log);
}
}
int? GetLastVisibleLogIndex()
{
for (var logIndex = logs.Count - 1; logIndex >= 0; logIndex--)
{
if (IsLogVisible(logIndex))
{
return logIndex;
}
}
return null;
}
void HandleLogThreaded(string message, string stackTrace, LogType type)
{
// Queue the log into a ConcurrentQueue to be processed later in the Unity main thread,
// so that we don't get GUI-related errors for logs coming from other threads
var log = new Log(message, stackTrace, type);
queuedLogs.Enqueue(log);
}
void ProcessLogItem(Log log)
{
var lastLog = logs.Count > 0 ? logs[logs.Count - 1] : (Log?)null;
var isDuplicateOfLastLog = lastLog.HasValue && log.Equals(lastLog.Value);
if (isDuplicateOfLastLog)
{
// Replace previous log with incremented count instead of adding a new one.
logs[logs.Count - 1] = lastLog.Value.IncrementedCount();
}
else
{
logs.Add(log);
TrimExcessLogs();
}
}
bool IsLogVisible(int logIndex)
{
var logType = logs[logIndex].Type;
return logTypeFilters[logType];
}
bool IsScrolledToBottom(Rect innerScrollRect, Rect outerScrollRect)
{
var innerScrollHeight = innerScrollRect.height;
// Take into account extra padding added to the scroll container.
var outerScrollHeight = outerScrollRect.height - GUI.skin.box.padding.vertical;
// If contents of scroll view haven't exceeded outer container, treat it as scrolled to bottom.
if (outerScrollHeight > innerScrollHeight)
{
return true;
}
// Scrolled to bottom (with error margin for float math)
return Mathf.Approximately(innerScrollHeight, logsScrollPosition.y + outerScrollHeight);
}
void ScrollToBottom()
{
logsScrollPosition = new Vector2(0, int.MaxValue);
}
void TrimExcessLogs()
{
if (!restrictLogCount)
{
return;
}
var amountToRemove = logs.Count - maxLogCount;
if (amountToRemove <= 0)
{
return;
}
logs.RemoveRange(0, amountToRemove);
}
bool WasMultiTouchThresholdExceeded()
{
#if ENABLE_INPUT_SYSTEM
var touchCount = UnityEngine.InputSystem.EnhancedTouch.Touch.activeTouches.Count;
#else
var touchCount = Input.touchCount;
#endif
return touchCount > 2;
}
bool WasShaken()
{
#if ENABLE_INPUT_SYSTEM
var acceleration = Accelerometer.current?.acceleration.ReadValue() ?? Vector3.zero;
#else
var acceleration = Input.acceleration;
#endif
return acceleration.sqrMagnitude > shakeAcceleration;
}
bool WasToggleKeyPressed()
{
#if ENABLE_INPUT_SYSTEM
return Keyboard.current[toggleKey].wasPressedThisFrame;
#else
return Input.GetKeyDown(toggleKey);
#endif
}
static void EnableMultiTouch()
{
#if ENABLE_INPUT_SYSTEM
UnityEngine.InputSystem.EnhancedTouch.EnhancedTouchSupport.Enable();
#else
Input.multiTouchEnabled = true;
#endif
}
}
/// <summary>
/// A basic container for log details.
/// </summary>
readonly struct Log
{
public readonly int Count;
public readonly string Message;
public readonly string StackTrace;
public readonly LogType Type;
public Log(string message, string stackTrace, LogType type)
{
Count = 1;
Message = TruncateForGUILabel(message);
StackTrace = TruncateForGUILabel(stackTrace);
Type = type;
}
Log(string message, string stackTrace, LogType type, int count)
{
Count = count;
Message = message;
StackTrace = stackTrace;
Type = type;
}
public bool Equals(Log log)
{
return Message == log.Message && StackTrace == log.StackTrace && Type == log.Type;
}
public Log IncrementedCount()
{
return new Log(Message, StackTrace, Type, Count + 1);
}
/// <summary>
/// Returns text shortened to fit in a GUILayout.Label.
/// </summary>
static string TruncateForGUILabel(string text)
{
// The max string length supported by UnityEngine.GUILayout.Label without triggering this error:
// "String too long for TextMeshGenerator. Cutting off characters."
const int maxLabelLength = 16382;
return string.IsNullOrEmpty(text) || text.Length <= maxLabelLength
? text
: text.Substring(0, maxLabelLength);
}
}
/// <summary>
/// Alternative to System.Collections.Concurrent.ConcurrentQueue
/// (It's only available in .NET 4.0 and greater)
/// </summary>
/// <remarks>
/// It's a bit slow (as it uses locks), and only provides a small subset of the interface
/// Overall, the implementation is intended to be simple & robust
/// </remarks>
class ConcurrentQueue<T>
{
readonly Queue<T> queue = new Queue<T>();
readonly object queueLock = new object();
public void Enqueue(T item)
{
lock (queueLock)
{
queue.Enqueue(item);
}
}
public bool TryDequeue(out T result)
{
lock (queueLock)
{
if (queue.Count == 0)
{
result = default(T);
return false;
}
result = queue.Dequeue();
return true;
}
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(Console))]
class ConsoleEditor : Editor
{
SerializedProperty toggleKey;
SerializedProperty openOnStart;
SerializedProperty shakeToOpen;
SerializedProperty shakeRequiresTouch;
SerializedProperty shakeAcceleration;
SerializedProperty toggleThresholdSeconds;
SerializedProperty restrictLogCount;
SerializedProperty maxLogCount;
SerializedProperty collapseLogOnStart;
SerializedProperty logFontSize;
SerializedProperty scaleFactor;
SerializedProperty skin;
void OnEnable()
{
toggleKey = serializedObject.FindProperty("toggleKey");
openOnStart = serializedObject.FindProperty("openOnStart");
shakeToOpen = serializedObject.FindProperty("shakeToOpen");
shakeRequiresTouch = serializedObject.FindProperty("shakeRequiresTouch");
shakeAcceleration = serializedObject.FindProperty("shakeAcceleration");
toggleThresholdSeconds = serializedObject.FindProperty("toggleThresholdSeconds");
restrictLogCount = serializedObject.FindProperty("restrictLogCount");
maxLogCount = serializedObject.FindProperty("maxLogCount");
collapseLogOnStart = serializedObject.FindProperty("collapseLogOnStart");
logFontSize = serializedObject.FindProperty("logFontSize");
scaleFactor = serializedObject.FindProperty("scaleFactor");
skin = serializedObject.FindProperty("skin");
}
public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(toggleKey);
EditorGUILayout.PropertyField(openOnStart);
EditorGUILayout.PropertyField(shakeToOpen);
using (new EditorGUI.DisabledScope(!shakeToOpen.boolValue))
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(shakeRequiresTouch);
EditorGUILayout.PropertyField(shakeAcceleration);
}
EditorGUILayout.PropertyField(toggleThresholdSeconds);
EditorGUILayout.PropertyField(restrictLogCount);
using (new EditorGUI.DisabledScope(!restrictLogCount.boolValue))
using (new EditorGUI.IndentLevelScope())
{
EditorGUILayout.PropertyField(maxLogCount);
}
EditorGUILayout.PropertyField(collapseLogOnStart);
EditorGUILayout.Space();
GUILayout.Label("Style", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(logFontSize);
EditorGUILayout.PropertyField(scaleFactor);
EditorGUILayout.PropertyField(skin);
serializedObject.ApplyModifiedProperties();
}
}
#endif
}