Skip to content

Commit

Permalink
Cleanup and improvements made while working on prefab/tag user utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
mtschoen-unity committed May 30, 2022
1 parent ba92239 commit aa736bf
Showing 1 changed file with 57 additions and 39 deletions.
96 changes: 57 additions & 39 deletions Editor/SolidColorTextures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ namespace Unity.Labs.SuperScience
/// </summary>
public class SolidColorTextures : EditorWindow
{
/// <summary>
/// Container for unique color rows.
/// </summary>
class ColorRow
{
public bool expanded;
public readonly List<Texture2D> textures = new List<Texture2D>();
}

/// <summary>
/// Tree structure for folder scan results.
/// This is the root object for the project scan, and represents the results in a hierarchy that matches the
Expand All @@ -31,23 +22,7 @@ class ColorRow
/// </summary>
class Folder
{
// TODO: Share code between this window and MissingProjectReferences
static class Styles
{
internal static readonly GUIStyle LineStyle = new GUIStyle
{
normal = new GUIStyleState
{
#if UNITY_2019_4_OR_NEWER
background = Texture2D.grayTexture
#else
background = Texture2D.whiteTexture
#endif
}
};
}

const string k_LabelFormat = "{0}: {1}";
// TODO: Share code between this window and others that display a folder structure
const int k_ShrunkTextureSize = 32;
const int k_IndentAmount = 15;
const int k_SeparatorLineHeight = 1;
Expand All @@ -62,7 +37,7 @@ static class Styles
/// <summary>
/// The number of solid color textures in this folder.
/// </summary>
public int Count;
public int Count { get; private set; }

/// <summary>
/// Clear the contents of this container.
Expand Down Expand Up @@ -126,7 +101,7 @@ public void Draw(string name)
var wasVisible = m_Visible;
using (new GUILayout.HorizontalScope())
{
m_Visible = EditorGUILayout.Foldout(m_Visible, string.Format(k_LabelFormat, name, Count), true);
m_Visible = EditorGUILayout.Foldout(m_Visible, $"{name}: {Count}", true);
if (GUILayout.Button(k_ShrinkAllGUIContent, k_ShrinkAllWidth))
ShrinkAndFinalize();
}
Expand Down Expand Up @@ -239,26 +214,52 @@ void SetVisibleRecursively(bool visible)
/// </summary>
public void SortContentsRecursively()
{
m_Textures.Sort((a, b) => a.Item2.name.CompareTo(b.Item2.name));
m_Textures.Sort((a, b) => a.Item1.CompareTo(b.Item1));
foreach (var kvp in m_Subfolders)
{
kvp.Value.SortContentsRecursively();
}
}
}

/// <summary>
/// Container for unique color rows.
/// </summary>
class ColorRow
{
public bool expanded;
public readonly List<Texture2D> textures = new List<Texture2D>();
}

static class Styles
{
internal static readonly GUIStyle LineStyle = new GUIStyle
{
normal = new GUIStyleState
{
#if UNITY_2019_4_OR_NEWER
background = Texture2D.grayTexture
#else
background = Texture2D.whiteTexture
#endif
}
};
}

const string k_MenuItemName = "Window/SuperScience/Solid Color Textures";
const string k_WindowTitle = "Solid Color Textures";
const string k_NoMissingReferences = "No solid color textures";
const string k_ProjectFolderName = "Project";
const int k_TextureColumnWidth = 150;
const int k_ColorPanelWidth = 150;
const string k_WindowTitle = "Solid Color Textures";
const string k_Instructions = "Click the Scan button to scan your project for solid color textures. WARNING: " +
"This will load every texture in your project. For large projects, this may take a long time and/or crash the Editor.";
const string k_ScanFilter = "t:Texture2D";
const int k_ProgressBarHeight = 15;
const int k_MaxScanUpdateTimeMilliseconds = 50;

static readonly GUIContent k_ScanGUIContent = new GUIContent("Scan", "Scan the project for solid color textures");
static readonly GUIContent k_CancelGUIContent = new GUIContent("Cancel", "Cancel the current scan");

static readonly GUILayoutOption k_ColorPanelWidthOption = GUILayout.Width(k_ColorPanelWidth);
static readonly GUILayoutOption k_ColorSwatchWidthOption = GUILayout.Width(30);
Expand All @@ -270,16 +271,16 @@ public void SortContentsRecursively()
Vector2 m_ColorListScrollPosition;
Vector2 m_FolderTreeScrollPosition;
readonly Folder m_ParentFolder = new Folder();
readonly Dictionary<int, ColorRow> m_TexturesByColor = new Dictionary<int, ColorRow>();
static readonly string[] k_ScanFolders = new[] {"Assets", "Packages"};
readonly SortedDictionary<int, ColorRow> m_TexturesByColor = new SortedDictionary<int, ColorRow>();
static readonly string[] k_ScanFolders = {"Assets", "Packages"};
int m_ScanCount;
int m_ScanProgress;
IEnumerator m_ScanEnumerator;

/// <summary>
/// Initialize the window
/// </summary>
[MenuItem("Window/SuperScience/Solid Color Textures")]
[MenuItem(k_MenuItemName)]
static void Init()
{
GetWindow<SolidColorTextures>(k_WindowTitle).Show();
Expand All @@ -292,14 +293,25 @@ void OnEnable()
m_ScanProgress = 0;
}

void OnDisable()
{
m_ScanEnumerator = null;
}

void OnGUI()
{
EditorGUIUtility.labelWidth = position.width - k_TextureColumnWidth - k_ColorPanelWidth;

var rect = GUILayoutUtility.GetRect(0, float.PositiveInfinity, k_ProgressBarHeight, k_ProgressBarHeight);
EditorGUI.ProgressBar(rect, (float)m_ScanProgress / m_ScanCount, $"{m_ScanProgress} / {m_ScanCount}");
if (GUILayout.Button(k_ScanGUIContent))
Scan();
if (m_ScanEnumerator == null)
{
if (GUILayout.Button(k_ScanGUIContent))
Scan();
}
else
{
if (GUILayout.Button(k_CancelGUIContent))
m_ScanEnumerator = null;
}

if (m_ParentFolder.Count == 0)
{
Expand Down Expand Up @@ -328,6 +340,12 @@ void OnGUI()
}
}
}

if (m_ScanCount > 0 && m_ScanCount - m_ScanProgress > 0)
{
var rect = GUILayoutUtility.GetRect(0, float.PositiveInfinity, k_ProgressBarHeight, k_ProgressBarHeight);
EditorGUI.ProgressBar(rect, (float) m_ScanProgress / m_ScanCount, $"{m_ScanProgress} / {m_ScanCount}");
}
}

/// <summary>
Expand Down Expand Up @@ -499,7 +517,7 @@ static bool IsSolidColorTexture(Texture2D texture, out int colorValue)
return false;
}

var pixels = texture.GetPixels32();
var pixels = texture.GetPixels();

// It is unlikely to get a null pixels array, but we should check just in case
if (pixels == null)
Expand All @@ -521,7 +539,7 @@ static bool IsSolidColorTexture(Texture2D texture, out int colorValue)
// Convert to int for faster comparison
colorValue = Color32ToInt.Convert(pixels[0]);
var isSolidColor = true;
for (var i = 0; i < pixelCount; i++)
for (var i = 1; i < pixelCount; i++)
{
var pixel = Color32ToInt.Convert(pixels[i]);
if (pixel != colorValue)
Expand Down

0 comments on commit aa736bf

Please sign in to comment.