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
|
/* ---------------------------------------
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
* Collaborators: Lars Aalbertsen (@Rockylars)
* Project: Graphy - Ultimate Stats Monitor
* Date: 23-Dec-17
* Studio: Tayx
*
* This project is released under the MIT license.
* Attribution is not required, but it is always welcomed!
* -------------------------------------*/
using UnityEngine;
using UnityEngine.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using Tayx.Graphy.Audio;
using Tayx.Graphy.Fps;
using Tayx.Graphy.Ram;
using Tayx.Graphy.Utils;
namespace Tayx.Graphy
{
public class GraphyDebugger : G_Singleton<GraphyDebugger>
{
/* ----- TODO: ----------------------------
* Add summaries to the variables.
* Add summaries to the functions.
* Ask why we're not using System.Serializable instead for the helper class.
* Simplify the initializers of the DebugPackets, but check wether we should as some wont work with certain lists.
* --------------------------------------*/
protected GraphyDebugger () { }
#region Enums -> Public
public enum DebugVariable
{
Fps,
Fps_Min,
Fps_Max,
Fps_Avg,
Ram_Allocated,
Ram_Reserved,
Ram_Mono,
Audio_DB
}
public enum DebugComparer
{
Less_than,
Equals_or_less_than,
Equals,
Equals_or_greater_than,
Greater_than
}
public enum ConditionEvaluation
{
All_conditions_must_be_met,
Only_one_condition_has_to_be_met,
}
public enum MessageType
{
Log,
Warning,
Error
}
#endregion
#region Structs -> Public
[Serializable]
public struct DebugCondition
{
[Tooltip("Variable to compare against")]
public DebugVariable Variable;
[Tooltip("Comparer operator to use")]
public DebugComparer Comparer;
[Tooltip("Value to compare against the chosen variable")]
public float Value;
}
#endregion
#region Helper Classes
[Serializable]
public class DebugPacket
{
[Tooltip("If false, it won't be checked")]
public bool Active = true;
[Tooltip("Optional Id. It's used to get or remove DebugPackets in runtime")]
public int Id;
[Tooltip("If true, once the actions are executed, this DebugPacket will delete itself")]
public bool ExecuteOnce = true;
[Tooltip("Time to wait before checking if conditions are met (use this to avoid low fps drops triggering the conditions when loading the game)")]
public float InitSleepTime = 2;
[Tooltip("Time to wait before checking if conditions are met again (once they have already been met and if ExecuteOnce is false)")]
public float ExecuteSleepTime = 2;
public ConditionEvaluation ConditionEvaluation = ConditionEvaluation.All_conditions_must_be_met;
[Tooltip("List of conditions that will be checked each frame")]
public List<DebugCondition> DebugConditions = new List<DebugCondition>();
// Actions on conditions met
public MessageType MessageType;
[Multiline]
public string Message = string.Empty;
public bool TakeScreenshot = false;
public string ScreenshotFileName = "Graphy_Screenshot";
[Tooltip("If true, it pauses the editor")]
public bool DebugBreak = false;
public UnityEvent UnityEvents;
public List<System.Action> Callbacks = new List<System.Action>();
private bool canBeChecked = false;
private bool executed = false;
private float timePassed = 0;
public bool Check { get { return canBeChecked; } }
public void Update()
{
if (!canBeChecked)
{
timePassed += Time.deltaTime;
if ( (executed && timePassed >= ExecuteSleepTime)
|| (!executed && timePassed >= InitSleepTime))
{
canBeChecked = true;
timePassed = 0;
}
}
}
public void Executed()
{
canBeChecked = false;
executed = true;
}
}
#endregion
#region Variables -> Serialized Private
[SerializeField] private List<DebugPacket> m_debugPackets = new List<DebugPacket>();
#endregion
#region Variables -> Private
private G_FpsMonitor m_fpsMonitor = null;
private G_RamMonitor m_ramMonitor = null;
private G_AudioMonitor m_audioMonitor = null;
#endregion
#region Methods -> Unity Callbacks
private void Start()
{
m_fpsMonitor = GetComponentInChildren<G_FpsMonitor>();
m_ramMonitor = GetComponentInChildren<G_RamMonitor>();
m_audioMonitor = GetComponentInChildren<G_AudioMonitor>();
}
private void Update()
{
CheckDebugPackets();
}
#endregion
#region Public Methods
/// <summary>
/// Add a new DebugPacket.
/// </summary>
public void AddNewDebugPacket(DebugPacket newDebugPacket)
{
if (m_debugPackets != null)
{
m_debugPackets.Add(newDebugPacket);
}
}
/// <summary>
/// Add a new DebugPacket.
/// </summary>
public void AddNewDebugPacket
(
int newId,
DebugCondition newDebugCondition,
MessageType newMessageType,
string newMessage,
bool newDebugBreak,
System.Action newCallback
)
{
DebugPacket newDebugPacket = new DebugPacket();
newDebugPacket.Id = newId;
newDebugPacket.DebugConditions.Add(newDebugCondition);
newDebugPacket.MessageType = newMessageType;
newDebugPacket.Message = newMessage;
newDebugPacket.DebugBreak = newDebugBreak;
newDebugPacket.Callbacks.Add(newCallback);
AddNewDebugPacket(newDebugPacket);
}
/// <summary>
/// Add a new DebugPacket.
/// </summary>
public void AddNewDebugPacket
(
int newId,
List<DebugCondition> newDebugConditions,
MessageType newMessageType,
string newMessage,
bool newDebugBreak,
System.Action newCallback
)
{
DebugPacket newDebugPacket = new DebugPacket();
newDebugPacket.Id = newId;
newDebugPacket.DebugConditions = newDebugConditions;
newDebugPacket.MessageType = newMessageType;
newDebugPacket.Message = newMessage;
newDebugPacket.DebugBreak = newDebugBreak;
newDebugPacket.Callbacks.Add(newCallback);
AddNewDebugPacket(newDebugPacket);
}
/// <summary>
/// Add a new DebugPacket.
/// </summary>
public void AddNewDebugPacket
(
int newId,
DebugCondition newDebugCondition,
MessageType newMessageType,
string newMessage,
bool newDebugBreak,
List<System.Action> newCallbacks
)
{
DebugPacket newDebugPacket = new DebugPacket();
newDebugPacket.Id = newId;
newDebugPacket.DebugConditions.Add(newDebugCondition);
newDebugPacket.MessageType = newMessageType;
newDebugPacket.Message = newMessage;
newDebugPacket.DebugBreak = newDebugBreak;
newDebugPacket.Callbacks = newCallbacks;
AddNewDebugPacket(newDebugPacket);
}
/// <summary>
/// Add a new DebugPacket.
/// </summary>
public void AddNewDebugPacket
(
int newId,
List<DebugCondition> newDebugConditions,
MessageType newMessageType,
string newMessage,
bool newDebugBreak,
List<System.Action> newCallbacks
)
{
DebugPacket newDebugPacket = new DebugPacket();
newDebugPacket.Id = newId;
newDebugPacket.DebugConditions = newDebugConditions;
newDebugPacket.MessageType = newMessageType;
newDebugPacket.Message = newMessage;
newDebugPacket.DebugBreak = newDebugBreak;
newDebugPacket.Callbacks = newCallbacks;
AddNewDebugPacket(newDebugPacket);
}
/// <summary>
/// Returns the first Packet with the specified ID in the DebugPacket list.
/// </summary>
/// <param name="packetId"></param>
/// <returns></returns>
public DebugPacket GetFirstDebugPacketWithId(int packetId)
{
return m_debugPackets.First(x => x.Id == packetId);
}
/// <summary>
/// Returns a list with all the Packets with the specified ID in the DebugPacket list.
/// </summary>
/// <param name="packetId"></param>
/// <returns></returns>
public List<DebugPacket> GetAllDebugPacketsWithId(int packetId)
{
return m_debugPackets.FindAll(x => x.Id == packetId);
}
/// <summary>
/// Removes the first Packet with the specified ID in the DebugPacket list.
/// </summary>
/// <param name="packetId"></param>
/// <returns></returns>
public void RemoveFirstDebugPacketWithId(int packetId)
{
if (m_debugPackets != null && GetFirstDebugPacketWithId(packetId) != null)
{
m_debugPackets.Remove(GetFirstDebugPacketWithId(packetId));
}
}
/// <summary>
/// Removes all the Packets with the specified ID in the DebugPacket list.
/// </summary>
/// <param name="packetId"></param>
/// <returns></returns>
public void RemoveAllDebugPacketsWithId(int packetId)
{
if (m_debugPackets != null)
{
m_debugPackets.RemoveAll(x => x.Id == packetId);
}
}
/// <summary>
/// Add an Action callback to the first Packet with the specified ID in the DebugPacket list.
/// </summary>
/// <param name="callback"></param>
/// <param name="id"></param>
public void AddCallbackToFirstDebugPacketWithId(System.Action callback, int id)
{
if (GetFirstDebugPacketWithId(id) != null)
{
GetFirstDebugPacketWithId(id).Callbacks.Add(callback);
}
}
/// <summary>
/// Add an Action callback to all the Packets with the specified ID in the DebugPacket list.
/// </summary>
/// <param name="callback"></param>
/// <param name="id"></param>
public void AddCallbackToAllDebugPacketWithId(System.Action callback, int id)
{
if (GetAllDebugPacketsWithId(id) != null)
{
foreach (var debugPacket in GetAllDebugPacketsWithId(id))
{
if (callback != null)
{
debugPacket.Callbacks.Add(callback);
}
}
}
}
#endregion
#region Methods -> Private
/// <summary>
/// Checks all the Debug Packets to see if they have to be executed.
/// </summary>
private void CheckDebugPackets()
{
if (m_debugPackets == null)
{
return;
}
for (var i = 0; i < m_debugPackets.Count; i++)
{
DebugPacket packet = m_debugPackets[i];
if (packet != null && packet.Active)
{
packet.Update();
if (packet.Check)
{
switch (packet.ConditionEvaluation)
{
case ConditionEvaluation.All_conditions_must_be_met:
int count = 0;
foreach (var packetDebugCondition in packet.DebugConditions)
{
if (CheckIfConditionIsMet(packetDebugCondition))
{
count++;
}
}
if (count >= packet.DebugConditions.Count)
{
ExecuteOperationsInDebugPacket(packet);
if (packet.ExecuteOnce)
{
m_debugPackets[i] = null;
}
}
break;
case ConditionEvaluation.Only_one_condition_has_to_be_met:
foreach (var packetDebugCondition in packet.DebugConditions)
{
if (CheckIfConditionIsMet(packetDebugCondition))
{
ExecuteOperationsInDebugPacket(packet);
if (packet.ExecuteOnce)
{
m_debugPackets[i] = null;
}
break;
}
}
break;
}
}
}
}
m_debugPackets.RemoveAll((packet) => packet == null);
}
/// <summary>
/// Returns true if a condition is met.
/// </summary>
/// <param name="debugCondition"></param>
/// <returns></returns>
private bool CheckIfConditionIsMet(DebugCondition debugCondition)
{
switch (debugCondition.Comparer)
{
case DebugComparer.Less_than:
return GetRequestedValueFromDebugVariable(debugCondition.Variable) < debugCondition.Value;
case DebugComparer.Equals_or_less_than:
return GetRequestedValueFromDebugVariable(debugCondition.Variable) <= debugCondition.Value;
case DebugComparer.Equals:
return Mathf.Approximately(GetRequestedValueFromDebugVariable(debugCondition.Variable), debugCondition.Value);
case DebugComparer.Equals_or_greater_than:
return GetRequestedValueFromDebugVariable(debugCondition.Variable) >= debugCondition.Value;
case DebugComparer.Greater_than:
return GetRequestedValueFromDebugVariable(debugCondition.Variable) > debugCondition.Value;
default:
return false;
}
}
/// <summary>
/// Obtains the requested value from the specified variable.
/// </summary>
/// <param name="debugVariable"></param>
/// <returns></returns>
private float GetRequestedValueFromDebugVariable(DebugVariable debugVariable)
{
switch (debugVariable)
{
case DebugVariable.Fps:
return m_fpsMonitor != null ? m_fpsMonitor.CurrentFPS : 0;
case DebugVariable.Fps_Min:
return m_fpsMonitor != null ? m_fpsMonitor.MinFPS : 0;
case DebugVariable.Fps_Max:
return m_fpsMonitor != null ? m_fpsMonitor.MaxFPS : 0;
case DebugVariable.Fps_Avg:
return m_fpsMonitor != null ? m_fpsMonitor.AverageFPS : 0;
case DebugVariable.Ram_Allocated:
return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
case DebugVariable.Ram_Reserved:
return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
case DebugVariable.Ram_Mono:
return m_ramMonitor != null ? m_ramMonitor.AllocatedRam : 0;
case DebugVariable.Audio_DB:
return m_audioMonitor != null ? m_audioMonitor.MaxDB : 0;
default:
return 0;
}
}
/// <summary>
/// Executes the operations in the DebugPacket specified.
/// </summary>
/// <param name="debugPacket"></param>
private void ExecuteOperationsInDebugPacket(DebugPacket debugPacket)
{
if (debugPacket != null)
{
if (debugPacket.DebugBreak)
{
Debug.Break();
}
if (debugPacket.Message != "")
{
string message = "[Graphy] (" + System.DateTime.Now + "): " + debugPacket.Message;
switch (debugPacket.MessageType)
{
case MessageType.Log:
Debug.Log(message);
break;
case MessageType.Warning:
Debug.LogWarning(message);
break;
case MessageType.Error:
Debug.LogError(message);
break;
}
}
if (debugPacket.TakeScreenshot)
{
string path = debugPacket.ScreenshotFileName + "_" + System.DateTime.Now + ".png";
path = path.Replace("/", "-").Replace(" ", "_").Replace(":", "-");
#if UNITY_2017_1_OR_NEWER
ScreenCapture.CaptureScreenshot(path);
#else
Application.CaptureScreenshot(path);
#endif
}
debugPacket.UnityEvents.Invoke();
foreach (var callback in debugPacket.Callbacks)
{
if (callback != null) callback();
}
debugPacket.Executed();
}
}
#endregion
}
}
|