-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathInspectorUIUtility.cs
More file actions
1200 lines (1010 loc) · 50.2 KB
/
InspectorUIUtility.cs
File metadata and controls
1200 lines (1010 loc) · 50.2 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
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace MixedReality.Toolkit.Editor
{
/// <summary>
/// This class has handy inspector UI utilities and functions.
/// </summary>
public static class InspectorUIUtility
{
#region Color
/// <summary>
/// Default background color, depending
/// on light/dark theme.
/// </summary>
public static Color DefaultBackgroundColor => EditorGUIUtility.isProSkin
? new Color32(56, 56, 56, 255)
: new Color32(194, 194, 194, 255);
// Colors
private static readonly Color PersonalThemeColorTint100 = new Color(1f, 1f, 1f);
private static readonly Color PersonalThemeColorTint75 = new Color(0.75f, 0.75f, 0.75f);
private static readonly Color PersonalThemeColorTint50 = new Color(0.5f, 0.5f, 0.5f);
private static readonly Color PersonalThemeColorTint25 = new Color(0.25f, 0.25f, 0.25f);
private static readonly Color PersonalThemeColorTint10 = new Color(0.10f, 0.10f, 0.10f);
private static readonly Color ProfessionalThemeColorTint100 = new Color(0f, 0f, 0f);
private static readonly Color ProfessionalThemeColorTint75 = new Color(0.25f, 0.25f, 0.25f);
private static readonly Color ProfessionalThemeColorTint50 = new Color(0.5f, 0.5f, 0.5f);
private static readonly Color ProfessionalThemeColorTint25 = new Color(0.75f, 0.75f, 0.75f);
private static readonly Color ProfessionalThemeColorTint10 = new Color(0.9f, 0.9f, 0.9f);
/// <summary>
/// A color based on the Unity theme color at a 100% tint.
/// </summary>
/// <remarks>
/// The Unity theme color is based on whether the user is using a Unity Pro license.
/// </remarks>
public static Color ColorTint100 => EditorGUIUtility.isProSkin ? ProfessionalThemeColorTint100 : PersonalThemeColorTint100;
/// <summary>
/// A color based on the Unity theme color at a 75% tint.
/// </summary>
/// <remarks>
/// The Unity theme color is based on whether the user is using a Unity Pro license.
/// </remarks>
public static Color ColorTint75 => EditorGUIUtility.isProSkin ? ProfessionalThemeColorTint75 : PersonalThemeColorTint75;
/// <summary>
/// A color based on the Unity theme color at a 50% tint.
/// </summary>
/// <remarks>
/// The Unity theme color is based on whether the user is using a Unity Pro license.
/// </remarks>
public static Color ColorTint50 => EditorGUIUtility.isProSkin ? ProfessionalThemeColorTint50 : PersonalThemeColorTint50;
/// <summary>
/// A color based on the Unity theme color at a 25% tint.
/// </summary>
/// <remarks>
/// The Unity theme color is based on whether the user is using a Unity Pro license.
/// </remarks>
public static Color ColorTint25 => EditorGUIUtility.isProSkin ? ProfessionalThemeColorTint25 : PersonalThemeColorTint25;
/// <summary>
/// A color based on the Unity theme color at a 10% tint.
/// </summary>
/// <remarks>
/// The Unity theme color is based on whether the user is using a Unity Pro license.
/// </remarks>
public static Color ColorTint10 => EditorGUIUtility.isProSkin ? ProfessionalThemeColorTint10 : PersonalThemeColorTint10;
/// <summary>
/// A color to use for disabled elements in custom editor UI.
/// </summary>
public static readonly Color DisabledColor = new Color(0.6f, 0.6f, 0.6f);
/// <summary>
/// A color to use for warning elements in custom editor UI.
/// </summary>
public static readonly Color WarningColor = new Color(1f, 0.85f, 0.6f);
/// <summary>
/// A color to use for error elements in custom editor UI.
/// </summary>
public static readonly Color ErrorColor = new Color(1f, 0.55f, 0.5f);
/// <summary>
/// A color to use for success elements in custom editor UI.
/// </summary>
public static readonly Color SuccessColor = new Color(0.8f, 1f, 0.75f);
/// <summary>
/// A color to use for selected elements in custom editor UI.
/// </summary>
public static readonly Color SectionColor = new Color(0.85f, 0.9f, 1f);
/// <summary>
/// A color to use for dark elements in custom editor UI.
/// </summary>
public static readonly Color DarkColor = new Color(0.1f, 0.1f, 0.1f);
/// <summary>
/// A color to use for square handle elements in custom editor UI.
/// </summary>
public static readonly Color HandleColorSquare = new Color(0.0f, 0.9f, 1f);
/// <summary>
/// A color to use for circle handle elements in custom editor UI.
/// </summary>
public static readonly Color HandleColorCircle = new Color(1f, 0.5f, 1f);
/// <summary>
/// A color to use for sphere handle elements in custom editor UI.
/// </summary>
public static readonly Color HandleColorSphere = new Color(1f, 0.5f, 1f);
/// <summary>
/// A color to use for axis handle elements in custom editor UI.
/// </summary>
public static readonly Color HandleColorAxis = new Color(0.0f, 1f, 0.2f);
/// <summary>
/// A color to use for rotation handle elements in custom editor UI.
/// </summary>
public static readonly Color HandleColorRotation = new Color(0.0f, 1f, 0.2f);
/// <summary>
/// A color to use for tangent handle elements in custom editor UI.
/// </summary>
public static readonly Color HandleColorTangent = new Color(0.1f, 0.8f, 0.5f, 0.7f);
/// <summary>
/// A color to use for line velocity elements in custom editor UI.
/// </summary>
public static readonly Color LineVelocityColor = new Color(0.9f, 1f, 0f, 0.8f);
#endregion Color
#region Sizes
/// <summary>
/// The default font size for title elements in custom editor UI.
/// </summary>
public const int TitleFontSize = 14;
/// <summary>
/// The default font size for header elements in custom editor UI.
/// </summary>
public const int HeaderFontSize = 11;
/// <summary>
/// The default font size for text elements in custom editor UI.
/// </summary>
public const int DefaultFontSize = 10;
/// <summary>
/// The default width of documentation link elements in custom editor UI.
/// </summary>
public const float DocLinkWidth = 175f;
#endregion Sizes
#region Special unicode characters
/// <summary>
/// The unicode value for the 'minus sign' character.
/// </summary>
public static readonly string Minus = "\u2212";
/// <summary>
/// The unicode value for the 'plus sign' character.
/// </summary>
public static readonly string Plus = "\u002B";
/// <summary>
/// The unicode value for the 'asterisk operator' character.
/// </summary>
public static readonly string Asterisk = "\u2217";
/// <summary>
/// The unicode value for the 'left arrowhead' character.
/// </summary>
public static readonly string Left = "\u02C2";
/// <summary>
/// The unicode value for the 'right arrowhead' character.
/// </summary>
public static readonly string Right = "\u02C3";
/// <summary>
/// The unicode value for the 'up arrowhead' character.
/// </summary>
public static readonly string Up = "\u02C4";
/// <summary>
/// The unicode value for the 'down arrowhead' character.
/// </summary>
public static readonly string Down = "\u02C5";
/// <summary>
/// The unicode value for the 'multiplication x' character.
/// </summary>
/// <remarks>
/// The 'multiplication x' character also represents a 'close' icon in MRTK.
/// </remarks>
public static readonly string Close = "\u2715";
/// <summary>
/// The unicode value for the 'heart suit' character.
/// </summary>
public static readonly string Heart = "\u2661";
/// <summary>
/// The unicode value for the 'star' character.
/// </summary>
public static readonly string Star = "\u2606";
/// <summary>
/// The unicode value for the 'smiling face' character.
/// </summary>
public static readonly string Emoji = "\u263A";
/// <summary>
/// The unicode value for the 'clockwise open circle arrow' character.
/// </summary>
/// <remarks>
/// The 'clockwise open circle arrow' character also represents a 'reload' icon in MRTK.
/// </remarks>
public static readonly string Reload = "\u21BB";
#endregion Special unicode characters
#region Handy icon textures
/// <summary>
/// A Unity <see cref="Texture"/> representing a 'help' icon.
/// </summary>
public static readonly Texture HelpIcon = EditorGUIUtility.IconContent("_Help").image;
/// <summary>
/// A Unity <see cref="Texture"/> representing a 'success' icon.
/// </summary>
public static readonly Texture SuccessIcon = EditorGUIUtility.IconContent("Collab").image;
/// <summary>
/// A Unity <see cref="Texture"/> representing a 'warning' icon.
/// </summary>
public static readonly Texture WarningIcon = EditorGUIUtility.IconContent("console.warnicon").image;
/// <summary>
/// A Unity <see cref="Texture"/> representing a 'information' icon.
/// </summary>
public static readonly Texture InfoIcon = EditorGUIUtility.IconContent("console.infoicon").image;
// StandardAssets/Textures/MRTK_Logo_Black.png
private const string LogoLightThemeGuid = "fa0038d8d2df1dd4c99f346c8ec9e746";
// StandardAssets/Textures/MRTK_Logo_White.png
private const string LogoDarkThemeGuid = "fe5cc215f12ea5e40b5021c4040bce24";
/// <summary>
/// The MRTK logo texture suitable for a light theme.
/// </summary>
public static readonly Texture2D LogoLightTheme = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(LogoLightThemeGuid));
/// <summary>
/// The MRTK logo texture suitable for a dark theme.
/// </summary>
public static readonly Texture2D LogoDarkTheme = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(LogoDarkThemeGuid));
#endregion Handy icon textures
#region Handy drawables/controls
/// <summary>
/// Delegate for button callbacks, single index
/// </summary>
/// <param name="index">location of item in a serialized array</param>
/// <param name="prop">A serialize property containing information needed if the button was clicked</param>
public delegate void ListButtonEvent(int index, SerializedProperty prop = null);
/// <summary>
/// Delegate for button callbacks, multi-index for nested arrays
/// </summary>
/// <param name="indexArray">location of item in a serialized array</param>
/// <param name="prop">A serialize property containing information needed if the button was clicked</param>
public delegate void MultiListButtonEvent(int[] indexArray, SerializedProperty prop = null);
internal static bool IsMixedRealityToolkitLogoAssetPresent()
{
return EditorGUIUtility.isProSkin ? LogoDarkTheme != null : LogoLightTheme != null;
}
/// <summary>
/// Render the Mixed Reality Toolkit Logo.
/// </summary>
/// <returns><see langword="true"/> if the logo was loadable and can be rendered, or <see langword="false"/> if a text fallback was used.</returns>
public static bool RenderMixedRealityToolkitLogo()
{
if (IsMixedRealityToolkitLogoAssetPresent())
{
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
GUILayout.Label(EditorGUIUtility.isProSkin ? LogoDarkTheme : LogoLightTheme, GUILayout.MaxHeight(96f));
GUILayout.FlexibleSpace();
}
GUILayout.Space(3f);
return true;
}
else
{
EditorGUILayout.LabelField("Microsoft Mixed Reality Toolkit", MRTKEditorStyles.ProductNameStyle);
GUILayout.Space(3f);
return false;
}
}
/// <summary>
/// Helper function to render buttons correctly indented according to EditorGUI.indentLevel since GUILayout component don't respond naturally
/// </summary>
/// <param name="buttonText">text to place in button</param>
/// <param name="options">layout options</param>
/// <returns><see langword="true"/> if button clicked, <see langword="false"/> if otherwise</returns>
public static bool RenderIndentedButton(string buttonText, params GUILayoutOption[] options)
{
return RenderIndentedButton(() => { return GUILayout.Button(buttonText, options); });
}
/// <summary>
/// Helper function to render buttons correctly indented according to EditorGUI.indentLevel since GUILayout component don't respond naturally
/// </summary>
/// <param name="content">What to draw in button</param>
/// <param name="style">Style configuration for button</param>
/// <param name="options">layout options</param>
/// <returns><see langword="true"/> if button clicked, <see langword="false"/> if otherwise.</returns>
public static bool RenderIndentedButton(GUIContent content, GUIStyle style, params GUILayoutOption[] options)
{
return RenderIndentedButton(() => { return GUILayout.Button(content, style, options); });
}
/// <summary>
/// Helper function to support primary overloaded version of this functionality
/// </summary>
/// <param name="renderButton">The code to render button correctly based on parameter types passed</param>
/// <returns><see langword="true"/> if button clicked, <see langword="false"/> if otherwise.</returns>
public static bool RenderIndentedButton(Func<bool> renderButton)
{
GUILayout.BeginHorizontal();
GUILayout.Space(EditorGUI.indentLevel * 15);
bool result = renderButton();
GUILayout.EndHorizontal();
return result;
}
/// <summary>
/// Render documentation button routing to relevant URI.
/// </summary>
/// <param name="docURL">The documentation URL to open on button click.</param>
/// <param name="width">The width of the documentation link button.</param>
/// <returns>
/// <see langword="true"/> if button clicked, <see langword="false"/> otherwise.
/// </returns>
public static bool RenderDocumentationButton(string docURL, float width = DocLinkWidth)
{
if (!string.IsNullOrEmpty(docURL))
{
var buttonContent = new GUIContent()
{
image = HelpIcon,
text = " Documentation",
tooltip = docURL,
};
// The documentation button should always be enabled.
using (new GUIEnabledWrapper())
{
if (GUILayout.Button(buttonContent, EditorStyles.miniButton, GUILayout.MaxWidth(width)))
{
Application.OpenURL(docURL);
return true;
}
}
}
return false;
}
/// <summary>
/// Render a documentation header with button if Object contains HelpURLAttribute
/// </summary>
/// <param name="targetType">Type to test for HelpURLAttribute</param>
/// <returns><see langword="true"/> if object drawn and button clicked, <see langword="false"/> otherwise.</returns>
public static bool RenderHelpURL(Type targetType)
{
bool result = false;
if (targetType != null)
{
HelpURLAttribute helpURL = targetType.GetCustomAttribute<HelpURLAttribute>();
if (helpURL != null)
{
result = RenderDocumentationSection(helpURL.URL);
}
}
return result;
}
/// <summary>
/// Render a documentation header with button for given url value
/// </summary>
/// <param name="url">Url to open if button is clicked</param>
/// <returns><see langword="true"/> if object drawn and button clicked, <see langword="false"/> otherwise.</returns>
public static bool RenderDocumentationSection(string url)
{
bool result = false;
if (!string.IsNullOrEmpty(url))
{
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
result = RenderDocumentationButton(url);
}
}
return result;
}
/// <summary>
/// A button that is as wide as the label
/// </summary>
public static bool FlexButton(GUIContent label, int index, ListButtonEvent callback, SerializedProperty prop = null)
{
if (FlexButton(label))
{
callback(index, prop);
return true;
}
return false;
}
/// <summary>
/// A button that is as wide as the label
/// </summary>
/// <returns><see langword="true"/> if button clicked, <see langword="false"/> otherwise.</returns>
public static bool FlexButton(GUIContent label, int[] indexArr, MultiListButtonEvent callback, SerializedProperty prop = null)
{
if (FlexButton(label))
{
callback(indexArr, prop);
return true;
}
return false;
}
/// <summary>
/// A button that is as wide as the label
/// </summary>
/// <param name="label">content for button</param>
/// <returns><see langword="true"/> if button clicked, <see langword="false"/> otherwise.</returns>
public static bool FlexButton(GUIContent label)
{
GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
float buttonWidth = GUI.skin.button.CalcSize(label).x;
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
if (GUILayout.Button(label, buttonStyle, GUILayout.Width(buttonWidth)))
{
return true;
}
}
return false;
}
/// <summary>
/// A button that is as wide as the available space.
/// </summary>
public static bool FullWidthButton(GUIContent label, float padding, int index, ListButtonEvent callback, SerializedProperty prop = null)
{
GUIStyle addStyle = new GUIStyle(GUI.skin.button);
addStyle.fixedHeight = 25;
float addButtonWidth = GUI.skin.button.CalcSize(label).x * padding;
bool triggered = false;
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
if (GUILayout.Button(label, addStyle, GUILayout.Width(addButtonWidth)))
{
callback(index, prop);
triggered = true;
}
GUILayout.FlexibleSpace();
}
return triggered;
}
/// <summary>
/// A button that is as wide as the available space.
/// </summary>
public static bool FullWidthButton(GUIContent label, float padding, int[] indexArr, MultiListButtonEvent callback, SerializedProperty prop = null)
{
GUIStyle addStyle = new GUIStyle(GUI.skin.button);
addStyle.fixedHeight = 25;
float addButtonWidth = GUI.skin.button.CalcSize(label).x * padding;
bool triggered = false;
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();
if (GUILayout.Button(label, addStyle, GUILayout.Width(addButtonWidth)))
{
callback(indexArr, prop);
triggered = true;
}
GUILayout.FlexibleSpace();
}
return triggered;
}
/// <summary>
/// A small editor button.
/// </summary>
/// <remarks>
/// This is good for a single icon, like a 'plus sign' or 'minus sign', with a single index callback event.
/// </remarks>
/// <param name="label">The content to place in the button.</param>
/// <param name="index">The index to pass into the <paramref name="callback"/> when the editor button is clicked.</param>
/// <param name="callback">A delegate callback to invoke when the button is clicked.</param>
/// <param name="property">The <see cref="SerializedProperty"/> to pass into the <paramref name="callback"/> when the editor button is clicked.</param>
/// <returns>
/// <see langword="true"/> if button selected, <see langword="false"/> otherwise.
/// </returns>
public static bool SmallButton(GUIContent label, int index, ListButtonEvent callback, SerializedProperty property = null)
{
if (SmallButton(label))
{
callback(index, property);
return true;
}
return false;
}
/// <summary>
/// A small editor button.
/// </summary>
/// <remarks>
/// This is good for a single icon, like a 'plus sign' or 'minus sign', with a multiple-indices callback event.
/// </remarks>
/// <param name="label">The content to place in the button.</param>
/// <param name="indices">The indices to pass into the <paramref name="callback"/> when the editor button is clicked.</param>
/// <param name="callback">A delegate callback to invoke when the button is clicked.</param>
/// <param name="property">The <see cref="SerializedProperty"/> to pass into the <paramref name="callback"/> when the editor button is clicked.</param>
/// <returns>
/// <see langword="true"/> if button selected, <see langword="false"/> otherwise.
/// </returns>
public static bool SmallButton(GUIContent label, int[] indices, MultiListButtonEvent callback, SerializedProperty property = null)
{
if (SmallButton(label))
{
callback(indices, property);
return true;
}
return false;
}
/// <summary>
/// A small editor button.
/// </summary>
/// <remarks>
/// This is good for a single icon, like a 'plus sign' or 'minus sign'.
/// </remarks>
/// <param name="label">The content to place in the button.</param>
/// <returns>
/// <see langword="true"/> if button selected, <see langword="false"/> otherwise.
/// </returns>
public static bool SmallButton(GUIContent label)
{
GUIStyle smallButton = new GUIStyle(EditorStyles.miniButton);
float smallButtonWidth = GUI.skin.button.CalcSize(label).x;
if (GUILayout.Button(label, smallButton, GUILayout.Width(smallButtonWidth)))
{
return true;
}
return false;
}
/// <summary>
/// Large title format.
/// </summary>
/// <param name="title">The <see langword="string"/> content to render.</param>
public static void DrawTitle(string title)
{
GUIStyle labelStyle = MRTKEditorStyles.LabelStyle(TitleFontSize, ColorTint50);
EditorGUILayout.LabelField(new GUIContent(title), labelStyle);
GUILayout.Space(TitleFontSize * 0.5f);
}
/// <summary>
/// Medium title format.
/// </summary>
/// <param name="header">The <see langword="string"/> content to render.</param>
public static void DrawHeader(string header)
{
GUIStyle labelStyle = MRTKEditorStyles.LabelStyle(HeaderFontSize, ColorTint10);
EditorGUILayout.LabelField(new GUIContent(header), labelStyle);
}
/// <summary>
/// Draw a basic label.
/// </summary>
/// <param name="title">The <see langword="string"/> content to render.</param>
/// <param name="size">The label size.</param>
/// <param name="color">The label color.</param>
public static void DrawLabel(string title, int size, Color color)
{
GUIStyle labelStyle = MRTKEditorStyles.LabelStyle(size, color);
EditorGUILayout.LabelField(new GUIContent(title), labelStyle);
}
/// <summary>
/// Draw a label with a yellow warning color.
/// </summary>
/// <param name="warning">The <see langword="string"/> content to render.</param>
public static void DrawWarning(string warning)
{
Color prevColor = GUI.color;
GUI.color = WarningColor;
EditorGUILayout.BeginVertical(EditorStyles.textArea);
EditorGUILayout.LabelField(warning, EditorStyles.wordWrappedMiniLabel);
EditorGUILayout.EndVertical();
GUI.color = prevColor;
}
/// <summary>
/// Draw a notice area, normal coloring.
/// </summary>
/// <param name="notice">The <see langword="string"/> content to render.</param>
public static void DrawNotice(string notice)
{
Color prevColor = GUI.color;
GUI.color = ColorTint50;
using (new EditorGUILayout.VerticalScope(EditorStyles.textArea))
{
EditorGUILayout.LabelField(notice, EditorStyles.wordWrappedMiniLabel);
}
GUI.color = prevColor;
}
/// <summary>
/// Draw a notice with green success color.
/// </summary>
/// <param name="notice">The <see langword="string"/> content to render.</param>
public static void DrawSuccess(string notice)
{
Color prevColor = GUI.color;
GUI.color = SuccessColor;
EditorGUILayout.BeginVertical(EditorStyles.textArea);
EditorGUILayout.LabelField(notice, EditorStyles.wordWrappedMiniLabel);
EditorGUILayout.EndVertical();
GUI.color = prevColor;
}
/// <summary>
/// Draw a notice with red error coloring.
/// </summary>
/// <param name="error">The <see langword="string"/> content to render.</param>
public static void DrawError(string error)
{
Color prevColor = GUI.color;
GUI.color = ErrorColor;
EditorGUILayout.BeginVertical(EditorStyles.textArea);
EditorGUILayout.LabelField(error, EditorStyles.wordWrappedMiniLabel);
EditorGUILayout.EndVertical();
GUI.color = prevColor;
}
/// <summary>
/// Create a line across the negative space.
/// </summary>
public static void DrawDivider()
{
EditorGUILayout.LabelField(string.Empty, GUI.skin.horizontalSlider);
}
/// <summary>
/// Draws a start section start.
/// </summary>
public static bool DrawSectionFoldout(string headerName, bool open = true, GUIStyle style = null)
{
style ??= EditorStyles.foldout;
using (new EditorGUI.IndentLevelScope())
{
return EditorGUILayout.Foldout(open, headerName, true, style);
}
}
/// <summary>
/// Draws a section start with header name and save the open and close state to given
/// preference key in a <see cref="SessionState"/>.
/// </summary>
public static bool DrawSectionFoldoutWithKey(string headerName, string preferenceKey = null, GUIStyle style = null, bool defaultOpen = true)
{
bool showPref = SessionState.GetBool(preferenceKey, defaultOpen);
bool show = DrawSectionFoldout(headerName, showPref, style);
if (show != showPref)
{
SessionState.SetBool(preferenceKey, show);
}
return show;
}
/// <summary>
/// Draws a popup UI with <see cref="SerializedProperty"/> type features.
/// </summary>
/// <param name="property">The serialized property corresponding to the <see cref="Enum"/>.</param>
/// <param name="label">The label for the property.</param>
/// <param name="propertyValue">Current enumeration value for the property.</param>
/// <returns>New enumeration value after draw.</returns>
public static Enum DrawEnumSerializedProperty(SerializedProperty property, GUIContent label, Enum propertyValue)
{
return DrawEnumSerializedProperty(EditorGUILayout.GetControlRect(), property, label, propertyValue);
}
/// <summary>
/// Draws a popup UI with <see cref="SerializedProperty"/> type features.
/// </summary>
/// <param name="position">The position to render the serialized property.</param>
/// <param name="property">serialized property corresponding to <see cref="Enum"/>.</param>
/// <param name="label">The label for the property.</param>
/// <param name="propertyValue">Current enumeration value for the property.</param>
/// <returns>New enumeration value after draw.</returns>
public static Enum DrawEnumSerializedProperty(Rect position, SerializedProperty property, GUIContent label, Enum propertyValue)
{
Enum result = propertyValue;
EditorGUI.BeginProperty(position, label, property);
{
result = EditorGUI.EnumPopup(position, label, propertyValue);
property.intValue = Convert.ToInt32(result);
}
EditorGUI.EndProperty();
return result;
}
/// <summary>
/// Draws a foldout enlisting all components, or derived types, of the given type attached to the passed <see cref="GameObject"/>.
/// </summary>
/// <remarks>
/// This adds a button for adding any of the component, or derived types, and a follow button to highlight existing attached components.
/// </remarks>
public static bool DrawComponentTypeFoldout<T>(GameObject gameObject, bool isExpanded, string typeDescription) where T : MonoBehaviour
{
isExpanded = EditorGUILayout.Foldout(isExpanded, typeDescription + "s", true);
if (isExpanded)
{
if (EditorGUILayout.DropdownButton(new GUIContent("Add " + typeDescription), FocusType.Keyboard))
{
// create the menu and add items to it
GenericMenu menu = new GenericMenu();
var type = typeof(T);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetLoadableTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsAbstract);
foreach (var derivedType in types)
{
menu.AddItem(new GUIContent(derivedType.Name), false, t => gameObject.AddComponent((Type)t), derivedType);
}
menu.ShowAsContext();
}
var constraints = gameObject.GetComponents<T>();
foreach (var constraint in constraints)
{
EditorGUILayout.BeginHorizontal();
string constraintName = constraint.GetType().Name;
EditorGUILayout.LabelField(constraintName);
if (GUILayout.Button("Go to component"))
{
Highlighter.Highlight("Inspector", $"{ObjectNames.NicifyVariableName(constraintName)}");
EditorGUIUtility.ExitGUI();
}
EditorGUILayout.EndHorizontal();
}
}
return isExpanded;
}
static private void DrawScriptableSubEditor(SerializedProperty scriptable)
{
if (scriptable.objectReferenceValue != null)
{
UnityEditor.Editor configEditor = UnityEditor.Editor.CreateEditor(scriptable.objectReferenceValue);
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
EditorGUILayout.Space();
configEditor.OnInspectorGUI();
EditorGUILayout.Space();
EditorGUILayout.EndVertical();
}
}
#endregion Handy drawables/controls
#region Utilities
/// <summary>
/// Obtain the name for an field which is capable of holding a value for the given property.
/// </summary>
/// <param name="propertyName">The name of a property.</param>
public static string GetBackingField(string propertyName) => $"<{propertyName}>k__BackingField";
/// <summary>
/// Get the Unity editor main window position.
/// </summary>
public static Rect GetEditorMainWindowPosition()
{
// Found at https://answers.unity.com/questions/960413/editor-window-how-to-center-a-window.html
var containerWinType = AppDomain.CurrentDomain.GetAllDerivedTypes(typeof(ScriptableObject)).FirstOrDefault(t => t.Name == "ContainerWindow");
if (containerWinType == null)
{
throw new MissingMemberException("Can't find internal type ContainerWindow. Maybe something has changed inside Unity");
}
var showModeField = containerWinType.GetField("m_ShowMode", BindingFlags.NonPublic | BindingFlags.Instance);
var positionProperty = containerWinType.GetProperty("position", BindingFlags.Public | BindingFlags.Instance);
if (showModeField == null || positionProperty == null)
{
throw new MissingFieldException("Can't find internal fields 'm_ShowMode' or 'position'. Maybe something has changed inside Unity");
}
var windows = Resources.FindObjectsOfTypeAll(containerWinType);
foreach (var win in windows)
{
var showMode = (int)showModeField.GetValue(win);
if (showMode == 4) // main window
{
var pos = (Rect)positionProperty.GetValue(win, null);
return pos;
}
}
throw new NotSupportedException("Can't find internal main window. Maybe something has changed inside Unity");
}
private static Type[] GetAllDerivedTypes(this AppDomain appDomain, Type aType)
{
var result = new List<Type>();
var assemblies = appDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
var types = assembly.GetLoadableTypes();
result.AddRange(types.Where(type => type.IsSubclassOf(aType)));
}
return result.ToArray();
}
/// <summary>
/// Centers an editor window on the main display.
/// </summary>
public static void CenterOnMainWin(this EditorWindow window)
{
var main = GetEditorMainWindowPosition();
var pos = window.position;
float w = (main.width - pos.width) * 0.5f;
float h = (main.height - pos.height) * 0.5f;
pos.x = main.x + w;
pos.y = main.y + h;
window.position = pos;
}
#endregion Utilities
#region Handles
// Magic number for dashed lines for editor handles/affordances.
private const float DottedLineScreenSpace = 4.65f;
/// <summary>
/// Draw an axis move handle.
/// </summary>
/// <param name="target"><see href="https://docs.unity3d.com/ScriptReference/Object.html">Object</see> that is undergoing the transformation. Also used for recording undo.</param>
/// <param name="origin">The initial position of the axis.</param>
/// <param name="direction">The direction the axis is facing.</param>
/// <param name="distance">Distance from the axis.</param>
/// <param name="handleSize">Optional handle size.</param>
/// <param name="autoSize">Optional, auto sizes the handles based on position and handle size.</param>
/// <param name="recordUndo">Optional, records undo state.</param>
/// <returns>The new <see cref="float"/> value.</returns>
public static float AxisMoveHandle(Object target, Vector3 origin, Vector3 direction, float distance, float handleSize = 0.2f, bool autoSize = true, bool recordUndo = true)
{
Vector3 position = origin + (direction.normalized * distance);
Handles.color = HandleColorAxis;
if (autoSize)
{
handleSize = Mathf.Lerp(handleSize, HandleUtility.GetHandleSize(position) * handleSize, 0.75f);
}
Handles.DrawDottedLine(origin, position, DottedLineScreenSpace);
Handles.ArrowHandleCap(0, position, Quaternion.LookRotation(direction), handleSize * 2, EventType.Repaint);
Vector3 newPosition = Handles.FreeMoveHandle(position, handleSize, Vector3.zero, Handles.CircleHandleCap);
if (recordUndo)
{
float newDistance = Vector3.Distance(origin, newPosition);
if (!distance.Equals(newDistance))
{
Undo.RegisterCompleteObjectUndo(target, target.name);
distance = newDistance;
}
}
return distance;
}
/// <summary>
/// Draw a Circle Move Handle.
/// </summary>
/// <param name="target"><see href="https://docs.unity3d.com/ScriptReference/Object.html">Object</see> that is undergoing the transformation. Also used for recording undo.</param>
/// <param name="position">The position to draw the handle.</param>
/// <param name="xScale">Scale the new value on the x axis by this amount.</param>
/// <param name="yScale">Scale the new value on the x axis by this amount.</param>
/// <param name="zScale">Scale the new value on the x axis by this amount.</param>
/// <param name="handleSize">Optional handle size.</param>
/// <param name="autoSize">Optional, auto sizes the handles based on position and handle size.</param>
/// <param name="recordUndo">Optional, records undo state.</param>
/// <returns>The new <see href="https://docs.unity3d.com/ScriptReference/Vector3.html">Vector3</see> value.</returns>
public static Vector3 CircleMoveHandle(Object target, Vector3 position, float xScale = 1f, float yScale = 1f, float zScale = 1f, float handleSize = 0.2f, bool autoSize = true, bool recordUndo = true)
{
Handles.color = HandleColorCircle;
if (autoSize)
{
handleSize = Mathf.Lerp(handleSize, HandleUtility.GetHandleSize(position) * handleSize, 0.75f);
}
Vector3 newPosition = Handles.FreeMoveHandle(position, handleSize, Vector3.zero, Handles.CircleHandleCap);
if (recordUndo && position != newPosition)
{
Undo.RegisterCompleteObjectUndo(target, target.name);
position.x = Mathf.Lerp(position.x, newPosition.x, Mathf.Clamp01(xScale));
position.y = Mathf.Lerp(position.z, newPosition.y, Mathf.Clamp01(yScale));
position.z = Mathf.Lerp(position.y, newPosition.z, Mathf.Clamp01(zScale));
}