@@ -26,19 +26,34 @@ public static class EditorIcons
26
26
public static readonly Texture2D Error = ( Texture2D ) EditorGUIUtility . Load ( "console.erroricon" ) ;
27
27
28
28
/// <summary>Triangle with one of the vertices looking to the right. Useful in foldout menus.</summary>
29
- public static EditorIcon TriangleRight { get ; private set ; }
29
+ public static EditorIcon TriangleRight => GetEditorIcon ( ref _triangleRight , EditorIconsDatabase . TriangleRight ) ;
30
+ private static EditorIcon _triangleRight ;
30
31
31
32
/// <summary>Triangle with one of the vertices looking to the bottom. Useful in foldout menus.</summary>
32
- public static EditorIcon TriangleDown { get ; private set ; }
33
+ public static EditorIcon TriangleDown
34
+ {
35
+ get
36
+ {
37
+ // not using GetEditorIcon here because it would rotate the texture each time the parameter is passed into the method.
38
+ if ( _triangleDown . Default == null )
39
+ {
40
+ _triangleDown . Dispose ( ) ;
41
+ _triangleDown = new EditorIcon ( EditorIconsDatabase . TriangleRight . Rotate ( ) ) ;
42
+ }
43
+
44
+ return _triangleDown ;
45
+ }
46
+ }
47
+ private static EditorIcon _triangleDown ;
33
48
34
- public static EditorIcon AddButtonS { get ; private set ; }
49
+ public static EditorIcon AddButtonS => GetEditorIcon ( ref _addButtonS , EditorIconsDatabase . ToolbarPlusS ) ;
50
+ private static EditorIcon _addButtonS ;
35
51
36
- public static EditorIcon AddButtonI { get ; private set ; }
52
+ public static EditorIcon AddButtonI => GetEditorIcon ( ref _addButtonI , EditorIconsDatabase . ToolbarPlusI ) ;
53
+ private static EditorIcon _addButtonI ;
37
54
38
55
static EditorIcons ( )
39
56
{
40
- CreateEditorIcons ( ) ;
41
- EditorApplication . playModeStateChanged += OnPlayModeStateChanged ;
42
57
AssemblyReloadEvents . beforeAssemblyReload += DisposeOfEditorIcons ;
43
58
}
44
59
@@ -51,27 +66,23 @@ private static EditorIconsDatabase GetDatabase()
51
66
return database ;
52
67
}
53
68
54
- private static void CreateEditorIcons ( )
55
- {
56
- DisposeOfEditorIcons ( ) ; // dispose of previous editor icons on play mode exit just in case.
57
- TriangleRight = new EditorIcon ( EditorIconsDatabase . TriangleRight ) ;
58
- TriangleDown = new EditorIcon ( EditorIconsDatabase . TriangleRight . Rotate ( ) ) ;
59
- AddButtonS = new EditorIcon ( EditorIconsDatabase . ToolbarPlusS ) ;
60
- AddButtonI = new EditorIcon ( EditorIconsDatabase . ToolbarPlusI ) ;
61
- }
62
-
63
69
private static void DisposeOfEditorIcons ( )
64
70
{
65
- TriangleRight . Dispose ( ) ;
66
- TriangleDown . Dispose ( ) ;
67
- AddButtonS . Dispose ( ) ;
68
- AddButtonI . Dispose ( ) ;
71
+ _triangleRight . Dispose ( ) ;
72
+ _triangleDown . Dispose ( ) ;
73
+ _addButtonS . Dispose ( ) ;
74
+ _addButtonI . Dispose ( ) ;
69
75
}
70
76
71
- private static void OnPlayModeStateChanged ( PlayModeStateChange stateChange )
77
+ private static EditorIcon GetEditorIcon ( ref EditorIcon editorIcon , Texture2D originalIcon )
72
78
{
73
- if ( stateChange == PlayModeStateChange . EnteredEditMode )
74
- CreateEditorIcons ( ) ;
79
+ if ( editorIcon . Default == null )
80
+ {
81
+ editorIcon . Dispose ( ) ;
82
+ editorIcon = new EditorIcon ( originalIcon ) ;
83
+ }
84
+
85
+ return editorIcon ;
75
86
}
76
87
}
77
88
}
0 commit comments