From 47c9807f2fd069c09a4f4e65bdc8054391dcca78 Mon Sep 17 00:00:00 2001 From: SolidAlloy Date: Sun, 21 Feb 2021 18:30:54 +0200 Subject: [PATCH] Started caching materials and moved texture-changing methods to TextureHelper This shaved off 10-20 more ms. --- Editor/Icons/HierarchyFolderIcon.cs | 49 ++-------------------- Editor/Icons/TextureHelper.cs | 65 ++++++++++++++++++++++++++++- 2 files changed, 67 insertions(+), 47 deletions(-) diff --git a/Editor/Icons/HierarchyFolderIcon.cs b/Editor/Icons/HierarchyFolderIcon.cs index 3f161bd..6286677 100644 --- a/Editor/Icons/HierarchyFolderIcon.cs +++ b/Editor/Icons/HierarchyFolderIcon.cs @@ -7,7 +7,6 @@ using UnityEditor; using UnityEditor.IMGUI.Controls; using UnityEngine; -using UnityEngine.Experimental.Rendering; using UnityHierarchyFolders.Runtime; using Object = UnityEngine.Object; @@ -74,46 +73,6 @@ private static void Startup() EditorApplication.hierarchyWindowItemOnGUI += RefreshFolderIcons; } - private static Texture2D GetTintedTexture(Texture2D original, Color tint, string name) - { - var material = new Material(Shader.Find("UI/Default")) { color = tint }; - return GetTextureWithMaterial(original, material, name); - } - - private static Texture2D GetWhiteTexture(Texture2D original, string name) - { - var material = new Material(Shader.Find("UI/Replace color")) { color = Color.white }; - return GetTextureWithMaterial(original, material, name); - } - - private static Texture2D GetTextureWithMaterial(Texture2D original, Material material, string name) - { - Texture2D newTexture; - - using (new TextureHelper.SRGBWriteScope(true)) - { - using (var temporary = new TextureHelper.TemporaryActiveTexture(original.width, original.height, 0)) - { - GL.Clear(false, true, new Color(1f, 1f, 1f, 0f)); - - Graphics.Blit(original, temporary, material); - - newTexture = new Texture2D(original.width, original.width, TextureFormat.ARGB32, false, true) - { - name = name, - filterMode = FilterMode.Bilinear, - hideFlags = HideFlags.DontSave - }; - - newTexture.ReadPixels(new Rect(0.0f, 0.0f, original.width, original.width), 0, 0); - newTexture.alphaIsTransparency = true; - newTexture.Apply(); - } - } - - return newTexture; - } - private static void InitIfNeeded() { if (_isInitialized) { return; } @@ -124,8 +83,8 @@ private static void InitIfNeeded() // We could use the actual white folder icons but I prefer the look of the tinted white folder icon // To use the actual white version: // texture = (Texture2D) EditorGUIUtility.IconContent($"{OpenedFolderPrefix | ClosedFolderPrefix} On Icon").image; - _openFolderSelectedTexture = GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White"); - _closedFolderSelectedTexture = GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White"); + _openFolderSelectedTexture = TextureHelper.GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White"); + _closedFolderSelectedTexture = TextureHelper.GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White"); _coloredFolderIcons = new (Texture2D, Texture2D)[] { (_openFolderTexture, _closedFolderTexture) }; @@ -136,9 +95,9 @@ private static void InitIfNeeded() int index = 1 + column + row * IconColumnCount; var color = IconColors[column, row]; - var openFolderIcon = GetTintedTexture(_openFolderSelectedTexture, + var openFolderIcon = TextureHelper.GetTintedTexture(_openFolderSelectedTexture, color, $"{_openFolderSelectedTexture.name} {index}"); - var closedFolderIcon = GetTintedTexture(_closedFolderSelectedTexture, + var closedFolderIcon = TextureHelper.GetTintedTexture(_closedFolderSelectedTexture, color, $"{_closedFolderSelectedTexture.name} {index}"); ArrayUtility.Add(ref _coloredFolderIcons, (openFolderIcon, closedFolderIcon)); diff --git a/Editor/Icons/TextureHelper.cs b/Editor/Icons/TextureHelper.cs index a38bbea..5ca3ad7 100644 --- a/Editor/Icons/TextureHelper.cs +++ b/Editor/Icons/TextureHelper.cs @@ -12,8 +12,69 @@ namespace UnityHierarchyFolders.Editor using UnityEngine; /// Helps to create new textures. - public static class TextureHelper + internal static class TextureHelper { + private static readonly Color _fullyTransparent = new Color(1f, 1f, 1f, 0f); + + private static Material _tintMaterial; + private static Material _colorReplaceMaterial; + + public static Texture2D GetTintedTexture(Texture2D original, Color tint, string name) + { + return GetTextureWithMaterial(original, GetTintMaterial(tint), name); + } + + public static Texture2D GetWhiteTexture(Texture2D original, string name) + { + return GetTextureWithMaterial(original, GetColorReplaceMaterial(Color.white), name); + } + + private static Material GetTintMaterial(Color tint) + { + if (_tintMaterial == null) + _tintMaterial = new Material(Shader.Find("UI/Default")); + + _tintMaterial.color = tint; + return _tintMaterial; + } + + private static Material GetColorReplaceMaterial(Color color) + { + if (_colorReplaceMaterial == null) + _colorReplaceMaterial = new Material(Shader.Find("UI/Replace color")); + + _colorReplaceMaterial.color = color; + return _colorReplaceMaterial; + } + + private static Texture2D GetTextureWithMaterial(Texture2D original, Material material, string name) + { + Texture2D newTexture; + + using (new SRGBWriteScope(true)) + { + using (var temporary = new TemporaryActiveTexture(original.width, original.height, 0)) + { + GL.Clear(false, true, _fullyTransparent); + + Graphics.Blit(original, temporary, material); + + newTexture = new Texture2D(original.width, original.width, TextureFormat.ARGB32, false, true) + { + name = name, + filterMode = FilterMode.Bilinear, + hideFlags = HideFlags.DontSave + }; + + newTexture.ReadPixels(new Rect(0f, 0f, original.width, original.width), 0, 0); + newTexture.alphaIsTransparency = true; + newTexture.Apply(); + } + } + + return newTexture; + } + /// /// Temporarily sets to the passed value, then returns it back. /// @@ -22,7 +83,7 @@ public static class TextureHelper { private readonly bool _previousValue; - /// Temporarily sets to , then executes the action. + /// Temporarily sets to true, then executes the action. /// Temporary value of . /// /// using (new SRGBWriteScope(true))