Skip to content

Commit 47c9807

Browse files
SolidAlloyxsduan
authored andcommitted
Started caching materials and moved texture-changing methods to TextureHelper
This shaved off 10-20 more ms.
1 parent 3f96964 commit 47c9807

File tree

2 files changed

+67
-47
lines changed

2 files changed

+67
-47
lines changed

Editor/Icons/HierarchyFolderIcon.cs

Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using UnityEditor;
88
using UnityEditor.IMGUI.Controls;
99
using UnityEngine;
10-
using UnityEngine.Experimental.Rendering;
1110
using UnityHierarchyFolders.Runtime;
1211
using Object = UnityEngine.Object;
1312

@@ -74,46 +73,6 @@ private static void Startup()
7473
EditorApplication.hierarchyWindowItemOnGUI += RefreshFolderIcons;
7574
}
7675

77-
private static Texture2D GetTintedTexture(Texture2D original, Color tint, string name)
78-
{
79-
var material = new Material(Shader.Find("UI/Default")) { color = tint };
80-
return GetTextureWithMaterial(original, material, name);
81-
}
82-
83-
private static Texture2D GetWhiteTexture(Texture2D original, string name)
84-
{
85-
var material = new Material(Shader.Find("UI/Replace color")) { color = Color.white };
86-
return GetTextureWithMaterial(original, material, name);
87-
}
88-
89-
private static Texture2D GetTextureWithMaterial(Texture2D original, Material material, string name)
90-
{
91-
Texture2D newTexture;
92-
93-
using (new TextureHelper.SRGBWriteScope(true))
94-
{
95-
using (var temporary = new TextureHelper.TemporaryActiveTexture(original.width, original.height, 0))
96-
{
97-
GL.Clear(false, true, new Color(1f, 1f, 1f, 0f));
98-
99-
Graphics.Blit(original, temporary, material);
100-
101-
newTexture = new Texture2D(original.width, original.width, TextureFormat.ARGB32, false, true)
102-
{
103-
name = name,
104-
filterMode = FilterMode.Bilinear,
105-
hideFlags = HideFlags.DontSave
106-
};
107-
108-
newTexture.ReadPixels(new Rect(0.0f, 0.0f, original.width, original.width), 0, 0);
109-
newTexture.alphaIsTransparency = true;
110-
newTexture.Apply();
111-
}
112-
}
113-
114-
return newTexture;
115-
}
116-
11776
private static void InitIfNeeded()
11877
{
11978
if (_isInitialized) { return; }
@@ -124,8 +83,8 @@ private static void InitIfNeeded()
12483
// We could use the actual white folder icons but I prefer the look of the tinted white folder icon
12584
// To use the actual white version:
12685
// texture = (Texture2D) EditorGUIUtility.IconContent($"{OpenedFolderPrefix | ClosedFolderPrefix} On Icon").image;
127-
_openFolderSelectedTexture = GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White");
128-
_closedFolderSelectedTexture = GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White");
86+
_openFolderSelectedTexture = TextureHelper.GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White");
87+
_closedFolderSelectedTexture = TextureHelper.GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White");
12988

13089
_coloredFolderIcons = new (Texture2D, Texture2D)[] { (_openFolderTexture, _closedFolderTexture) };
13190

@@ -136,9 +95,9 @@ private static void InitIfNeeded()
13695
int index = 1 + column + row * IconColumnCount;
13796
var color = IconColors[column, row];
13897

139-
var openFolderIcon = GetTintedTexture(_openFolderSelectedTexture,
98+
var openFolderIcon = TextureHelper.GetTintedTexture(_openFolderSelectedTexture,
14099
color, $"{_openFolderSelectedTexture.name} {index}");
141-
var closedFolderIcon = GetTintedTexture(_closedFolderSelectedTexture,
100+
var closedFolderIcon = TextureHelper.GetTintedTexture(_closedFolderSelectedTexture,
142101
color, $"{_closedFolderSelectedTexture.name} {index}");
143102

144103
ArrayUtility.Add(ref _coloredFolderIcons, (openFolderIcon, closedFolderIcon));

Editor/Icons/TextureHelper.cs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,69 @@ namespace UnityHierarchyFolders.Editor
1212
using UnityEngine;
1313

1414
/// <summary>Helps to create new textures.</summary>
15-
public static class TextureHelper
15+
internal static class TextureHelper
1616
{
17+
private static readonly Color _fullyTransparent = new Color(1f, 1f, 1f, 0f);
18+
19+
private static Material _tintMaterial;
20+
private static Material _colorReplaceMaterial;
21+
22+
public static Texture2D GetTintedTexture(Texture2D original, Color tint, string name)
23+
{
24+
return GetTextureWithMaterial(original, GetTintMaterial(tint), name);
25+
}
26+
27+
public static Texture2D GetWhiteTexture(Texture2D original, string name)
28+
{
29+
return GetTextureWithMaterial(original, GetColorReplaceMaterial(Color.white), name);
30+
}
31+
32+
private static Material GetTintMaterial(Color tint)
33+
{
34+
if (_tintMaterial == null)
35+
_tintMaterial = new Material(Shader.Find("UI/Default"));
36+
37+
_tintMaterial.color = tint;
38+
return _tintMaterial;
39+
}
40+
41+
private static Material GetColorReplaceMaterial(Color color)
42+
{
43+
if (_colorReplaceMaterial == null)
44+
_colorReplaceMaterial = new Material(Shader.Find("UI/Replace color"));
45+
46+
_colorReplaceMaterial.color = color;
47+
return _colorReplaceMaterial;
48+
}
49+
50+
private static Texture2D GetTextureWithMaterial(Texture2D original, Material material, string name)
51+
{
52+
Texture2D newTexture;
53+
54+
using (new SRGBWriteScope(true))
55+
{
56+
using (var temporary = new TemporaryActiveTexture(original.width, original.height, 0))
57+
{
58+
GL.Clear(false, true, _fullyTransparent);
59+
60+
Graphics.Blit(original, temporary, material);
61+
62+
newTexture = new Texture2D(original.width, original.width, TextureFormat.ARGB32, false, true)
63+
{
64+
name = name,
65+
filterMode = FilterMode.Bilinear,
66+
hideFlags = HideFlags.DontSave
67+
};
68+
69+
newTexture.ReadPixels(new Rect(0f, 0f, original.width, original.width), 0, 0);
70+
newTexture.alphaIsTransparency = true;
71+
newTexture.Apply();
72+
}
73+
}
74+
75+
return newTexture;
76+
}
77+
1778
/// <summary>
1879
/// Temporarily sets <see cref="GL.sRGBWrite"/> to the passed value, then returns it back.
1980
/// </summary>
@@ -22,7 +83,7 @@ public static class TextureHelper
2283
{
2384
private readonly bool _previousValue;
2485

25-
/// <summary>Temporarily sets <see cref="GL.sRGBWrite"/> to <paramref name=""/>, then executes the action.</summary>
86+
/// <summary>Temporarily sets <see cref="GL.sRGBWrite"/> to <c>true</c>, then executes the action.</summary>
2687
/// <param name="enableWrite"> Temporary value of <see cref="GL.sRGBWrite"/>. </param>
2788
/// <example><code>
2889
/// using (new SRGBWriteScope(true))

0 commit comments

Comments
 (0)