-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugEditor.cs
More file actions
1072 lines (874 loc) · 38.3 KB
/
DebugEditor.cs
File metadata and controls
1072 lines (874 loc) · 38.3 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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Reflection;
using System.Linq;
using static DebugManager;
using UnityEditor.Experimental.GraphView;
using Unity.VisualScripting.FullSerializer;
#if UNITY_EDITOR
[CustomEditor(typeof(DebugManager))]
public class DebugEditor : Editor
{
//Debug manager reference
public DebugManager debugManager;
//logo
private Texture2D logoTexture;
//Script Properties Variables
private SerializedProperty scriptProperty;
private List<string> scriptVariable = new List<string>();
private List<int> selectedVariableIndex;
//Show each panel
private List<bool> isShowing;
//Update real time the panels
private void OnEnable()
{
debugManager = (DebugManager)target;
//Get the properties from the user's script
scriptProperty = serializedObject.FindProperty("script");
EditorApplication.update += OnUpdate;
//Logo
logoTexture = AssetDatabase.LoadAssetAtPath<Texture2D>("Assets/DebugManager/Icons/Icon.png");
//Create a list to show each panel configuration
isShowing = new List<bool>();
//Create the variable index list
selectedVariableIndex = new List<int>(debugManager.panelConfigurations.Count);
// VariableIndex list
for (int i = 0; i < debugManager.panelConfigurations.Count; i++)
{
//add a visible panel
isShowing.Add(true);
//add the selected index
selectedVariableIndex.Add(debugManager.panelConfigurations[i].variableIndex);
}
}
private void OnDisable()
{
EditorApplication.update -= OnUpdate;
}
private void OnUpdate()
{
Repaint();
EditorApplication.QueuePlayerLoopUpdate();
}
//Main
public override void OnInspectorGUI()
{
//Update the Instance object
serializedObject.Update();
//Basic Inspector
base.OnInspectorGUI();
debugManager = (DebugManager)target;
//========== Logo ==========
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.Label(logoTexture, GUILayout.MaxHeight(128), GUILayout.ExpandWidth(true));
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
//Canvas Object Name
EditorGUILayout.LabelField("UI folder name: ", EditorStyles.boldLabel);
debugManager.canvasName = EditorGUILayout.TextField("Object name: ", debugManager.canvasName);
//Gap
EditorGUILayout.Space(10);
if (Application.isPlaying)
{
for (int i = 0; i < isShowing.Count; i++)
{
isShowing[i] = true;
}
}
//========== Inspector Setup ==========
for (int i = 0; i < debugManager.panelConfigurations.Count; i++)
{
if (i >= 0 && i <= debugManager.panelConfigurations.Count)
{
//Start the arrow drop
isShowing[i] = EditorGUILayout.BeginFoldoutHeaderGroup(isShowing[i], $"Debug {i}");
if (isShowing[i])
{
//Start box
EditorGUILayout.BeginVertical("box");
//ToolBar
string[] tabText = { "Panel", "Gizmo" };
debugManager.panelConfigurations[i].currentTab = GUILayout.Toolbar(debugManager.panelConfigurations[i].currentTab, tabText);
//If panel is selected
if (debugManager.panelConfigurations[i].currentTab == 0)
{
Panel(debugManager, i);
}
//if gizmo is selected
if (debugManager.panelConfigurations[i].currentTab == 1)
{
Lines(debugManager, i);
}
//Gap
EditorGUILayout.Space(10);
//End box
EditorGUILayout.EndVertical();
}
}
//End the arrow drop
EditorGUILayout.EndFoldoutHeaderGroup();
}
//========== Button Field ==========
//------ Add Panels ------
AddButton(debugManager);
//------ Remove the last Panel ------
RemoveLastButton(debugManager);
//------ Clear all panels ------
ClearButton(debugManager);
//Create Panels
EditorGUILayout.Space(10);
//Check if there is a least one panel
bool showCreateButton = debugManager.panelConfigurations.Any(config => config.currentTab != 1);
//if true
if (showCreateButton)
{
CreateButton(debugManager);
}
//Update the instance
serializedObject.ApplyModifiedProperties();
EditorUtility.SetDirty(target);
}
//========== Methods ==========
//Panel setup
private void Panel(DebugManager debugManager, int i)
{
//Gap
EditorGUILayout.Space(10);
//Start horizontal gap
EditorGUI.indentLevel++;
//Object Name
EditorGUILayout.LabelField("Game Object", EditorStyles.boldLabel);
debugManager.panelConfigurations[i].objectName = EditorGUILayout.TextField("Panel name: ", debugManager.panelConfigurations[i].objectName);
debugManager.panelConfigurations[i].panelDimension = EditorGUILayout.Vector2Field("Panel Dimensions: ", debugManager.panelConfigurations[i].panelDimension);
EditorGUILayout.Space(5);
//Text Color
EditorGUILayout.LabelField("Text", EditorStyles.boldLabel);
debugManager.panelConfigurations[i].textColor = EditorGUILayout.ColorField("Text Color: ", debugManager.panelConfigurations[i].textColor);
debugManager.panelConfigurations[i].fontSize = EditorGUILayout.FloatField("Font Size: ", debugManager.panelConfigurations[i].fontSize);
EditorGUILayout.Space(5);
//Script assign
EditorGUILayout.LabelField("Script", EditorStyles.boldLabel);
debugManager.panelConfigurations[i].script = (MonoBehaviour)EditorGUILayout.ObjectField("Script", debugManager.panelConfigurations[i].script, typeof(MonoBehaviour), true);
//Type of variable
if (debugManager.panelConfigurations[i].script != null)
{
//Select the type of variable field
debugManager.panelConfigurations[i].variable = (DebugManager.PanelConfiguration.Variable)EditorGUILayout.EnumPopup("Variable Type", debugManager.panelConfigurations[i].variable);
//Method that saves the type of the variable
SaveTypeVariable(debugManager, i, true, true);
//Variable
DisplayScriptVariables(debugManager.panelConfigurations[i].script, debugManager.panelConfigurations[i].variable, out debugManager.panelConfigurations[i].scriptVariable, i);
//Color
EditorGUILayout.Space(5);
if (debugManager.panelConfigurations[i].plotGraph == false)
{
EditorGUILayout.LabelField("Color", EditorStyles.boldLabel);
debugManager.panelConfigurations[i].primaryColor = EditorGUILayout.ColorField("Primary Color(true): ", debugManager.panelConfigurations[i].primaryColor);
debugManager.panelConfigurations[i].secondaryColor = EditorGUILayout.ColorField("Secondary Color(false): ", debugManager.panelConfigurations[i].secondaryColor);
}
}
//End horizontal gap
EditorGUI.indentLevel--;
//for the last panel
if (i != debugManager.panelConfigurations.Count - 1)
{
//Remove this panel
RemoveButton(debugManager, i);
}
}
//Line Gizmo setup
private void Lines(DebugManager debugManager, int i)
{
//Gap
EditorGUILayout.Space(10);
//Start horizontal gap
EditorGUI.indentLevel++;
//========== Gizmo Type ==========
//Select the type of gizmo title
debugManager.panelConfigurations[i].gizmoType = (DebugManager.PanelConfiguration.GizmoType)EditorGUILayout.EnumPopup("Gizmo Type", debugManager.panelConfigurations[i].gizmoType);
//save the selected type
SaveTypeGizmo(debugManager, i);
//---------- Parameters ----------
//If is a cube or a sphere
if (!debugManager.panelConfigurations[i].isLine)
{
EditorGUILayout.Space(5);
EditorGUILayout.LabelField("Wire", EditorStyles.boldLabel);
//create a check box to set if the object is a wire
debugManager.panelConfigurations[i].isWire = EditorGUILayout.Toggle("Set the object to be wired", debugManager.panelConfigurations[i].isWire);
}
//Gap
EditorGUILayout.Space(5);
//Title
EditorGUILayout.LabelField("Length", EditorStyles.boldLabel);
//Distance(length) option
debugManager.panelConfigurations[i].isScriptDistance = EditorGUILayout.Toggle("Use variable", debugManager.panelConfigurations[i].isScriptDistance);
//Check if the user want's to use a variable instead of a raw number
if (debugManager.panelConfigurations[i].isScriptDistance)
{
//gap
EditorGUILayout.Space(5);
//Show script field
EditorGUILayout.LabelField("Script", EditorStyles.boldLabel);
debugManager.panelConfigurations[i].script = (MonoBehaviour)EditorGUILayout.ObjectField("Script", debugManager.panelConfigurations[i].script, typeof(MonoBehaviour), true);
//check if the script field isn't null
if (debugManager.panelConfigurations[i].script != null)
{
//Select the type of variable field
debugManager.panelConfigurations[i].variable = DebugManager.PanelConfiguration.Variable.floats;
debugManager.panelConfigurations[i].floats = true;
//Method that saves the type of the variable
SaveTypeVariable(debugManager, i, false, false);
//Method that shows the variables avaliables
DisplayScriptVariables(debugManager.panelConfigurations[i].script, debugManager.panelConfigurations[i].variable, out debugManager.panelConfigurations[i].scriptVariable, i);
debugManager.panelConfigurations[i].gizmoScale = (float)debugManager.panelConfigurations[i].scriptVariable;
}
}
else
{
//if is a line
if (debugManager.panelConfigurations[i].isLine)
{
//create a float field
debugManager.panelConfigurations[i].gizmoScale = EditorGUILayout.FloatField("Scale: ", debugManager.panelConfigurations[i].gizmoScale);
}
else
{
//create a slider
debugManager.panelConfigurations[i].gizmoScale = EditorGUILayout.Slider("Scale: ", debugManager.panelConfigurations[i].gizmoScale, 0.1f, 100);
}
}
//if is a line
if (debugManager.panelConfigurations[i].isLine)
{
//Show the amount of lines field
debugManager.panelConfigurations[i].lineAmount = EditorGUILayout.IntSlider("Amount: ", debugManager.panelConfigurations[i].lineAmount, 1, 100);
}
//space gap
EditorGUILayout.Space(5);
//---------- Transform ----------
EditorGUILayout.LabelField("Transform", EditorStyles.boldLabel);
//Start point of the line
debugManager.panelConfigurations[i].origin = (Transform)EditorGUILayout.ObjectField("Initial Location: ", debugManager.panelConfigurations[i].origin, typeof(Transform), true);
//If is a line
if (debugManager.panelConfigurations[i].isLine)
{
//Direction to which the line will be point
debugManager.panelConfigurations[i].direction = (Transform)EditorGUILayout.ObjectField("Direction: ", debugManager.panelConfigurations[i].direction, typeof(Transform), true);
}
//Offset to adjust the line
debugManager.panelConfigurations[i].offSet = EditorGUILayout.Vector3Field("Offset: ", debugManager.panelConfigurations[i].offSet);
EditorGUILayout.Space(5);
//---------- Style ----------
EditorGUILayout.LabelField("Style", EditorStyles.boldLabel);
//Line color
debugManager.panelConfigurations[i].gizmoCurrentColor = EditorGUILayout.ColorField("Color: ", debugManager.panelConfigurations[i].gizmoCurrentColor);
//========== Collision ==========
CollisionGizmo(debugManager, i);
//end horizontal gap
EditorGUI.indentLevel--;
//for the last panel
if (i != debugManager.panelConfigurations.Count - 1)
{
//Remove this panel
RemoveButton(debugManager, i);
}
}
//Collision method
private void CollisionGizmo(DebugManager debugManager, int i)
{
EditorGUILayout.Space(5);
EditorGUILayout.LabelField("Collision", EditorStyles.boldLabel);
debugManager.panelConfigurations[i].isCollision = EditorGUILayout.Toggle("Use collision", debugManager.panelConfigurations[i].isCollision);
//Collider
if (debugManager.panelConfigurations[i].isCollision)
{
//Save the object name
debugManager.panelConfigurations[i].objectName = EditorGUILayout.TextField("Collider name", debugManager.panelConfigurations[i].objectName);
EditorGUILayout.Space(5);
//Style
EditorGUILayout.LabelField("Style", EditorStyles.boldLabel);
//Primary color
debugManager.panelConfigurations[i].gizmoPrimaryColor = EditorGUILayout.ColorField("True color", debugManager.panelConfigurations[i].gizmoPrimaryColor);
//Secondary color
debugManager.panelConfigurations[i].gizmoSecondaryColor = EditorGUILayout.ColorField("False color", debugManager.panelConfigurations[i].gizmoSecondaryColor);
}
}
//------ Buttons ------
//Add
private void AddButton(DebugManager debugManager)
{
//Add Item
if (GUILayout.Button("Add"))
{
//Create a hollow pane
PanelConfiguration newPanel = new PanelConfiguration();
//add the hollow panel to the list
debugManager.panelConfigurations.Add(newPanel);
isShowing.Add(true);
selectedVariableIndex.Add(0);
}
}
//Remove
public void RemoveButton(DebugManager debugManager, int i)
{
//Optimization
PanelConfiguration config = debugManager.panelConfigurations[i];
//Remove Item
if (debugManager.panelConfigurations.Count > 0)
{
if (GUILayout.Button("Remove", GUILayout.Width(100)))
{
//Variables
string objectToRemove;
GameObject toDestroy;
//if it is a panel
if (config.currentTab == 0)
{
//Removing the game object deleted panel
objectToRemove = config.objectName + " Panel";
toDestroy = GameObject.Find(objectToRemove);
if (toDestroy != null)
{
DestroyImmediate(toDestroy);
}
}
//if is a gizmo
else
{
//Removing the game object collider
objectToRemove = config.objectName + " Collider";
toDestroy = GameObject.Find(objectToRemove);
if (toDestroy != null)
{
DestroyImmediate(toDestroy);
}
}
//Removing the last panel
debugManager.panelConfigurations.Remove(config);
isShowing.Remove(isShowing[i]);
selectedVariableIndex.Remove(selectedVariableIndex[i]);
}
}
}
//Remove last
private void RemoveLastButton(DebugManager debugManager)
{
//Remove Item
if (debugManager.panelConfigurations.Count > 0)
{
if (GUILayout.Button("Remove last"))
{
//Variables
string objectToRemove;
GameObject toDestroy;
//Optimization
PanelConfiguration config = debugManager.panelConfigurations[debugManager.panelConfigurations.Count - 1];
if (config.currentTab == 0)
{
//Removing the game object deleted panel
objectToRemove = config.objectName + " Panel";
toDestroy = GameObject.Find(objectToRemove);
if (toDestroy != null)
{
DestroyImmediate(toDestroy);
}
}
else
{
//Removing the game object collider
objectToRemove = config.objectName + " Collider";
toDestroy = GameObject.Find(objectToRemove);
if (toDestroy != null)
{
DestroyImmediate(toDestroy);
}
}
//Removing the last panel
debugManager.panelConfigurations.RemoveAt(debugManager.panelConfigurations.Count - 1);
isShowing.RemoveAt(isShowing.Count - 1);
selectedVariableIndex.RemoveAt(selectedVariableIndex.Count - 1);
}
}
}
//Clear all
private void ClearButton(DebugManager debugManager)
{
if (debugManager.panelConfigurations.Count > 1)
{
if (GUILayout.Button("Clear all"))
{
for (int i = 0; i < debugManager.panelConfigurations.Count; i++)
{
//Removing the game object deleted panel
string objectToRemove = debugManager.panelConfigurations[i].objectName + " Panel";
GameObject toDestroy = GameObject.Find(objectToRemove);
if (toDestroy != null)
{
DestroyImmediate(toDestroy);
}
}
debugManager.panelConfigurations.Clear();
isShowing.Clear();
selectedVariableIndex.Clear();
}
}
}
//Create
private void CreateButton(DebugManager debugManager)
{
//show the button create
if (GUILayout.Button("Create"))
{
for (int i = 0; i < debugManager.panelConfigurations.Count; i++)
{
PanelConfiguration config = debugManager.panelConfigurations[i];
GameObject copyObject = GameObject.Find(config.objectName + " Panel");
if (copyObject != null && copyObject.name == config.objectName + " Panel")
{
Debug.LogWarning("The Object: " + copyObject.name + " Already exist");
continue;
}
//if the current tab is a panel
if (config.currentTab == 0)
{
debugManager.CanvasSetup(i);
}
}
}
}
//Save the selected variable type
private void SaveTypeVariable(DebugManager debugManager, int configurationIndex, bool interval, bool plotGraph)
{
PanelConfiguration debugPanel = debugManager.panelConfigurations[configurationIndex];
if (debugPanel.script != null)
{
//Save the current type
switch (debugPanel.variable)
{
case DebugManager.PanelConfiguration.Variable.integer:
debugPanel.integer = true;
debugPanel.floats = false;
debugPanel.boolean = false;
debugPanel.strings = false;
debugPanel.transform = false;
break;
case DebugManager.PanelConfiguration.Variable.floats:
debugPanel.integer = false;
debugPanel.floats = true;
debugPanel.boolean = false;
debugPanel.strings = false;
debugPanel.transform = false;
break;
case DebugManager.PanelConfiguration.Variable.boolean:
debugPanel.integer = false;
debugPanel.floats = false;
debugPanel.boolean = true;
debugPanel.strings = false;
debugPanel.transform = false;
break;
case DebugManager.PanelConfiguration.Variable.strings:
debugPanel.integer = false;
debugPanel.floats = false;
debugPanel.boolean = false;
debugPanel.strings = true;
debugPanel.transform = false;
break;
case DebugManager.PanelConfiguration.Variable.transform:
debugPanel.integer = false;
debugPanel.floats = false;
debugPanel.boolean = false;
debugPanel.transform = true;
break;
default:
break;
}
}
else
{
debugPanel.integer = false;
debugPanel.floats = false;
debugPanel.boolean = false;
debugPanel.strings = false;
debugPanel.transform = false;
}
if ((debugPanel.floats || debugPanel.integer))
{
//if the user wants to set an interval
if (interval)
{
//Interval Method
ShowInterval(debugManager, configurationIndex);
}
if (plotGraph)
{
PlotGraph(debugManager, configurationIndex);
}
}
if (debugPanel.transform)
{
TransformCheckBox(debugManager, configurationIndex);
}
}
//Save the selected type of gizmo
private void SaveTypeGizmo(DebugManager debugManager, int configurationIndex)
{
PanelConfiguration debugPanel = debugManager.panelConfigurations[configurationIndex];
//Save the current type
switch (debugPanel.gizmoType)
{
case DebugManager.PanelConfiguration.GizmoType.line:
debugPanel.isLine = true;
debugPanel.isCube = false;
debugPanel.isSphere = false;
break;
case DebugManager.PanelConfiguration.GizmoType.cube:
debugPanel.isLine = false;
debugPanel.isCube = true;
debugPanel.isSphere = false;
break;
case DebugManager.PanelConfiguration.GizmoType.sphere:
debugPanel.isLine = false;
debugPanel.isCube = false;
debugPanel.isSphere = true;
break;
default:
debugPanel.isLine = false;
debugPanel.isCube = false;
debugPanel.isSphere = false;
break;
}
}
//Plot Graph setup
private void PlotGraph(DebugManager debugManager, int index)
{
// Optimization
PanelConfiguration debugPanel = debugManager.panelConfigurations[index];
debugPanel.plotGraph = EditorGUILayout.Toggle("Plot Graph", debugPanel.plotGraph);
// If the plot graph is visible
if (debugPanel.plotGraph)
{
// Title
EditorGUILayout.LabelField("Graph Setup", EditorStyles.boldLabel);
// Start indent level
EditorGUI.indentLevel++;
//Graph here...
debugPanel.graphScale = EditorGUILayout.Slider("Graph Scale %", debugPanel.graphScale, 1, 1000);
DrawGraph(debugPanel.xValues, debugPanel.yValues, debugPanel);
// ========== Graph time interval ==========
debugPanel.graphCustomInterval = EditorGUILayout.Toggle("Custom Interval", debugPanel.graphCustomInterval);
// Check if the user wants to use a custom interval
if (debugPanel.graphCustomInterval)
{
// Custom interval
debugPanel.graphInterval = EditorGUILayout.IntSlider("Graph update interval: ", (int)debugPanel.graphInterval, 1, 25);
}
else
{
//debugPanel.graphInterval = 1/60;
// Warning message
EditorGUILayout.HelpBox("If 'custom interval' is false, the update interval will be set by the frame rate.", MessageType.Warning);
}
// Color
EditorGUILayout.Space(5);
EditorGUILayout.LabelField("Color", EditorStyles.boldLabel);
debugPanel.primaryColor = EditorGUILayout.ColorField("Line color: ", debugManager.panelConfigurations[index].primaryColor);
debugPanel.secondaryColor = EditorGUILayout.ColorField("Background color: ", debugManager.panelConfigurations[index].secondaryColor);
// End indent level
EditorGUI.indentLevel--;
}
}
private void DrawGraph(List<float> xValues, List<float> yValues, PanelConfiguration config)
{
int GraphWidth = (int)(5 * config.graphScale);//Length of the math function
const int GraphHeight = 500;//Height of the math function
int Padding = 20;
GUILayout.Label("Graph", EditorStyles.boldLabel);
// Creating the rectangle to draw the graph
Rect graphRect = GUILayoutUtility.GetRect(GraphWidth, GraphHeight);
graphRect = EditorGUI.IndentedRect(graphRect);
// origin of y axle
float origin = graphRect.yMax - graphRect.height / 2;
// x axle:
Handles.DrawLine(new Vector3(graphRect.x, origin), new Vector3(graphRect.xMax, origin));//Line draw
EditorGUI.LabelField(new Rect(EditorGUIUtility.currentViewWidth - 85, (origin - Padding), 100, 20), "Seconds");//title
int xInterval = 50;
// creating the style marking
GUIStyle measureStyle = new GUIStyle(GUI.skin.label);
measureStyle.fontSize = 8; // font size
// for each space in view width
for (int i = 0; i <= (int)EditorGUIUtility.currentViewWidth; i++)
{
// calculate the position of the x marks
if (i % xInterval == 0 && i > 0)
{
// Drawing the lines
Handles.DrawLine(new Vector3(i, origin - 5), new Vector3(i, origin + 5));//X lines
// drawing the marks
EditorGUI.LabelField(new Rect(i, origin, 100, 20), string.Format("{0:F1}s", i), measureStyle);
}
}
// Drawing the Y axle
Handles.DrawLine(new Vector3(graphRect.x, graphRect.yMax), new Vector3(graphRect.x, graphRect.y));
// Graph Draw
Handles.color = Color.blue;
int numPoints = Mathf.Min(xValues.Count, yValues.Count); //make sure the smaller number doesn't exceed the larger one
Vector3[] points = new Vector3[numPoints];
float speed = 100;
for (int i = 0; i < numPoints; i++)
{
float variableScale = config.graphScale / ((float)config.scriptVariable + 1);
float x = graphRect.x + xValues[i];
float y = origin - (yValues[i] * (config.graphScale * variableScale / speed));
points[i] = new Vector3(x, y);
}
//Maximum treatment here...
Handles.DrawAAPolyLine(points);
}
//Method that shows a interval checkbox
private void ShowInterval(DebugManager debugManager, int configurationIndex)
{
PanelConfiguration debugPanel = debugManager.panelConfigurations[configurationIndex];
//========= Interval of the float value =========
//Show the interval checkbox if the script variable is a float
//Create the toggle box
debugPanel.interval = EditorGUILayout.Toggle("Interval", debugPanel.interval);
//set the min and max values of the interval
if (debugPanel.interval)
{
//Save the min and max numbers
EditorGUILayout.BeginHorizontal();
//If it's a float, save the minimum and maximum float values
if (debugPanel.floats)
{
debugPanel.minNumber = EditorGUILayout.FloatField("Min Value", debugPanel.minNumber);
debugPanel.maxNumber = EditorGUILayout.FloatField("Max Value", debugPanel.maxNumber);
}
//else, save the minimum and maximum int values
else
{
debugPanel.minNumber = EditorGUILayout.IntField("Min Value", (int)debugPanel.minNumber);
debugPanel.maxNumber = EditorGUILayout.IntField("Max Value", (int)debugPanel.maxNumber);
}
//----- Range treatment -----
//if the minimum is different from the maximum
if (debugPanel.minNumber != debugPanel.maxNumber)
{
//if the minimum is bigger than maximum
if (debugPanel.minNumber > debugPanel.maxNumber)
{
//minimum is equal to maximum
debugPanel.minNumber = debugPanel.maxNumber;
}
//if the maximum is smaller than minimum
if (debugPanel.maxNumber < debugPanel.minNumber)
{
//maximum is equal to minimum
debugPanel.maxNumber = debugPanel.minNumber;
}
}
EditorGUILayout.EndHorizontal();
//Create a reset button
if (GUILayout.Button("Reset"))
{
debugPanel.minNumber = 0;
debugPanel.maxNumber = 1;
}
}
}
//create the transform treatment
private void TransformCheckBox(DebugManager debugManager, int configurationIndex)
{
DebugManager.PanelConfiguration debugPanel = debugManager.panelConfigurations[configurationIndex];
//Add gap
EditorGUI.indentLevel++;
//Creating the group
//Add gap
EditorGUI.indentLevel++;
//Create the checkbox for the transform options
//Position
debugPanel.position = EditorGUILayout.Toggle("Position", debugPanel.position);
//Rotation
debugPanel.rotation = EditorGUILayout.Toggle("Rotation", debugPanel.rotation);
//Scale
debugPanel.scale = EditorGUILayout.Toggle("Scale", debugPanel.scale);
//Booleans treatment
if (debugPanel.position)
{
debugPanel.position = true;
debugPanel.rotation = false;
debugPanel.scale = false;
}
else if (debugPanel.rotation)
{
debugPanel.position = false;
debugPanel.rotation = true;
debugPanel.scale = false;
}
else if (debugPanel.scale)
{
debugPanel.position = false;
debugPanel.rotation = false;
debugPanel.scale = true;
}
else
{
debugPanel.position = false;
debugPanel.rotation = false;
debugPanel.scale = false;
}
//Remove gap
EditorGUI.indentLevel--;
}
#region DisplayScriptVariables
//Method which display the variables of the user's script depending of the type
private void DisplayScriptVariables(MonoBehaviour script, DebugManager.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 (IsVariableOfType(field, variableType))
{
variableNames.Add(field.Name);
}
}
// if there's at least one variable, create a popup field
if (variableNames.Count > 0)
{
// Check if the variable is in a valid interval-
selectedVariableIndex[i] = Mathf.Clamp(selectedVariableIndex[i], 0, variableNames.Count - 1);
GUILayout.Space(15);
// Show the popup with the available options
selectedVariableIndex[i] = EditorGUILayout.Popup("Select Variable", selectedVariableIndex[i], variableNames.ToArray());
//("Index: " + selectedVariableIndex[i]);
// Obtain the name of the selected variable
string selectedVariableName = variableNames[selectedVariableIndex[i]];
// 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
debugManager.panelConfigurations[i].variableIndex = selectedVariableIndex[i];
EditorGUILayout.LabelField(selectedVariableName, field.GetValue(script).ToString());
break;
}
}
}
else
{
EditorGUILayout.LabelField("No variables available");
}
}
#endregion
//Method that check the type of the variable
public static bool IsVariableOfType(FieldInfo field, DebugManager.PanelConfiguration.Variable variableType)
{
switch (variableType)
{
case DebugManager.PanelConfiguration.Variable.integer:
return field.FieldType == typeof(int);
case DebugManager.PanelConfiguration.Variable.floats:
return field.FieldType == typeof(float);
case DebugManager.PanelConfiguration.Variable.boolean:
return field.FieldType == typeof(bool);
case DebugManager.PanelConfiguration.Variable.strings:
return field.FieldType == typeof(string);
case DebugManager.PanelConfiguration.Variable.transform:
return field.FieldType == typeof(Transform);
default:
return false;
}
}
}
#endif
#if UNITY_EDITOR
[InitializeOnLoad]
public class DebugManagerSelection : MonoBehaviour
{
static DebugManagerSelection()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredPlayMode)
{
// Find the DebugManager in the scene
DebugManager[] debugManagers = FindObjectsOfType<DebugManager>();
//Update each debug manager panel
foreach (DebugManager debugManager in debugManagers)
{