Skip to content

Commit f9644ba

Browse files
authored
Fixes for the button iconset inspectors (#9999)
1 parent aff2311 commit f9644ba

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

Assets/MRTK/SDK/Features/UX/Scripts/Buttons/ButtonIconSet.cs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ public static string ConvertUInt32ToUnicodeCharString(uint unicode)
205205
private Texture[] spriteIconTextures = null;
206206
private static Material fontRenderMat;
207207

208+
private const string missingPreviewImagesMessage = "Not all icon previews were loaded. Check the settings of the icons included in your iconset.";
208209
private const string noIconFontMessage = "No icon font selected. Icon fonts will be unavailable.";
209210
private const string downloadIconFontMessage = "For instructions on how to install the HoloLens icon font, click the button below.";
210211
private const string hololensIconFontUrl = "https://docs.microsoft.com/windows/mixed-reality/mrtk-unity/features/ux-building-blocks/button";
@@ -344,8 +345,16 @@ public bool EditorDrawSpriteIconSelector(Sprite currentSprite, out bool foundSpr
344345
{
345346
float height = maxButtonSize * ((float)spriteIcons.Length / maxButtonsPerColumn);
346347
var maxHeight = GUILayout.MaxHeight(height);
348+
349+
bool allPreviewsLoaded;
350+
var gridContent = GenerateGridContent(spriteIconTextures, out allPreviewsLoaded);
351+
352+
if(!allPreviewsLoaded)
353+
{
354+
EditorGUILayout.HelpBox(missingPreviewImagesMessage, MessageType.Warning);
355+
}
347356
#if UNITY_2019_3_OR_NEWER
348-
int newSelection = GUILayout.SelectionGrid(currentSelection, spriteIconTextures, maxButtonsPerColumn, maxHeight);
357+
int newSelection = GUILayout.SelectionGrid(currentSelection, gridContent, maxButtonsPerColumn, maxHeight);
349358
#else
350359
var maxWidth = GUILayout.MaxWidth(maxButtonSize * maxButtonsPerColumn);
351360
int newSelection = GUILayout.SelectionGrid(currentSelection, spriteIconTextures, maxButtonsPerColumn, maxHeight, maxWidth);
@@ -363,7 +372,6 @@ public bool EditorDrawSpriteIconSelector(Sprite currentSprite, out bool foundSpr
363372
return newSprite != null;
364373
}
365374

366-
367375
/// <summary>
368376
/// Updates the cached sprite icon textures to the latest textures in spriteIcons
369377
/// </summary>
@@ -420,8 +428,16 @@ public bool EditorDrawQuadIconSelector(Texture currentTexture, out bool foundTex
420428
{
421429
float height = maxButtonSize * ((float)quadIcons.Length / maxButtonsPerColumn);
422430
var maxHeight = GUILayout.MaxHeight(height);
431+
432+
bool allPreviewsLoaded;
433+
var gridContent = GenerateGridContent(quadIcons, out allPreviewsLoaded);
434+
435+
if (!allPreviewsLoaded)
436+
{
437+
EditorGUILayout.HelpBox(missingPreviewImagesMessage, MessageType.Warning);
438+
}
423439
#if UNITY_2019_3_OR_NEWER
424-
int newSelection = GUILayout.SelectionGrid(currentSelection, quadIcons, maxButtonsPerColumn, maxHeight);
440+
int newSelection = GUILayout.SelectionGrid(currentSelection, gridContent, maxButtonsPerColumn, maxHeight);
425441
#else
426442
var maxWidth = GUILayout.MaxWidth(maxButtonSize * maxButtonsPerColumn);
427443
int newSelection = GUILayout.SelectionGrid(currentSelection, quadIcons, maxButtonsPerColumn, maxHeight, maxWidth);
@@ -544,6 +560,27 @@ public bool EditorAddCustomQuadIcon(Texture customQuadIcon)
544560
return true;
545561
}
546562

563+
public GUIContent[] GenerateGridContent(Texture[] previewTextures, out bool allPreviewsLoaded)
564+
{
565+
GUIContent[] gridContent = new GUIContent[previewTextures.Length];
566+
allPreviewsLoaded = true;
567+
568+
for (int i = 0; i < previewTextures.Length; i++)
569+
{
570+
if (previewTextures[i] != null)
571+
{
572+
gridContent[i] = new GUIContent(previewTextures[i]);
573+
}
574+
else
575+
{
576+
gridContent[i] = new GUIContent("N/A");
577+
allPreviewsLoaded = false;
578+
}
579+
}
580+
581+
return gridContent;
582+
}
583+
547584
[CustomEditor(typeof(ButtonIconSet))]
548585
private class ButtonIconSetInspector : UnityEditor.Editor
549586
{
@@ -578,6 +615,7 @@ public override void OnInspectorGUI()
578615

579616
#if UNITY_2019_3_OR_NEWER
580617
showQuadIconFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(showQuadIconFoldout, "Quad Icons");
618+
EditorGUILayout.EndFoldoutHeaderGroup();
581619
#else
582620
showQuadIconFoldout = EditorGUILayout.Foldout(showQuadIconFoldout, "Quad Icons");
583621
#endif
@@ -589,12 +627,10 @@ public override void OnInspectorGUI()
589627
EditorGUILayout.PropertyField(quadIconsProp, true);
590628
}
591629
}
592-
#if UNITY_2019_3_OR_NEWER
593-
EditorGUILayout.EndFoldoutHeaderGroup();
594-
#endif
595630

596631
#if UNITY_2019_3_OR_NEWER
597632
showSpriteIconFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(showSpriteIconFoldout, "Sprite Icons");
633+
EditorGUILayout.EndFoldoutHeaderGroup();
598634
#else
599635
showSpriteIconFoldout = EditorGUILayout.Foldout(showSpriteIconFoldout, "Sprite Icons");
600636
#endif
@@ -615,21 +651,14 @@ public override void OnInspectorGUI()
615651
}
616652
}
617653
}
618-
#if UNITY_2019_3_OR_NEWER
619-
EditorGUILayout.EndFoldoutHeaderGroup();
620-
#endif
621654

622655
#if UNITY_2019_3_OR_NEWER
623656
showCharIconFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(showCharIconFoldout, "Font Icons");
657+
EditorGUILayout.EndFoldoutHeaderGroup();
624658
#else
625659
showCharIconFoldout = EditorGUILayout.Foldout(showCharIconFoldout, "Font Icons");
626660
#endif
627661

628-
629-
#if UNITY_2019_3_OR_NEWER
630-
EditorGUILayout.EndFoldoutHeaderGroup();
631-
#endif
632-
633662
if (showCharIconFoldout)
634663
{
635664
EditorGUILayout.PropertyField(charIconFontProp);
@@ -823,6 +852,11 @@ private bool CheckIfHololensIconFontExists()
823852

824853
Texture2D GetTextureFromSprite(Sprite sprite)
825854
{
855+
if (sprite == null || sprite.texture == null)
856+
{
857+
return null;
858+
}
859+
826860
var rect = sprite.rect;
827861
var tex = new Texture2D((int)rect.width, (int)rect.height);
828862
var data = sprite.texture.GetPixels((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height);

0 commit comments

Comments
 (0)