Skip to content

Commit fadd6a7

Browse files
committed
fix: Fixed the issue with editor icons disappearing randomly
1 parent d5e30ca commit fadd6a7

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

Editor/Helpers/EditorIcons.cs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,34 @@ public static class EditorIcons
2626
public static readonly Texture2D Error = (Texture2D) EditorGUIUtility.Load("console.erroricon");
2727

2828
/// <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;
3031

3132
/// <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;
3348

34-
public static EditorIcon AddButtonS { get; private set; }
49+
public static EditorIcon AddButtonS => GetEditorIcon(ref _addButtonS, EditorIconsDatabase.ToolbarPlusS);
50+
private static EditorIcon _addButtonS;
3551

36-
public static EditorIcon AddButtonI { get; private set; }
52+
public static EditorIcon AddButtonI => GetEditorIcon(ref _addButtonI, EditorIconsDatabase.ToolbarPlusI);
53+
private static EditorIcon _addButtonI;
3754

3855
static EditorIcons()
3956
{
40-
CreateEditorIcons();
41-
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
4257
AssemblyReloadEvents.beforeAssemblyReload += DisposeOfEditorIcons;
4358
}
4459

@@ -51,27 +66,23 @@ private static EditorIconsDatabase GetDatabase()
5166
return database;
5267
}
5368

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-
6369
private static void DisposeOfEditorIcons()
6470
{
65-
TriangleRight.Dispose();
66-
TriangleDown.Dispose();
67-
AddButtonS.Dispose();
68-
AddButtonI.Dispose();
71+
_triangleRight.Dispose();
72+
_triangleDown.Dispose();
73+
_addButtonS.Dispose();
74+
_addButtonI.Dispose();
6975
}
7076

71-
private static void OnPlayModeStateChanged(PlayModeStateChange stateChange)
77+
private static EditorIcon GetEditorIcon(ref EditorIcon editorIcon, Texture2D originalIcon)
7278
{
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;
7586
}
7687
}
7788
}

0 commit comments

Comments
 (0)