11// Copyright (c) Mixed Reality Toolkit Contributors
22// Licensed under the BSD 3-Clause
33
4- using System ;
4+ using System . Collections . Generic ;
55using TMPro ;
66using UnityEngine ;
77
@@ -11,7 +11,7 @@ namespace MixedReality.Toolkit.UX
1111 /// Allows the user to select a specific icon for display via a Unity text component.
1212 /// </summary>
1313 [ AddComponentMenu ( "MRTK/UX/Font Icon Selector" ) ]
14- public class FontIconSelector : MonoBehaviour
14+ public class FontIconSelector : MonoBehaviour , ISerializationCallbackReceiver
1515 {
1616 [ Tooltip ( "The FontIconSet that contains the icons available for use." ) ]
1717 [ SerializeField ]
@@ -52,6 +52,11 @@ public string CurrentIconName
5252 /// </summary>
5353 public TMP_Text TextMeshProComponent => textMeshProComponent ;
5454
55+ // A temporary variable used to migrate instances of FontIconSelector to use new FontIconSetDefinition names.
56+ // TODO: Remove this after some time to ensure users have successfully migrated.
57+ [ SerializeField , HideInInspector ]
58+ private bool migratedSuccessfully = false ;
59+
5560 /// <summary>
5661 /// A Unity event function that is called when an enabled script instance is being loaded.
5762 /// </summary>
@@ -83,12 +88,32 @@ private void OnValidate()
8388
8489 private void SetIcon ( string newIconName )
8590 {
86- if ( fontIcons != null && textMeshProComponent != null )
91+ if ( fontIcons != null && textMeshProComponent != null && fontIcons . TryGetGlyphIcon ( newIconName , out uint unicodeValue ) )
92+ {
93+ currentIconName = newIconName ;
94+ textMeshProComponent . text = FontIconSet . ConvertUnicodeToHexString ( unicodeValue ) ;
95+ }
96+ }
97+
98+ void ISerializationCallbackReceiver . OnBeforeSerialize ( ) { }
99+
100+ void ISerializationCallbackReceiver . OnAfterDeserialize ( )
101+ {
102+ if ( ! migratedSuccessfully && fontIcons != null && fontIcons . FontIconSetDefinition != null && textMeshProComponent != null )
87103 {
88- if ( fontIcons . TryGetGlyphIcon ( newIconName , out uint unicodeValue ) )
104+ uint unicodeValue = FontIconSet . ConvertHexStringToUnicode ( textMeshProComponent . text ) ;
105+ foreach ( KeyValuePair < string , uint > kv in fontIcons . GlyphIconsByName )
89106 {
90- currentIconName = newIconName ;
91- textMeshProComponent . text = FontIconSet . ConvertUnicodeToHexString ( unicodeValue ) ;
107+ if ( kv . Value == unicodeValue )
108+ {
109+ if ( currentIconName != kv . Key )
110+ {
111+ Debug . Log ( $ "[{ nameof ( FontIconSelector ) } ] Successfully migrated icon: \" { currentIconName } \" to \" { kv . Key } \" ", this ) ;
112+ currentIconName = kv . Key ;
113+ migratedSuccessfully = true ;
114+ }
115+ break ;
116+ }
92117 }
93118 }
94119 }
0 commit comments