Skip to content

Commit 4b14628

Browse files
committed
(Experimental)(#14) Added SearchRefactoring parameter to AssetUsageDetector (only useful while triggering the search via Scripting API). It allows getting notified of found references during the search (some references like Assembly Definition File references or Shader Graph references aren't supported) and in most cases, refactoring them (e.g. changing all usages of a searched object with something else or null). Disabling "Lazy Scene Search" is recommended while using this feature and it's advised to backup your project beforehand
1 parent 8a6fe82 commit 4b14628

8 files changed

Lines changed: 690 additions & 47 deletions

File tree

.github/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ There are 5 ways to install this plugin:
3737

3838
![SearchResults2](Images/SearchResults2Dark.png)
3939

40+
## SEARCH REFACTORING
41+
42+
While searching for references using the Scripting API, it's possible to get notified of the found references *during* the search (some references like *Assembly Definition File* references or *Shader Graph* references aren't supported) and in most cases, refactor them (e.g. changing all usages of a searched object with something else or *null*). Disabling *Lazy Scene Search* is recommended while using this feature and it's advised to backup your project beforehand.
43+
44+
To initiate a search using the Scripting API, you need to put your script either in *Editor* folder or add `AssetUsageDetector.Editor` as reference to your *Assembly Definition File*. Then, you can either create a new instance of `AssetUsageDetectorNamespace.AssetUsageDetector` object and call its `Run` method, or call the `AssetUsageDetectorNamespace.AssetUsageDetectorWindow.ShowAndSearch` method. In either case, you'll be handling the search refactoring in the **searchRefactoring** callback:
45+
46+
```csharp
47+
void ReplaceFontUsages( Font from, Font to )
48+
{
49+
AssetUsageDetector assetUsageDetector = new AssetUsageDetector();
50+
assetUsageDetector.Run( new AssetUsageDetector.Parameters()
51+
{
52+
objectsToSearch = new Object[] { from },
53+
lazySceneSearch = false,
54+
searchRefactoring = ( searchMatch ) =>
55+
{
56+
Debug.LogFormat( "Found a {0} reference from {1} to {2}", searchMatch.GetType().Name, searchMatch.Source, searchMatch.Value );
57+
searchMatch.ChangeValue( to );
58+
}
59+
} );
60+
}
61+
```
62+
63+
**NOTE:** Refactored references won't be reflected to the returned search results, old references will continue to be displayed. After saving the changes (modified scenes) and initiating another search, correct search results will be shown.
64+
65+
**NOTE2:** After refactoring the references, consider performing a normal search to see if all references were correctly refactored. If some references weren't refactored (even though they could've been), feel free to report it.
66+
4067
## KNOWN LIMITATIONS
4168

4269
- *Addressables* aren't supported

Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class Parameters
4444

4545
public bool searchUnusedMaterialProperties = true;
4646

47+
public SearchRefactoring searchRefactoring = null;
48+
4749
public bool lazySceneSearch = true;
4850
public bool calculateUnusedObjects = false;
4951
public bool hideDuplicateRows = true;
@@ -53,6 +55,8 @@ public class Parameters
5355
}
5456
#endregion
5557

58+
private Parameters searchParameters;
59+
5660
// A set that contains the searched scene object(s), asset(s) and their sub-assets (if any)
5761
private readonly HashSet<Object> objectsToSearchSet = new HashSet<Object>();
5862
// Scenes of scene object(s) in objectsToSearchSet
@@ -78,14 +82,9 @@ public class Parameters
7882
// Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function)
7983
private readonly List<object> callStack = new List<object>( 64 );
8084

81-
private int searchDepthLimit; // Depth limit for recursively searching variables of objects
82-
8385
private Object currentSearchedObject;
8486
private int currentDepth;
8587

86-
private bool searchUnusedMaterialProperties;
87-
88-
private bool dontSearchInSourceAssets;
8988
private bool searchingSourceAssets;
9089
private bool isInPlayMode;
9190

@@ -166,6 +165,8 @@ public SearchResult Run( Parameters searchParameters )
166165

167166
try
168167
{
168+
this.searchParameters = searchParameters;
169+
169170
// Initialize commonly used variables
170171
searchResult = new List<SearchResultGroup>(); // Overall search results
171172

@@ -174,10 +175,6 @@ public SearchResult Run( Parameters searchParameters )
174175
searchedObjectsCount = 0;
175176
searchStartTime = EditorApplication.timeSinceStartup;
176177

177-
searchDepthLimit = searchParameters.searchDepthLimit;
178-
dontSearchInSourceAssets = searchParameters.dontSearchInSourceAssets;
179-
searchUnusedMaterialProperties = searchParameters.searchUnusedMaterialProperties;
180-
181178
searchedObjects.Clear();
182179
searchedUnityObjects.Clear();
183180
animationClipUniqueBindings.Clear();
@@ -545,7 +542,7 @@ public SearchResult Run( Parameters searchParameters )
545542
}
546543

547544
// Searching source assets last prevents some references from being excluded due to callStack.ContainsFast
548-
if( !dontSearchInSourceAssets )
545+
if( !searchParameters.dontSearchInSourceAssets )
549546
{
550547
searchingSourceAssets = true;
551548

@@ -833,7 +830,7 @@ private void AddSearchedObjectToFilteredSets( Object obj, bool expandGameObjects
833830
if( !string.IsNullOrEmpty( assetPath ) )
834831
{
835832
assetsToSearchPathsSet.Add( assetPath );
836-
if( dontSearchInSourceAssets && AssetDatabase.IsMainAsset( obj ) )
833+
if( searchParameters.dontSearchInSourceAssets && AssetDatabase.IsMainAsset( obj ) )
837834
excludedAssetsPathsSet.Add( assetPath );
838835
}
839836

@@ -1062,7 +1059,7 @@ private ReferenceNode SearchObject( object obj )
10621059
else
10631060
{
10641061
// Comply with the recursive search limit
1065-
if( currentDepth >= searchDepthLimit )
1062+
if( currentDepth >= searchParameters.searchDepthLimit )
10661063
return null;
10671064

10681065
callStack.Add( obj );

0 commit comments

Comments
 (0)