-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugManager.cs
More file actions
712 lines (592 loc) · 23 KB
/
DebugManager.cs
File metadata and controls
712 lines (592 loc) · 23 KB
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using TMPro;
using Unity.VisualScripting;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
using Unity.VisualScripting.FullSerializer;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class DebugManager : MonoBehaviour
{
[Header("Fill with your existing canvas object")]
[HideInInspector] public string canvasName;
[System.Serializable]
public class PanelConfiguration
{
#region Variables
//Game Objects
[HideInInspector] public GameObject myGO;
[HideInInspector] public GameObject debugFolder;
[HideInInspector] public GameObject myText;
//Tabs
[HideInInspector] public int currentTab;
#region Graph
public bool graphCustomInterval;//custom interval toggle variabel
public float graphInterval = 1;//interval in which the graph will update
public float counter = 0;//interval counter
public List<float> xValues = new List<float>();//time values
public List<float> yValues = new List<float>();//script values
public float graphScale = 100;
#endregion
#region Gizmo Lines
//========== Lines ==========
//Gizmo type
[HideInInspector] public enum GizmoType { line, cube, sphere };
[HideInInspector] public GizmoType gizmoType;
//Bools
[HideInInspector] public bool isScriptDistance, isLine, isCube, isSphere;
[HideInInspector] public bool isWire = true;
//Collider
[HideInInspector] public bool isCollision;
//Color
[HideInInspector] public Color gizmoPrimaryColor = Color.green, gizmoSecondaryColor = Color.red;
[HideInInspector] public Color gizmoCurrentColor = Color.white;
//Int
[HideInInspector] public int lineAmount = 30;
//Float
[HideInInspector] public float gizmoScale = 5;
[HideInInspector] public float lineDuration = 1;
//Transform
[HideInInspector] public Transform origin;
[HideInInspector] public Transform direction;
//Vector3
[HideInInspector] public Vector3 offSet;
#endregion
//Panel
[HideInInspector] public GameObject panel;
[HideInInspector] public Image myImage;
[HideInInspector] public TextMeshProUGUI text;
//Rect Transform
[HideInInspector] public RectTransform rectTransform;
public Vector2 panelDimension = new Vector2(200, 100);
//=============================
[Header("Object")]
public string objectName;
[Header("Text")]
public Color textColor = Color.black;
public float fontSize = 20;
[Header("Variables")]
//Scripts
[HideInInspector] public MonoBehaviour script;
[HideInInspector] public int variableIndex = 0;
//Variables types
[HideInInspector] public enum Variable { boolean, floats, integer, strings, transform };
[HideInInspector] public Variable variable;
public object scriptVariable;
//save the type of the chosen variable
[HideInInspector] public bool boolean, floats, integer, strings, transform;
//Float
[HideInInspector] public bool interval, plotGraph;//check an interval || set a graph
[HideInInspector] public float minNumber = 0, maxNumber = 1;
//Transform
[SerializeField] public bool position, rotation, scale;
//Colors
[HideInInspector] public Color primaryColor = Color.green;
[HideInInspector] public Color secondaryColor = Color.red;
#endregion
}
[HideInInspector] public List<PanelConfiguration> panelConfigurations = new List<PanelConfiguration>();
#region Setup
//Canvas setup
public void CanvasSetup(int configurationIndex)
{
if (configurationIndex < 0 || configurationIndex >= panelConfigurations.Count)
{
//Invalid configuration index
return;
}
PanelConfiguration config = panelConfigurations[configurationIndex];
//========== Canvas ==========
// Check if there's already a Canvas
GameObject existingCanvas = GameObject.Find(canvasName);
// Check if there's already a debug folder
GameObject existingFolder = GameObject.Find("Debug");
// if there's no canvas. Create one.
if (existingCanvas == null)
{
// Creating game object
config.myGO = new GameObject();
config.myGO.name = "Canvas";
config.myGO.AddComponent<Canvas>();
// Creating canvas
Canvas myCanvas = config.myGO.GetComponent<Canvas>();
myCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
config.myGO.AddComponent<CanvasScaler>();
config.myGO.AddComponent<GraphicRaycaster>();
}
else
{
// Assigning the existing Canvas to the MyGO game object
config.myGO = existingCanvas.gameObject;
}
//if there's no debug folder. Create one.
if (existingFolder == null)
{
// creates the gameObject
config.debugFolder = new GameObject();
config.debugFolder.name = "Debug";
}
// if there's already a folder
else
{
// assign the existing folder
config.debugFolder = existingFolder.gameObject;
}
// make the debug folder gameObject son of the canvas gameObject
config.debugFolder.transform.parent = config.myGO.transform;
config.debugFolder.transform.localPosition = Vector3.zero;
config.debugFolder.transform.localScale = Vector3.one;
//========== Panel ==========
// panel setup
config.panel = new GameObject(); //creating object
config.panel.name = config.objectName + " Panel"; //naming
config.panel.AddComponent<Image>(); //adding the Image component
config.panel.transform.parent = config.debugFolder.transform; //setting as the son of the debug folder
// transform
config.panel.transform.localPosition = Vector3.zero;
config.panel.transform.localScale = Vector3.one;
config.rectTransform = config.panel.GetComponent<RectTransform>();
config.rectTransform.sizeDelta = config.panelDimension;
// image settings
config.myImage = config.panel.GetComponent<Image>();
if (config.plotGraph)
config.myImage.color = config.secondaryColor;
else
config.myImage.color = config.primaryColor;
//========== Text ==========
config.myText = new GameObject();
config.myText.transform.parent = config.panel.transform;
if (config.plotGraph)
{
config.myText.name = config.objectName + " Graph";
}
else
{
config.myText.name = config.objectName + " Text";
}
config.myText.AddComponent<TextMeshProUGUI>();
config.text = config.myText.GetComponent<TextMeshProUGUI>();
config.text.text = config.objectName;
config.text.fontSize = config.fontSize;
config.text.color = config.textColor;
config.text.transform.localScale = Vector3.one;
config.text.alignment = TextAlignmentOptions.Center;
config.text.alignment = TextAlignmentOptions.Midline;
//Text position
config.rectTransform = config.text.GetComponent<RectTransform>();
config.rectTransform.localPosition = new Vector3(0, 0, 0);
config.rectTransform.sizeDelta = config.panelDimension;
}
#endregion
#region Functions
//Update each frame
private void Update()
{
UpdatePanel();
for (int configurationIndex = 0; configurationIndex < panelConfigurations.Count; configurationIndex++)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
if (config != null)
{
UpdateVariables(config.script, config.variable, out config.scriptVariable, configurationIndex);
//if is a line and the panel is set to gizmo
if (config.isLine && config.currentTab == 1)
{
DrawLines(configurationIndex);
}
}
}
}
//Update Variables Method
private void UpdateVariables(MonoBehaviour script, PanelConfiguration.Variable variableType, out object selectedVariableValue, int i)
{
selectedVariableValue = null;
// if the script is null
if (script == null)
{
//exit
return;
}
//Get the variable properties
System.Type scriptType = script.GetType();
var fields = scriptType.GetFields();
var properties = scriptType.GetProperties();
// Create a list to store the variables
List<string> variableNames = new List<string>();
//Adding the variables at the list
foreach (var field in fields)
{
if (DebugEditor.IsVariableOfType(field, variableType))
{
variableNames.Add(field.Name);
}
}
// if there's at least one variable, create a popup field
if (variableNames.Count > 0)
{
int selectedVariableIndex = panelConfigurations[i].variableIndex;
// Check if the variable is in a valid interval-
selectedVariableIndex = Mathf.Clamp(selectedVariableIndex, 0, variableNames.Count - 1);
// Obtain the name of the selected variable
string selectedVariableName = variableNames[selectedVariableIndex];
// Show the value of the selected variable
foreach (var field in fields)
{
if (field.Name == selectedVariableName)
{
//save the variable selected
selectedVariableValue = field.GetValue(script);
//saving index in Debug manager script
panelConfigurations[i].variableIndex = selectedVariableIndex;
break;
}
}
}
}
//Gizmo update
private void OnDrawGizmos()
{
for (int i = 0; i < panelConfigurations.Count; i++)
{
if (panelConfigurations[i].currentTab == 1)
{
if (panelConfigurations[i].isCube)
{
DrawCube(i);
}
if (panelConfigurations[i].isSphere)
{
DrawSphere(i);
}
}
}
}
//Draw line Method
private void DrawLines(int configurationIndex)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
//Vectors
Vector3 startPoint, endPoint;
//check if the draw line option is true
if (config.currentTab == 1)
{
//check if the origin isn't null
if (config.origin != null)
{
//Saving the end point location
startPoint = config.origin.position + config.offSet;
//if the direction was set
if (config.direction != null)
{
endPoint = startPoint - config.direction.forward * config.gizmoScale;
}
else
{
//direction is the start point forward
endPoint = startPoint + config.origin.forward * config.gizmoScale;
}
config.lineDuration = (config.lineAmount - 1) * Time.deltaTime;
//Draw the lines
Debug.DrawLine(startPoint, endPoint, config.gizmoCurrentColor, config.lineDuration);
}
}
}
//Draw cube method
private void DrawCube(int configurationIndex)
{
//Panel class reference
PanelConfiguration config = panelConfigurations[configurationIndex];
//Gizmo setup
Gizmos.color = config.gizmoCurrentColor;
//initial position;
Vector3 startPosition;
//Check if there's a transform variable
if (config.origin != null)
{
startPosition = config.origin.position + config.offSet;
}
else
{
startPosition = config.offSet;
}
if (config.isWire)
{
Gizmos.DrawWireCube(startPosition, 0.05f * config.gizmoScale * Vector3.one);
}
else
{
Gizmos.DrawCube(startPosition, 0.05f * config.gizmoScale * Vector3.one);
}
}
//Draw Sphere method
private void DrawSphere(int configurationIndex)
{
//Panel class reference
PanelConfiguration config = panelConfigurations[configurationIndex];
//Gizmo setup
//Color
Gizmos.color = config.gizmoCurrentColor;
//initial position;
Vector3 startPosition;
//Check if there's a transform variable
if (config.origin != null)
{
startPosition = config.origin.position + config.offSet;
}
else
{
startPosition = config.offSet;
}
if (config.isWire)
{
Gizmos.DrawWireSphere(startPosition, config.gizmoScale * 0.05f);
}
else
{
Gizmos.DrawSphere(startPosition, config.gizmoScale * 0.05f);
}
}
//GameObject setup
public void CreateGameObject(Vector3 startPosition, int i)
{
//Panel class reference
PanelConfiguration config = panelConfigurations[i];
GameObject newGameObject;
newGameObject = new GameObject();
//name
newGameObject.name = config.objectName + " Collider";
//position
newGameObject.transform.position = startPosition;
//scale
newGameObject.transform.localScale = Vector3.one * (config.gizmoScale * 0.05f);
//collider
if (config.isCube)
{
BoxCollider collider = newGameObject.AddComponent<BoxCollider>();
collider.isTrigger = true;
collider.transform.localScale = Vector3.one * (config.gizmoScale * 0.05f);
}
else if (config.isSphere)
{
SphereCollider collider = newGameObject.AddComponent<SphereCollider>();
collider.isTrigger = true;
collider.transform.localScale = Vector3.one * (config.gizmoScale * 0.1f);
}
//collision script
newGameObject.AddComponent<CollisionHandler>();
}
#region Panel Update Methods
//========== Main Method ==========
private void UpdatePanel()
{
for (int configurationIndex = 0; configurationIndex < panelConfigurations.Count; configurationIndex++)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
//check if the variable and the image panel isn't null
if (config.scriptVariable != null && config.myImage != null && config.currentTab == 0)
{
//print("Value: " + config.scriptVariable);
UpdateBool(configurationIndex);//bool update
UpdateInt(configurationIndex);//int update
UpdateFloat(configurationIndex);//float update
UpdateString(configurationIndex);//string update
UpdateTransform(configurationIndex);//transform update
}
}
}
//========== Bool Treatment ==========
private void UpdateBool(int configurationIndex)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
if (config.scriptVariable.GetType() == typeof(bool))
{
if ((bool)config.scriptVariable)
{
config.myImage.color = config.primaryColor;
}
else
{
config.myImage.color = config.secondaryColor;
}
}
}
//========== Int Treatment ==========
private void UpdateInt(int configurationIndex)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
if (config.scriptVariable.GetType() == typeof(int))
{
//save the variable value
float value = (int)config.scriptVariable;
if (config.text != null)
{
config.text.text = config.objectName + ": " + config.scriptVariable.ToString();
//if the interval is true
if (config.interval)
{
//if the value is between the minimum and maximum values
if (config.minNumber <= value && value <= config.maxNumber)
{
//set the panel color to the primary color
config.myImage.color = config.primaryColor;
}
else
{
//set the panel color to the secondary color
config.myImage.color = config.secondaryColor;
}
}
}
else
{
print("Text is null");
}
}
}
//========== Float Treatment ==========
private void UpdateFloat(int configurationIndex)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
//If the type of the variable is float
if (config.scriptVariable.GetType() == typeof(float))
{
//save the variable value
float value = (float)config.scriptVariable;
//check is the text isn't null
if (config.text != null)
{
//change the panel text to the variable value text
config.text.text = config.objectName + ": " + ((float)config.scriptVariable).ToString("F2");
//if the plot graph option is true
if (config.plotGraph)
{
//Update Plot Graph method
UpdatePlotGraph(config.xValues, config.yValues, value, configurationIndex);
}
//if the interval is true
if (config.interval)
{
//if the value is between the minimum and maximum values
if (config.minNumber <= value && value <= config.maxNumber)
{
//set the panel color to the primary color
config.myImage.color = config.primaryColor;
}
else
{
//set the panel color to the secondary color
config.myImage.color = config.secondaryColor;
}
}
}
else
{
//print("Text is null");
}
}
}
//========== String Treatment ==========
private void UpdateString(int configurationIndex)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
//If the type of the variable is float
if (config.scriptVariable.GetType() == typeof(string))
{
//check if the text isn't null
if (config.text != null)
{
config.text.text = config.objectName + ": " + config.scriptVariable.ToString();
}
}
}
//========== Transform Treatment ==========
private void UpdateTransform(int configurationIndex)
{
PanelConfiguration config = panelConfigurations[configurationIndex];
//If the type of the variable is float
if (config.scriptVariable.GetType() == typeof(Transform))
{
//check if the text isn't null
if (config.text != null)
{
//Position
if (config.position)
{
//change the panel text to the variable value text
config.text.text = config.objectName + ": " + ((Transform)config.scriptVariable).position.ToString();
}
//Rotation
if (config.rotation)
{
//change the panel text to the variable value text
config.text.text = config.objectName + ": " + ((Transform)config.scriptVariable).rotation.ToString();
}
//Scale
if (config.scale)
{
//change the panel text to the variable value text
config.text.text = config.objectName + ": " + ((Transform)config.scriptVariable).localScale.ToString();
}
}
else
{
print("Text is null");
}
}
}
//========== Graph Treatment ==========
private void UpdatePlotGraph(List<float> xValues, List<float> yValues, float scriptValue, int configurationIndex)
{
//Optimization
PanelConfiguration config = panelConfigurations[configurationIndex];
//Debug
for (int i = 0; i < xValues.Count; i++)
{
//print(xValues[i]);
}
//========== Updating Y values ==========
if (config.graphCustomInterval)//if the user set a custom interval
{
//========== Updating X values ==========
float insert = Mathf.Round(Time.time);
//waiting for this interval
if (WaitTime(config, config.graphInterval))
{
xValues.Add(insert);
//adding the script value
yValues.Add(scriptValue);
}
}
else//update at a frame rate
{
config.graphInterval = Time.deltaTime;
//========== Updating X values ==========
float insert = Time.time;
//waiting for this interval
if (WaitTime(config, config.graphInterval))
{
xValues.Add(insert);
//adding the script value
yValues.Add(scriptValue);
}
}
}
private bool WaitTime(PanelConfiguration config, float timeToWait)
{
config.counter += Time.deltaTime;
if (config.counter >= timeToWait)
{
config.counter = 0;
return true;
}
else
{
return false;
}
}
#endregion
#endregion
}