Skip to content

Commit

Permalink
- Added 2 options to the "Search for References" button in Hierarchy …
Browse files Browse the repository at this point in the history
…objects' context (right-click) menu: "This Object Only" and "Include Children"

- In search results, "arrayVariable.Array.data[0]" is now displayed as "arrayVariable[0]"
  • Loading branch information
yasirkula committed Aug 8, 2020
1 parent 68f1b28 commit 003cedd
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode )

// m_RD.texture is a redundant reference that shows up when searching sprites
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath );
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
}
} while( iterator.Next( enterChildren ) );

Expand Down
36 changes: 27 additions & 9 deletions Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,13 @@ private static void OpenNewWindow()
}

// Quickly initiate search for the selected assets
[MenuItem( "GameObject/Search for References", priority = 49 )]
[MenuItem( "GameObject/Search for References/This Object Only", priority = 49 )]
[MenuItem( "Assets/Search for References", priority = 1000 )]
private static void SearchSelectedAssetReferences( MenuCommand command )
{
// This happens when this button is clicked via hierarchy's right click context menu
// and is called once for each object in the selection. We don't want that, we want
// the function to be called only once so that there aren't multiple empty parents
// generated in one call
// the function to be called only once
if( command.context )
{
EditorApplication.update -= CallSearchSelectedAssetReferencesOnce;
Expand All @@ -141,30 +140,43 @@ private static void SearchSelectedAssetReferences( MenuCommand command )
ShowAndSearch( Selection.objects );
}

[MenuItem( "GameObject/Search for References/Include Children", priority = 49 )]
private static void SearchSelectedAssetReferencesWithChildren( MenuCommand command )
{
if( command.context )
{
EditorApplication.update -= CallSearchSelectedAssetReferencesWithChildrenOnce;
EditorApplication.update += CallSearchSelectedAssetReferencesWithChildrenOnce;
}
else
ShowAndSearch( Selection.objects, true );
}

// Show the menu item only if there is a selection in the Editor
[MenuItem( "GameObject/Search for References", validate = true )]
[MenuItem( "GameObject/Search for References (Include Children)", validate = true )]
[MenuItem( "Assets/Search for References", validate = true )]
private static bool SearchSelectedAssetReferencesValidate( MenuCommand command )
{
return Selection.objects.Length > 0;
}

// Quickly show the AssetUsageDetector window and initiate a search
public static void ShowAndSearch( IEnumerable<Object> searchObjects )
public static void ShowAndSearch( IEnumerable<Object> searchObjects, bool? shouldSearchChildren = null )
{
ShowAndSearchInternal( searchObjects, null );
ShowAndSearchInternal( searchObjects, null, shouldSearchChildren );
}

// Quickly show the AssetUsageDetector window and initiate a search
public static void ShowAndSearch( AssetUsageDetector.Parameters searchParameters )
public static void ShowAndSearch( AssetUsageDetector.Parameters searchParameters, bool? shouldSearchChildren = null )
{
if( searchParameters == null )
{
Debug.LogError( "searchParameters can't be null!" );
return;
}

ShowAndSearchInternal( searchParameters.objectsToSearch, searchParameters );
ShowAndSearchInternal( searchParameters.objectsToSearch, searchParameters, shouldSearchChildren );
}

private static void CallSearchSelectedAssetReferencesOnce()
Expand All @@ -173,7 +185,13 @@ private static void CallSearchSelectedAssetReferencesOnce()
SearchSelectedAssetReferences( new MenuCommand( null ) );
}

private static void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsageDetector.Parameters searchParameters )
private static void CallSearchSelectedAssetReferencesWithChildrenOnce()
{
EditorApplication.update -= CallSearchSelectedAssetReferencesWithChildrenOnce;
SearchSelectedAssetReferencesWithChildren( new MenuCommand( null ) );
}

private static void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsageDetector.Parameters searchParameters, bool? shouldSearchChildren )
{
if( mainWindow != null && !mainWindow.ReturnToSetupPhase( true ) )
{
Expand All @@ -187,7 +205,7 @@ private static void ShowAndSearchInternal( IEnumerable<Object> searchObjects, As
if( searchObjects != null )
{
foreach( Object obj in searchObjects )
mainWindow.objectsToSearch.Add( new ObjectToSearch( obj ) );
mainWindow.objectsToSearch.Add( new ObjectToSearch( obj, shouldSearchChildren ) );
}

if( searchParameters != null )
Expand Down
20 changes: 10 additions & 10 deletions Plugins/AssetUsageDetector/Editor/ObjectToSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public SubAsset( Object subAsset, bool shouldSearch )
private static MonoScript[] monoScriptsInProject;
private static HashSet<Object> currentSubAssets;

public ObjectToSearch( Object obj )
public ObjectToSearch( Object obj, bool? shouldSearchChildren = null )
{
this.obj = obj;
RefreshSubAssets();
RefreshSubAssets( shouldSearchChildren );
}

public void RefreshSubAssets()
public void RefreshSubAssets( bool? shouldSearchChildren = null )
{
if( subAssets == null )
subAssets = new List<SubAsset>();
Expand All @@ -47,11 +47,11 @@ public void RefreshSubAssets()
else
currentSubAssets.Clear();

AddSubAssets( obj, false );
AddSubAssets( obj, false, shouldSearchChildren );
currentSubAssets.Clear();
}

private void AddSubAssets( Object target, bool includeTarget )
private void AddSubAssets( Object target, bool includeTarget, bool? shouldSearchChildren )
{
if( target == null || target.Equals( null ) )
return;
Expand All @@ -71,7 +71,7 @@ private void AddSubAssets( Object target, bool includeTarget )
if( ReferenceEquals( children[i], goTransform ) )
continue;

subAssets.Add( new SubAsset( children[i].gameObject, false ) );
subAssets.Add( new SubAsset( children[i].gameObject, shouldSearchChildren ?? false ) );
}
}
else
Expand All @@ -83,7 +83,7 @@ private void AddSubAssets( Object target, bool includeTarget )
{
if( !currentSubAssets.Contains( target ) )
{
subAssets.Add( new SubAsset( target, true ) );
subAssets.Add( new SubAsset( target, shouldSearchChildren ?? true ) );
currentSubAssets.Add( target );
}
}
Expand All @@ -93,7 +93,7 @@ private void AddSubAssets( Object target, bool includeTarget )
if( target.IsFolder() )
{
foreach( string filePath in Utilities.EnumerateFolderContents( target ) )
AddSubAssets( AssetDatabase.LoadAssetAtPath<Object>( filePath ), true );
AddSubAssets( AssetDatabase.LoadAssetAtPath<Object>( filePath ), true, shouldSearchChildren );

return;
}
Expand All @@ -112,7 +112,7 @@ private void AddSubAssets( Object target, bool includeTarget )

if( asset != target )
{
subAssets.Add( new SubAsset( asset, true ) );
subAssets.Add( new SubAsset( asset, shouldSearchChildren ?? true ) );
currentSubAssets.Add( asset );
}

Expand Down Expand Up @@ -142,7 +142,7 @@ private void AddSubAssets( Object target, bool includeTarget )

if( !currentSubAssets.Contains( monoScriptsInProject[j] ) )
{
subAssets.Add( new SubAsset( monoScriptsInProject[j], true ) );
subAssets.Add( new SubAsset( monoScriptsInProject[j], shouldSearchChildren ?? true ) );
currentSubAssets.Add( monoScriptsInProject[j] );
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.yasirkula.assetusagedetector",
"displayName": "Asset Usage Detector",
"version": "1.8.4",
"version": "1.8.5",
"description": "This editor extension helps you figure out at which places an asset or GameObject is used, i.e. lists the objects that refer to the asset. It is possible to search for references in the Assets folder (Project view) and/or in the scene(s) of your project. You can also search for references while in Play mode!"
}

0 comments on commit 003cedd

Please sign in to comment.