Skip to content

Commit 962f6e0

Browse files
committed
Changed "Hide non-overridden prefab variables" options to "Hide redundant prefab references" which now also apply to C# scripts' and prefab assets' usages (#42)
1 parent 204de97 commit 962f6e0

7 files changed

Lines changed: 183 additions & 49 deletions

File tree

Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public class Parameters
5353
#endif
5454
public bool calculateUnusedObjects = false;
5555
public bool hideDuplicateRows = true;
56-
public bool hideNonOverriddenPrefabVariablesInAssets = true;
57-
public bool hideNonOverriddenPrefabVariablesInScenes = false;
56+
public bool hideRedundantPrefabReferencesInAssets = false;
57+
public bool hideRedundantPrefabReferencesInScenes = false;
5858
public bool noAssetDatabaseChanges = false;
5959
public bool showDetailedProgressBar = true;
6060
}
@@ -454,7 +454,7 @@ public SearchResult Run( Parameters searchParameters )
454454
}
455455

456456
// If a reference is found in the Project view, save the results
457-
if( currentSearchResultGroup.NumberOfReferences > 0 )
457+
if( currentSearchResultGroup.AnyReferencesFound )
458458
searchResult.Add( currentSearchResultGroup );
459459
}
460460

@@ -479,7 +479,7 @@ public SearchResult Run( Parameters searchParameters )
479479
}
480480
}
481481

482-
if( currentSearchResultGroup.NumberOfReferences > 0 )
482+
if( currentSearchResultGroup.AnyReferencesFound )
483483
searchResult.Add( currentSearchResultGroup );
484484
}
485485

@@ -554,7 +554,7 @@ public SearchResult Run( Parameters searchParameters )
554554
for( int i = 0; i < rootGameObjects.Length; i++ )
555555
SearchGameObjectRecursively( rootGameObjects[i] );
556556

557-
if( currentSearchResultGroup.NumberOfReferences > 0 )
557+
if( currentSearchResultGroup.AnyReferencesFound )
558558
searchResult.Add( currentSearchResultGroup );
559559
}
560560

@@ -663,7 +663,7 @@ private void InitializeSearchResultNodes( List<SearchResultGroup> searchResult )
663663
searchResult[i].InitializeNodes( objectsToSearchSet );
664664

665665
// Remove empty search result groups
666-
if( !searchResult[i].PendingSearch && searchResult[i].NumberOfReferences == 0 )
666+
if( !searchResult[i].PendingSearch && !searchResult[i].AnyReferencesFound )
667667
searchResult.RemoveAt( i-- );
668668
}
669669
}
@@ -982,11 +982,9 @@ private void SearchScene( string scenePath, List<SearchResultGroup> searchResult
982982
EditorSceneManager.CloseScene( scene, true );
983983
}
984984
}
985-
else
986-
{
987-
// Some references are found in this scene, save the results
985+
986+
if( currentSearchResultGroup.AnyReferencesFound )
988987
searchResult.Add( currentSearchResultGroup );
989-
}
990988
}
991989

992990
// Search a GameObject and its children for references recursively

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,21 @@ private ReferenceNode SearchGameObject( object obj )
397397
// Check if this GameObject's prefab is one of the selected assets
398398
if( searchPrefabConnections )
399399
{
400-
#if UNITY_2018_3_OR_NEWER
401-
Object prefab = go;
402-
while( prefab = PrefabUtility.GetCorrespondingObjectFromSource( prefab ) )
403-
#else
404-
Object prefab = PrefabUtility.GetPrefabParent( go );
405-
if( prefab )
406-
#endif
400+
bool? skipFurtherPrefabReferences = null;
401+
for( GameObject goPrefab = go.GetPrefabParent(), prefab = goPrefab; prefab != null; prefab = prefab.GetPrefabParent() )
407402
{
408-
if( objectsToSearchSet.Contains( prefab ) && assetsToSearchRootPrefabs.ContainsFast( prefab as GameObject ) )
403+
if( objectsToSearchSet.Contains( prefab ) && assetsToSearchRootPrefabs.ContainsFast( prefab ) )
409404
{
405+
if( goPrefab != prefab && !skipFurtherPrefabReferences.HasValue )
406+
{
407+
skipFurtherPrefabReferences = ShouldExcludeRedundantPrefabReferences( go ) && !go.HasAnyPrefabOverrides();
408+
if( skipFurtherPrefabReferences.Value )
409+
{
410+
currentSearchResultGroup.NumberOfRedundantReferences++;
411+
break;
412+
}
413+
}
414+
410415
referenceNode.AddLinkTo( GetReferenceNode( prefab ), "Prefab object" );
411416

412417
if( searchParameters.searchRefactoring != null )
@@ -440,10 +445,15 @@ private ReferenceNode SearchComponent( object obj )
440445
MonoScript script = MonoScript.FromMonoBehaviour( (MonoBehaviour) component );
441446
if( objectsToSearchSet.Contains( script ) )
442447
{
443-
referenceNode.AddLinkTo( GetReferenceNode( script ) );
448+
if( ShouldExcludeRedundantPrefabReferences( component ) && !component.HasAnyPrefabOverrides() )
449+
currentSearchResultGroup.NumberOfRedundantReferences++;
450+
else
451+
{
452+
referenceNode.AddLinkTo( GetReferenceNode( script ) );
444453

445-
if( searchParameters.searchRefactoring != null )
446-
searchParameters.searchRefactoring( new BehaviourUsageMatch( component.gameObject, script, component ) );
454+
if( searchParameters.searchRefactoring != null )
455+
searchParameters.searchRefactoring( new BehaviourUsageMatch( component.gameObject, script, component ) );
456+
}
447457
}
448458
}
449459

@@ -1452,9 +1462,7 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
14521462
if( iterator.Next( true ) )
14531463
{
14541464
bool iteratingVisible = iteratorVisible.NextVisible( true );
1455-
#if UNITY_2018_3_OR_NEWER
1456-
bool searchPrefabOverridesOnly = ( ( searchParameters.hideNonOverriddenPrefabVariablesInAssets && unityObject.IsAsset() ) || ( searchParameters.hideNonOverriddenPrefabVariablesInScenes && !unityObject.IsAsset() ) ) && PrefabUtility.GetCorrespondingObjectFromSource( unityObject ) != null;
1457-
#endif
1465+
bool searchPrefabOverridesOnly = ShouldExcludeRedundantPrefabReferences( unityObject );
14581466
bool enterChildren;
14591467
do
14601468
{
@@ -1471,10 +1479,6 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
14711479

14721480
if( !isVisible )
14731481
enterChildren = false;
1474-
#if UNITY_2018_3_OR_NEWER
1475-
else if( searchPrefabOverridesOnly && !iterator.prefabOverride )
1476-
enterChildren = false;
1477-
#endif
14781482
else
14791483
{
14801484
Object propertyValue;
@@ -1542,10 +1546,18 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
15421546
// m_RD.texture is a redundant reference that shows up when searching sprites
15431547
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
15441548
{
1545-
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
1549+
if( searchPrefabOverridesOnly && !iterator.prefabOverride )
1550+
{
1551+
currentSearchResultGroup.NumberOfRedundantReferences++;
1552+
enterChildren = false;
1553+
}
1554+
else
1555+
{
1556+
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
15461557

1547-
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
1548-
searchParameters.searchRefactoring( new SerializedPropertyMatch( unityObject, propertyValue, iterator ) );
1558+
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
1559+
searchParameters.searchRefactoring( new SerializedPropertyMatch( unityObject, propertyValue, iterator ) );
1560+
}
15491561
}
15501562
}
15511563
}
@@ -1924,6 +1936,11 @@ private Object GetAddressablesAssetReferenceValue( AssetReference assetReference
19241936
}
19251937
#endif
19261938

1939+
private bool ShouldExcludeRedundantPrefabReferences( Object obj )
1940+
{
1941+
return ( ( searchParameters.hideRedundantPrefabReferencesInAssets && obj.IsAsset() ) || ( searchParameters.hideRedundantPrefabReferencesInScenes && !obj.IsAsset() ) ) && obj.GetPrefabParent() != null;
1942+
}
1943+
19271944
// Iterates over all occurrences of specific key-value pairs in string
19281945
// Example1: #include "VALUE" valuePrefix=#include, valueWrapperChar="
19291946
// Example2: "guid": "VALUE" valuePrefix="guid", valueWrapperChar="

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,25 @@ private enum WindowFilter { AlwaysReturnActive, ReturnActiveIfNotLocked, AlwaysR
3737
#endif
3838
private const string PREFS_CALCULATE_UNUSED_OBJECTS = "AUD_FindUnusedObjs";
3939
private const string PREFS_HIDE_DUPLICATE_ROWS = "AUD_HideDuplicates";
40-
private const string PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_ASSETS = "AUD_HideRedundantPVariantLinks";
41-
private const string PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_SCENES = "AUD_HideRedundantPVarsInScenes";
40+
private const string PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_ASSETS = "AUD_HideRedundantPRefsInAssets";
41+
private const string PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_SCENES = "AUD_HideRedundantPRefsInScenes";
4242
private const string PREFS_SHOW_PROGRESS = "AUD_Progress";
4343

4444
private static readonly GUIContent windowTitle = new GUIContent( "Asset Usage Detector" );
4545
private static readonly Vector2 windowMinSize = new Vector2( 325f, 220f );
4646

4747
private static readonly GUILayoutOption GL_WIDTH_12 = GUILayout.Width( 12f );
4848

49+
private static readonly GUIContent sharedGUIContent = new GUIContent();
50+
51+
private readonly GUIContent hideRedundantPrefabReferencesInAssetsLabel = new GUIContent( "Hide redundant prefab references in Assets", "Hides redundant/non-overridden references in prefab variants and nested prefabs. " +
52+
"This will help focus on only the references that actually matter. For example:\n\n" +
53+
"- Material CloudMat is assigned to prefab Cloud and its variant CloudBig. Since changing Cloud's material will also affect CloudBig, search results won't show CloudBig's reference" );
54+
private readonly GUIContent hideRedundantPrefabReferencesInScenesLabel = new GUIContent( "Hide redundant prefab references in Scenes", "Hides redundant/non-overridden references in prefab instances. " +
55+
"This will help focus on only the references that actually matter. For example:\n\n" +
56+
"- Prefab Healthbar is nested inside prefab Player. An instance of Player exists in the current scene and its Healthbar isn't modified. Since modifying Healthbar in Player prefab will also affect " +
57+
"the instance in the scene, search results won't show the instance in the scene while searching for Healthbar's references" );
58+
4959
private GUIStyle lockButtonStyle;
5060

5161
private readonly AssetUsageDetector core = new AssetUsageDetector();
@@ -96,8 +106,8 @@ private bool IsLocked
96106
private bool searchUnusedMaterialProperties = true;
97107
private bool calculateUnusedObjects = false;
98108
private bool hideDuplicateRows = true;
99-
private bool hideNonOverriddenPrefabVariablesInAssets = true;
100-
private bool hideNonOverriddenPrefabVariablesInScenes = false;
109+
private bool hideRedundantPrefabReferencesInAssets = false;
110+
private bool hideRedundantPrefabReferencesInScenes = false;
101111
private bool noAssetDatabaseChanges = false;
102112
private bool showDetailedProgressBar = true;
103113

@@ -335,8 +345,8 @@ private void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsag
335345
#endif
336346
calculateUnusedObjects = searchParameters.calculateUnusedObjects;
337347
hideDuplicateRows = searchParameters.hideDuplicateRows;
338-
hideNonOverriddenPrefabVariablesInAssets = searchParameters.hideNonOverriddenPrefabVariablesInAssets;
339-
hideNonOverriddenPrefabVariablesInScenes = searchParameters.hideNonOverriddenPrefabVariablesInScenes;
348+
hideRedundantPrefabReferencesInAssets = searchParameters.hideRedundantPrefabReferencesInAssets;
349+
hideRedundantPrefabReferencesInScenes = searchParameters.hideRedundantPrefabReferencesInScenes;
340350
noAssetDatabaseChanges = searchParameters.noAssetDatabaseChanges;
341351
showDetailedProgressBar = searchParameters.showDetailedProgressBar;
342352

@@ -419,8 +429,8 @@ private void SavePrefs()
419429
#endif
420430
EditorPrefs.SetBool( PREFS_CALCULATE_UNUSED_OBJECTS, calculateUnusedObjects );
421431
EditorPrefs.SetBool( PREFS_HIDE_DUPLICATE_ROWS, hideDuplicateRows );
422-
EditorPrefs.SetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_ASSETS, hideNonOverriddenPrefabVariablesInAssets );
423-
EditorPrefs.SetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_SCENES, hideNonOverriddenPrefabVariablesInScenes );
432+
EditorPrefs.SetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_ASSETS, hideRedundantPrefabReferencesInAssets );
433+
EditorPrefs.SetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_SCENES, hideRedundantPrefabReferencesInScenes );
424434
EditorPrefs.SetBool( PREFS_SHOW_PROGRESS, showDetailedProgressBar );
425435
}
426436

@@ -442,8 +452,8 @@ private void LoadPrefs()
442452
#endif
443453
calculateUnusedObjects = EditorPrefs.GetBool( PREFS_CALCULATE_UNUSED_OBJECTS, false );
444454
hideDuplicateRows = EditorPrefs.GetBool( PREFS_HIDE_DUPLICATE_ROWS, true );
445-
hideNonOverriddenPrefabVariablesInAssets = EditorPrefs.GetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_ASSETS, true );
446-
hideNonOverriddenPrefabVariablesInScenes = EditorPrefs.GetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_SCENES, false );
455+
hideRedundantPrefabReferencesInAssets = EditorPrefs.GetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_ASSETS, hideRedundantPrefabReferencesInAssets );
456+
hideRedundantPrefabReferencesInScenes = EditorPrefs.GetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_SCENES, hideRedundantPrefabReferencesInScenes );
447457
showDetailedProgressBar = EditorPrefs.GetBool( PREFS_SHOW_PROGRESS, true );
448458
}
449459

@@ -571,10 +581,8 @@ private void OnGUI()
571581
#endif
572582
calculateUnusedObjects = WordWrappingToggleLeft( "Calculate unused objects", calculateUnusedObjects );
573583
hideDuplicateRows = WordWrappingToggleLeft( "Hide duplicate rows in search results", hideDuplicateRows );
574-
#if UNITY_2018_3_OR_NEWER
575-
hideNonOverriddenPrefabVariablesInAssets = WordWrappingToggleLeft( "Hide non-overridden prefab variables in assets (references found in non-overridden variables of prefab variants/nested prefabs will be hidden)", hideNonOverriddenPrefabVariablesInAssets );
576-
hideNonOverriddenPrefabVariablesInScenes = WordWrappingToggleLeft( "Hide non-overridden prefab variables in scenes (references found in non-overridden variables of prefab instances will be hidden)", hideNonOverriddenPrefabVariablesInScenes );
577-
#endif
584+
hideRedundantPrefabReferencesInAssets = WordWrappingToggleLeft( hideRedundantPrefabReferencesInAssetsLabel, hideRedundantPrefabReferencesInAssets );
585+
hideRedundantPrefabReferencesInScenes = WordWrappingToggleLeft( hideRedundantPrefabReferencesInScenesLabel, hideRedundantPrefabReferencesInScenes );
578586
noAssetDatabaseChanges = WordWrappingToggleLeft( "I haven't modified any assets/scenes since the last search (faster search)", noAssetDatabaseChanges );
579587
showDetailedProgressBar = WordWrappingToggleLeft( "Update search progress bar more often (cancelable search) (slower search)", showDetailedProgressBar );
580588

@@ -679,6 +687,13 @@ private void EndIndentedGUI( bool isVertical = true )
679687
}
680688

681689
public static bool WordWrappingToggleLeft( string label, bool value )
690+
{
691+
sharedGUIContent.text = label;
692+
sharedGUIContent.tooltip = null;
693+
return WordWrappingToggleLeft( sharedGUIContent, value );
694+
}
695+
696+
public static bool WordWrappingToggleLeft( GUIContent label, bool value )
682697
{
683698
GUILayout.BeginHorizontal();
684699
bool result = EditorGUILayout.ToggleLeft( GUIContent.none, value, GL_WIDTH_12 );
@@ -728,8 +743,8 @@ private void InitiateSearch()
728743
#endif
729744
calculateUnusedObjects = calculateUnusedObjects,
730745
hideDuplicateRows = hideDuplicateRows,
731-
hideNonOverriddenPrefabVariablesInAssets = hideNonOverriddenPrefabVariablesInAssets,
732-
hideNonOverriddenPrefabVariablesInScenes = hideNonOverriddenPrefabVariablesInScenes,
746+
hideRedundantPrefabReferencesInAssets = hideRedundantPrefabReferencesInAssets,
747+
hideRedundantPrefabReferencesInScenes = hideRedundantPrefabReferencesInScenes && searchInAssetsFolder,
733748
noAssetDatabaseChanges = noAssetDatabaseChanges,
734749
showDetailedProgressBar = showDetailedProgressBar
735750
} );

Plugins/AssetUsageDetector/Editor/SearchResult.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal class SerializableResultGroup
2323
public SearchResultGroup.GroupType type;
2424
public bool isExpanded;
2525
public bool pendingSearch;
26+
public int redundantReferences;
2627
public SearchResultTreeViewState treeViewState;
2728

2829
public List<int> initialSerializedNodes;
@@ -576,6 +577,9 @@ public enum GroupType { Assets = 0, Scene = 1, DontDestroyOnLoad = 2, ProjectSet
576577
public int NumberOfReferences { get { return references.Count; } }
577578
public ReferenceNode this[int index] { get { return references[index]; } }
578579

580+
public int NumberOfRedundantReferences { get; internal set; }
581+
public bool AnyReferencesFound { get { return NumberOfReferences > 0 || NumberOfRedundantReferences > 0; } }
582+
579583
public SearchResultGroup( string title, GroupType type, bool isExpanded = true, bool pendingSearch = false )
580584
{
581585
Title = title.StartsWith( "<b>" ) ? title : string.Concat( "<b>", title, "</b>" );
@@ -869,6 +873,9 @@ public float DrawOnGUI( SearchResult searchResult, EditorWindow window, float sc
869873

870874
if( IsExpanded )
871875
{
876+
if( NumberOfRedundantReferences > 0 )
877+
EditorGUILayout.HelpBox( string.Concat( NumberOfRedundantReferences.ToString(), " redundant prefab reference(s) were skipped. Disable \"Hide redundant prefab references in ", ( Type == GroupType.Scene || Type == GroupType.DontDestroyOnLoad ) ? "Scenes" : "Assets", "\" to see them." ), MessageType.Info );
878+
872879
if( PendingSearch )
873880
GUILayout.Box( "Lazy Search: this scene potentially has some references, hit Refresh to find them", Utilities.BoxGUIStyle );
874881
else if( references.Count == 0 )
@@ -1065,6 +1072,7 @@ internal SearchResult.SerializableResultGroup Serialize( Dictionary<ReferenceNod
10651072
type = Type,
10661073
isExpanded = IsExpanded,
10671074
pendingSearch = PendingSearch,
1075+
redundantReferences = NumberOfRedundantReferences,
10681076
treeViewState = treeViewState
10691077
};
10701078

@@ -1082,6 +1090,7 @@ internal SearchResult.SerializableResultGroup Serialize( Dictionary<ReferenceNod
10821090
// Deserialize this result group from the serialized data
10831091
internal void Deserialize( SearchResult.SerializableResultGroup serializedResultGroup, List<ReferenceNode> allNodes )
10841092
{
1093+
NumberOfRedundantReferences = serializedResultGroup.redundantReferences;
10851094
treeViewState = serializedResultGroup.treeViewState;
10861095

10871096
if( serializedResultGroup.initialSerializedNodes != null )

0 commit comments

Comments
 (0)