Skip to content

Layer/Tag Users #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Editor/.plan
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Refactor "Layer Users" to "Layer Usages"
Upgrade Prefab Layer Users to display GameObjects the same way Scene Layer Users does
Update scene scanning to be async in MissingReferences and Layer Usages
Refactor scanning options to be display options so we don't require a re-scan
Rename MissingReferenceWindow base class to AnalysisWindow and refactor to share code Prefab/Tag usages and Solid Color Textures
Split out Folder, GameObjectContainer, etc. types into their own files, share among all analysis windows
Create options to allow Layer Usages windows to skip scanning for layer masks (SerializedProperty access is the bottleneck)
Allow filtering Layer Usages with multiple layers
Use rich text for GameObject labels (bold tag or layer, etc.)
91 changes: 84 additions & 7 deletions Editor/GlobalNamespaceWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class GlobalNamespaceWatcher : EditorWindow
/// </summary>
class AssemblyRow
{
static readonly GUIContent k_OpenGUIContent = new GUIContent("Open", "Open this script in the default script editor.");
static readonly GUIContent k_OpenAllGUIContent = new GUIContent("Open All", "Open all scripts in this assembly's " +
"global namespace in the default script editor.\nWARNING: This process can lock the Editor for a long time and cannot be canceled.");
static readonly GUILayoutOption k_OpenButtonWidth = GUILayout.Width(100);

/// <summary>
/// The path to the assembly, sourced from Assembly.Location.
/// </summary>
Expand All @@ -46,6 +51,11 @@ class AssemblyRow
/// </summary>
public int MonoScriptTypeCount => m_MonoScriptCount;

/// <summary>
/// The types in this assembly.
/// </summary>
public SortedList<string, MonoScript> Types => m_Types;

/// <summary>
/// Draw this assembly row to the GUI.
/// </summary>
Expand All @@ -54,7 +64,25 @@ class AssemblyRow
public void Draw(string assemblyName, bool showOnlyMonoScriptTypes = false)
{
var count = showOnlyMonoScriptTypes ? m_MonoScriptCount : m_Types.Count;
m_Expanded = EditorGUILayout.Foldout(m_Expanded, $"{assemblyName}: ({count})", true);
using (new EditorGUILayout.HorizontalScope())
{
m_Expanded = EditorGUILayout.Foldout(m_Expanded, $"{assemblyName}: ({count})", true);
using (new EditorGUI.DisabledScope(m_MonoScriptCount == 0))
{
if (GUILayout.Button(k_OpenAllGUIContent, k_OpenButtonWidth))
{
foreach (var kvp in m_Types)
{
var monoScript = kvp.Value;
if (monoScript == null)
continue;

AssetDatabase.OpenAsset(monoScript);
}
}
}
}

if (m_Expanded)
{
using (new EditorGUI.IndentLevelScope())
Expand All @@ -64,18 +92,33 @@ public void Draw(string assemblyName, bool showOnlyMonoScriptTypes = false)
{
foreach (var kvp in m_Types)
{
var label = kvp.Key;
var monoScript = kvp.Value;
if (showOnlyMonoScriptTypes && monoScript == null)
continue;

EditorGUILayout.LabelField(kvp.Key);
EditorGUILayout.ObjectField(monoScript, typeof(MonoScript), false);
DrawScript(showOnlyMonoScriptTypes, monoScript, label);
}
}
}
}
}

static void DrawScript(bool showOnlyMonoScriptTypes, MonoScript monoScript, string label)
{
if (showOnlyMonoScriptTypes && monoScript == null)
return;

using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField(label);
using (new EditorGUI.DisabledScope(monoScript == null))
{
if (GUILayout.Button(k_OpenGUIContent, k_OpenButtonWidth))
AssetDatabase.OpenAsset(monoScript);
}
}

EditorGUILayout.ObjectField(monoScript, typeof(MonoScript), false);
}

/// <summary>
/// Add a type to this assembly row.
/// The type will be stored in a dictionary, and if there is a MonoScript, we increment a counter to show if only MonoScript types will be shown.
Expand All @@ -84,10 +127,16 @@ public void Draw(string assemblyName, bool showOnlyMonoScriptTypes = false)
/// <param name="monoScript">An associated MonoScript, if one exists.</param>
public void AddType(string typeName, MonoScript monoScript)
{
var label = typeName;
if (monoScript != null)
{
m_MonoScriptCount++;
var path = AssetDatabase.GetAssetPath(monoScript);
if (!string.IsNullOrEmpty(path))
label = path;
}

m_Types.Add(typeName, monoScript);
m_Types.Add(label, monoScript);
}
}

Expand All @@ -99,7 +148,11 @@ public void AddType(string typeName, MonoScript monoScript)
const int k_LabelWidth = 200;
const string k_CongratulationsLabel = "Congratulations! There are no types in the global namespace. :)";

static readonly GUIContent k_OpenEverythingGUIContent = new GUIContent("Open Everything", "Open all scripts in the " +
"global namespace in the default script editor.\nWARNING: This process can lock the Editor for a long time and cannot be canceled.");

static SortedList<string, AssemblyRow> s_Assemblies;
static int s_TotalMonoScriptCount;

[SerializeField]
Vector2 m_ScrollPosition;
Expand All @@ -120,6 +173,9 @@ void OnEnable()
{
if (s_Assemblies == null)
{
// Reset total count, just in case it's gone out of sync with s_Assemblies.
s_TotalMonoScriptCount = 0;

// Prepare a map of MonoScript types for fast access.
var monoScripts = MonoImporter.GetAllRuntimeMonoScripts();
var monoScriptDictionary = new Dictionary<string, MonoScript>(monoScripts.Length);
Expand Down Expand Up @@ -190,7 +246,10 @@ void OnEnable()
}

if (addedType)
{
s_Assemblies.Add(assemblyName, row);
s_TotalMonoScriptCount += row.MonoScriptTypeCount;
}
}
catch
{
Expand All @@ -209,6 +268,24 @@ void OnGUI()
m_ShowOnlyProjectAssemblies = EditorGUILayout.Toggle(k_ShowOnlyProjectAssembliesLabel, m_ShowOnlyProjectAssemblies);
m_ShowOnlyMonoScriptTypes = EditorGUILayout.Toggle(k_ShowOnlyMonoScriptTypesLabel, m_ShowOnlyMonoScriptTypes);

using (new EditorGUI.DisabledScope(s_TotalMonoScriptCount == 0))
{
if (GUILayout.Button(k_OpenEverythingGUIContent))
{
foreach (var kvp in s_Assemblies)
{
foreach (var kvp2 in kvp.Value.Types)
{
var monoScript = kvp2.Value;
if (monoScript != null)
continue;

AssetDatabase.OpenAsset(monoScript);
}
}
}
}

// Give users convenient buttons to expand/collapse the assembly rows
using (new EditorGUILayout.HorizontalScope())
{
Expand Down
8 changes: 8 additions & 0 deletions Editor/LayerUsers.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading