@@ -36,19 +36,7 @@ public class FontIconSetInspector : UnityEditor.Editor
3636 private SerializedProperty iconFontAssetProp = null ;
3737 private SerializedProperty fontIconSetDefinitionProp = null ;
3838
39- private class IconEntry
40- {
41- public string Name ;
42- public uint UnicodeValue ;
43-
44- public IconEntry ( string name , uint unicodeValue )
45- {
46- Name = name ;
47- UnicodeValue = unicodeValue ;
48- }
49- }
50-
51- private List < IconEntry > iconEntries = new List < IconEntry > ( ) ;
39+ private SortedList < uint , string > iconEntries = new SortedList < uint , string > ( ) ;
5240
5341 /// <summary>
5442 /// A Unity event function that is called when the script component has been enabled.
@@ -62,7 +50,7 @@ private void OnEnable()
6250 // Make a list out of dictionary to avoid changing order while editing names
6351 foreach ( KeyValuePair < string , uint > kv in fontIconSet . GlyphIconsByName )
6452 {
65- iconEntries . Add ( new IconEntry ( kv . Key , kv . Value ) ) ;
53+ iconEntries . Add ( kv . Value , kv . Key ) ;
6654 }
6755 }
6856
@@ -98,8 +86,8 @@ public override void OnInspectorGUI()
9886 }
9987 else
10088 {
101- FontIconSet fontIconSet = ( FontIconSet ) target ;
102- TMP_FontAsset fontAsset = ( TMP_FontAsset ) iconFontAssetProp . objectReferenceValue ;
89+ FontIconSet fontIconSet = target as FontIconSet ;
90+ TMP_FontAsset fontAsset = iconFontAssetProp . objectReferenceValue as TMP_FontAsset ;
10391
10492 showAvailableIcons = EditorGUILayout . Foldout ( showAvailableIcons , "Available Icons" , true ) ;
10593 if ( showAvailableIcons )
@@ -140,8 +128,9 @@ public override void OnInspectorGUI()
140128
141129 int column = 0 ;
142130 string iconToRemove = null ;
131+ string iconToRename = null ;
143132 EditorGUILayout . BeginHorizontal ( ) ;
144- foreach ( IconEntry iconEntry in iconEntries )
133+ foreach ( KeyValuePair < uint , string > iconEntry in iconEntries )
145134 {
146135 if ( column >= MaxButtonsPerColumn )
147136 {
@@ -156,26 +145,31 @@ public override void OnInspectorGUI()
156145 GUILayout . Height ( ButtonDimension ) ,
157146 GUILayout . MaxWidth ( ButtonDimension ) ) )
158147 {
159- iconToRemove = iconEntry . Name ;
148+ iconToRemove = iconEntry . Value ;
160149 }
161150
162151 Rect textureRect = GUILayoutUtility . GetLastRect ( ) ;
163152 textureRect . width = GlyphDrawSize ;
164153 textureRect . height = GlyphDrawSize ;
165- EditorDrawTMPGlyph ( textureRect , iconEntry . UnicodeValue , fontAsset ) ;
154+ EditorDrawTMPGlyph ( textureRect , iconEntry . Key , fontAsset ) ;
166155
167- string currentName = EditorGUILayout . TextField ( iconEntry . Name ) ;
168- if ( currentName != iconEntry . Name )
156+ string currentName = EditorGUILayout . TextField ( iconEntry . Value ) ;
157+ if ( currentName != iconEntry . Value )
169158 {
170- UpdateIconName ( fontIconSet , iconEntry . Name , currentName ) ;
159+ iconToRename = currentName ;
160+ iconToRemove = iconEntry . Value ;
171161 }
172162 EditorGUILayout . EndVertical ( ) ;
173163
174164 column ++ ;
175165 }
176166 EditorGUILayout . EndHorizontal ( ) ;
177167
178- if ( iconToRemove != null )
168+ if ( iconToRename != null )
169+ {
170+ UpdateIconName ( fontIconSet , iconToRemove , iconToRename ) ;
171+ }
172+ else if ( iconToRemove != null )
179173 {
180174 RemoveIcon ( fontIconSet , iconToRemove ) ;
181175 }
@@ -248,51 +242,39 @@ private void DrawFontGlyphsGrid(TMP_FontAsset fontAsset, FontIconSet fontIconSet
248242
249243 private bool AddIcon ( FontIconSet fontIconSet , uint unicodeValue )
250244 {
251- string name = "Icon " + ( iconEntries . Count + 1 ) ;
245+ string name = $ "Icon { unicodeValue } " ;
252246 if ( fontIconSet . AddIcon ( name , unicodeValue ) )
253247 {
254- iconEntries . Add ( new IconEntry ( name , unicodeValue ) ) ;
248+ iconEntries . Add ( unicodeValue , name ) ;
255249 EditorUtility . SetDirty ( fontIconSet ) ;
250+ return true ;
256251 }
257- return true ;
252+ return false ;
258253 }
259254
260255 private bool RemoveIcon ( FontIconSet fontIconSet , string iconName )
261256 {
262- bool removed = fontIconSet . RemoveIcon ( iconName ) ;
263- if ( removed && FindIconIndexByName ( iconName , out int index ) )
257+ if ( fontIconSet . TryGetGlyphIcon ( iconName , out uint unicodeValue ) && fontIconSet . RemoveIcon ( iconName ) )
264258 {
265- iconEntries . RemoveAt ( index ) ;
259+ iconEntries . Remove ( unicodeValue ) ;
266260 EditorUtility . SetDirty ( fontIconSet ) ;
261+ return true ;
267262 }
268- return removed ;
269- }
270-
271- private bool FindIconIndexByName ( string iconName , out int outIndex )
272- {
273- for ( outIndex = 0 ; outIndex < iconEntries . Count ; outIndex ++ )
274- {
275- if ( iconEntries [ outIndex ] . Name == iconName )
276- {
277- return true ;
278- }
279- }
280- outIndex = - 1 ;
281263 return false ;
282264 }
283265
284266 private void UpdateIconName ( FontIconSet fontIconSet , string oldName , string newName )
285267 {
286- if ( fontIconSet . UpdateIconName ( oldName , newName ) && FindIconIndexByName ( oldName , out int index ) )
268+ if ( fontIconSet . UpdateIconName ( oldName , newName ) && fontIconSet . TryGetGlyphIcon ( newName , out uint unicodeValue ) )
287269 {
288- iconEntries [ index ] . Name = newName ;
270+ iconEntries [ unicodeValue ] = newName ;
289271 EditorUtility . SetDirty ( fontIconSet ) ;
290272 }
291273 }
292274
293275 private bool CheckIfHoloLensIconFontExists ( )
294276 {
295- foreach ( string guid in AssetDatabase . FindAssets ( $ "t:{ typeof ( UnityEngine . Font ) . Name } ") )
277+ foreach ( string guid in AssetDatabase . FindAssets ( $ "t:{ nameof ( Font ) } ") )
296278 {
297279 if ( AssetDatabase . GUIDToAssetPath ( guid ) . Contains ( MDL2IconFontName ) )
298280 {
0 commit comments