mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-19 22:42:37 +08:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12ed06d43c | |||
| 69ce197932 | |||
| e3dc45b917 |
@@ -12,6 +12,7 @@ using System.Reflection;
|
||||
using System;
|
||||
using System.IO;
|
||||
using UnityEngine.UI;
|
||||
using System.Text;
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
using UnityEngine.U2D;
|
||||
#if UNITY_2018_2_OR_NEWER
|
||||
@@ -33,40 +34,27 @@ namespace AssetUsageDetectorNamespace
|
||||
public class AssetUsageDetector
|
||||
{
|
||||
#region Helper Classes
|
||||
[Serializable]
|
||||
public class Parameters
|
||||
{
|
||||
public IEnumerable<Object> objectsToSearch;
|
||||
public Object[] objectsToSearch = null;
|
||||
|
||||
public SceneSearchMode searchInScenes;
|
||||
public bool searchInAssetsFolder;
|
||||
public IEnumerable<Object> searchInAssetsSubset;
|
||||
public IEnumerable<Object> excludedAssetsFromSearch;
|
||||
public bool dontSearchInSourceAssets;
|
||||
public IEnumerable<Object> excludedScenesFromSearch;
|
||||
public SceneSearchMode searchInScenes = SceneSearchMode.AllScenes;
|
||||
public Object[] searchInScenesSubset = null;
|
||||
public bool searchInAssetsFolder = true;
|
||||
public Object[] searchInAssetsSubset = null;
|
||||
public Object[] excludedAssetsFromSearch = null;
|
||||
public bool dontSearchInSourceAssets = false;
|
||||
public Object[] excludedScenesFromSearch = null;
|
||||
|
||||
public int searchDepthLimit;
|
||||
public BindingFlags fieldModifiers, propertyModifiers;
|
||||
public int searchDepthLimit = 4;
|
||||
public BindingFlags fieldModifiers = BindingFlags.Public | BindingFlags.NonPublic;
|
||||
public BindingFlags propertyModifiers = BindingFlags.Public | BindingFlags.NonPublic;
|
||||
public bool searchNonSerializableVariables = true;
|
||||
|
||||
public bool searchNonSerializableVariables;
|
||||
public bool noAssetDatabaseChanges;
|
||||
public bool showProgressBar;
|
||||
|
||||
public Parameters()
|
||||
{
|
||||
objectsToSearch = null;
|
||||
searchInScenes = SceneSearchMode.AllScenes;
|
||||
searchInAssetsFolder = true;
|
||||
searchInAssetsSubset = null;
|
||||
excludedAssetsFromSearch = null;
|
||||
dontSearchInSourceAssets = false;
|
||||
excludedScenesFromSearch = null;
|
||||
searchDepthLimit = 4;
|
||||
fieldModifiers = BindingFlags.Public | BindingFlags.NonPublic;
|
||||
propertyModifiers = BindingFlags.Public | BindingFlags.NonPublic;
|
||||
searchNonSerializableVariables = true;
|
||||
noAssetDatabaseChanges = false;
|
||||
showProgressBar = false;
|
||||
}
|
||||
public bool lazySceneSearch = false;
|
||||
public bool noAssetDatabaseChanges = false;
|
||||
public bool showDetailedProgressBar = false;
|
||||
}
|
||||
|
||||
private class CacheEntry
|
||||
@@ -111,28 +99,44 @@ namespace AssetUsageDetectorNamespace
|
||||
fileSizes = new long[dependencies.Length];
|
||||
|
||||
for( int i = 0; i < dependencies.Length; i++ )
|
||||
fileSizes[i] = new FileInfo( dependencies[i] ).Length;
|
||||
{
|
||||
FileInfo assetFile = new FileInfo( dependencies[i] );
|
||||
fileSizes[i] = assetFile.Exists ? assetFile.Length : 0L;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
private HashSet<Object> objectsToSearchSet; // A set that contains the searched asset(s) and their sub-assets (if any)
|
||||
// A set that contains the searched asset(s) and their sub-assets (if any)
|
||||
private readonly HashSet<Object> objectsToSearchSet = new HashSet<Object>();
|
||||
|
||||
private HashSet<Object> sceneObjectsToSearchSet; // Scene object(s) in objectsToSearchSet
|
||||
private HashSet<string> sceneObjectsToSearchScenesSet; // sceneObjectsToSearchSet's scene(s)
|
||||
// Scene object(s) in objectsToSearchSet
|
||||
private readonly HashSet<Object> sceneObjectsToSearchSet = new HashSet<Object>();
|
||||
// sceneObjectsToSearchSet's scene(s)
|
||||
private readonly HashSet<string> sceneObjectsToSearchScenesSet = new HashSet<string>();
|
||||
|
||||
private HashSet<Object> assetsToSearchSet; // Project asset(s) in objectsToSearchSet
|
||||
private HashSet<string> assetsToSearchPathsSet; // assetsToSearchSet's path(s)
|
||||
// Project asset(s) in objectsToSearchSet
|
||||
private readonly HashSet<Object> assetsToSearchSet = new HashSet<Object>();
|
||||
// assetsToSearchSet's path(s)
|
||||
private readonly HashSet<string> assetsToSearchPathsSet = new HashSet<string>();
|
||||
// The root prefab objects in assetsToSearch that will be used to search for prefab references
|
||||
private readonly List<GameObject> assetsToSearchRootPrefabs = new List<GameObject>( 4 );
|
||||
|
||||
private SearchResultGroup currentSearchResultGroup; // Results for the currently searched scene
|
||||
// Results for the currently searched scene
|
||||
private SearchResultGroup currentSearchResultGroup;
|
||||
|
||||
private Dictionary<Type, VariableGetterHolder[]> typeToVariables; // An optimization to fetch & filter fields and properties of a class only once
|
||||
private Dictionary<string, ReferenceNode> searchedObjects; // An optimization to search an object only once (key is a hash of the searched object)
|
||||
private Dictionary<string, CacheEntry> assetDependencyCache; // An optimization to fetch the dependencies of an asset only once (key is the path of the asset)
|
||||
// An optimization to fetch & filter fields and properties of a class only once
|
||||
private readonly Dictionary<Type, VariableGetterHolder[]> typeToVariables = new Dictionary<Type, VariableGetterHolder[]>( 4096 );
|
||||
// An optimization to search an object only once (key is a hash of the searched object)
|
||||
private readonly Dictionary<string, ReferenceNode> searchedObjects = new Dictionary<string, ReferenceNode>( 32768 );
|
||||
// An optimization to fetch the dependencies of an asset only once (key is the path of the asset)
|
||||
private Dictionary<string, CacheEntry> assetDependencyCache;
|
||||
|
||||
private Dictionary<Type, Func<Object, ReferenceNode>> typeToSearchFunction; // Dictionary to quickly find the function to search a specific type with
|
||||
// Dictionary to quickly find the function to search a specific type with
|
||||
private Dictionary<Type, Func<Object, ReferenceNode>> typeToSearchFunction;
|
||||
|
||||
private List<object> callStack; // Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function)
|
||||
// Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function)
|
||||
private readonly List<object> callStack = new List<object>( 64 );
|
||||
|
||||
private bool searchPrefabConnections;
|
||||
private bool searchMonoBehavioursForScript;
|
||||
@@ -147,6 +151,12 @@ namespace AssetUsageDetectorNamespace
|
||||
private Object currentObject;
|
||||
private int currentDepth;
|
||||
|
||||
private bool isInPlayMode;
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
private UnityEditor.Experimental.SceneManagement.PrefabStage openPrefabStage;
|
||||
private GameObject openPrefabStagePrefabAsset;
|
||||
#endif
|
||||
|
||||
private BindingFlags fieldModifiers, propertyModifiers;
|
||||
private BindingFlags prevFieldModifiers, prevPropertyModifiers;
|
||||
|
||||
@@ -163,16 +173,53 @@ namespace AssetUsageDetectorNamespace
|
||||
// Search for references!
|
||||
public SearchResult Run( Parameters searchParameters )
|
||||
{
|
||||
if( searchParameters == null )
|
||||
{
|
||||
Debug.LogError( "searchParameters must not be null!" );
|
||||
return new SearchResult( false, null, null, this, searchParameters );
|
||||
}
|
||||
|
||||
if( searchParameters.objectsToSearch == null )
|
||||
{
|
||||
Debug.LogError( "objectsToSearch list is empty" );
|
||||
return new SearchResult( false, null, null );
|
||||
Debug.LogError( "objectsToSearch list is empty!" );
|
||||
return new SearchResult( false, null, null, this, searchParameters );
|
||||
}
|
||||
|
||||
if( !EditorApplication.isPlaying && !Utilities.AreScenesSaved() )
|
||||
{
|
||||
// Don't start the search if at least one scene is currently dirty (not saved)
|
||||
Debug.LogError( "Save open scenes first!" );
|
||||
return new SearchResult( false, null, null, this, searchParameters );
|
||||
}
|
||||
|
||||
List<SearchResultGroup> searchResult = null;
|
||||
|
||||
isInPlayMode = EditorApplication.isPlaying;
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
openPrefabStagePrefabAsset = null;
|
||||
string openPrefabStageAssetPath = null;
|
||||
openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
||||
if( openPrefabStage != null )
|
||||
{
|
||||
if( !openPrefabStage.stageHandle.IsValid() )
|
||||
openPrefabStage = null;
|
||||
else
|
||||
{
|
||||
if( openPrefabStage.scene.isDirty )
|
||||
{
|
||||
// Don't start the search if a prefab stage is currently open and dirty (not saved)
|
||||
Debug.LogError( "Save open prefabs first!" );
|
||||
return new SearchResult( false, null, null, this, searchParameters );
|
||||
}
|
||||
|
||||
openPrefabStagePrefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStage.prefabAssetPath );
|
||||
openPrefabStageAssetPath = openPrefabStage.prefabAssetPath;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Get the scenes that are open right now
|
||||
SceneSetup[] initialSceneSetup = !EditorApplication.isPlaying ? EditorSceneManager.GetSceneManagerSetup() : null;
|
||||
SceneSetup[] initialSceneSetup = !isInPlayMode ? EditorSceneManager.GetSceneManagerSetup() : null;
|
||||
|
||||
// Make sure the AssetDatabase is up-to-date
|
||||
AssetDatabase.SaveAssets();
|
||||
@@ -190,45 +237,17 @@ namespace AssetUsageDetectorNamespace
|
||||
// Initialize commonly used variables
|
||||
searchResult = new List<SearchResultGroup>(); // Overall search results
|
||||
|
||||
if( typeToVariables == null )
|
||||
typeToVariables = new Dictionary<Type, VariableGetterHolder[]>( 4096 );
|
||||
else if( prevFieldModifiers != fieldModifiers || prevPropertyModifiers != propertyModifiers )
|
||||
if( prevFieldModifiers != fieldModifiers || prevPropertyModifiers != propertyModifiers )
|
||||
typeToVariables.Clear();
|
||||
|
||||
if( searchedObjects == null )
|
||||
searchedObjects = new Dictionary<string, ReferenceNode>( 32768 );
|
||||
else
|
||||
searchedObjects.Clear();
|
||||
|
||||
if( callStack == null )
|
||||
callStack = new List<object>( 64 );
|
||||
else
|
||||
callStack.Clear();
|
||||
|
||||
if( objectsToSearchSet == null )
|
||||
objectsToSearchSet = new HashSet<Object>();
|
||||
else
|
||||
objectsToSearchSet.Clear();
|
||||
|
||||
if( sceneObjectsToSearchSet == null )
|
||||
sceneObjectsToSearchSet = new HashSet<Object>();
|
||||
else
|
||||
sceneObjectsToSearchSet.Clear();
|
||||
|
||||
if( sceneObjectsToSearchScenesSet == null )
|
||||
sceneObjectsToSearchScenesSet = new HashSet<string>();
|
||||
else
|
||||
sceneObjectsToSearchScenesSet.Clear();
|
||||
|
||||
if( assetsToSearchSet == null )
|
||||
assetsToSearchSet = new HashSet<Object>();
|
||||
else
|
||||
assetsToSearchSet.Clear();
|
||||
|
||||
if( assetsToSearchPathsSet == null )
|
||||
assetsToSearchPathsSet = new HashSet<string>();
|
||||
else
|
||||
assetsToSearchPathsSet.Clear();
|
||||
searchedObjects.Clear();
|
||||
callStack.Clear();
|
||||
objectsToSearchSet.Clear();
|
||||
sceneObjectsToSearchSet.Clear();
|
||||
sceneObjectsToSearchScenesSet.Clear();
|
||||
assetsToSearchSet.Clear();
|
||||
assetsToSearchPathsSet.Clear();
|
||||
assetsToSearchRootPrefabs.Clear();
|
||||
|
||||
if( assetDependencyCache == null )
|
||||
{
|
||||
@@ -327,30 +346,36 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Find the scenes to search for references
|
||||
HashSet<string> openScenes = null;
|
||||
if( EditorApplication.isPlaying )
|
||||
HashSet<string> scenesToSearch = new HashSet<string>();
|
||||
if( searchParameters.searchInScenesSubset != null )
|
||||
{
|
||||
// In Play mode, only open scenes can be searched
|
||||
openScenes = new HashSet<string>();
|
||||
for( int i = 0; i < EditorSceneManager.loadedSceneCount; i++ )
|
||||
foreach( Object obj in searchParameters.searchInScenesSubset )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneAt( i );
|
||||
if( scene.IsValid() )
|
||||
openScenes.Add( scene.path );
|
||||
if( obj == null || obj.Equals( null ) )
|
||||
continue;
|
||||
|
||||
if( !obj.IsAsset() )
|
||||
continue;
|
||||
|
||||
if( obj.IsFolder() )
|
||||
{
|
||||
string[] folderContents = AssetDatabase.FindAssets( "t:SceneAsset", new string[] { AssetDatabase.GetAssetPath( obj ) } );
|
||||
if( folderContents == null )
|
||||
continue;
|
||||
|
||||
for( int i = 0; i < folderContents.Length; i++ )
|
||||
scenesToSearch.Add( AssetDatabase.GUIDToAssetPath( folderContents[i] ) );
|
||||
}
|
||||
else if( obj is SceneAsset )
|
||||
scenesToSearch.Add( AssetDatabase.GetAssetPath( obj ) );
|
||||
}
|
||||
}
|
||||
|
||||
HashSet<string> scenesToSearch = new HashSet<string>();
|
||||
if( ( searchParameters.searchInScenes & SceneSearchMode.AllScenes ) == SceneSearchMode.AllScenes )
|
||||
else if( ( searchParameters.searchInScenes & SceneSearchMode.AllScenes ) == SceneSearchMode.AllScenes )
|
||||
{
|
||||
// Get all scenes from the Assets folder
|
||||
string[] sceneGuids = AssetDatabase.FindAssets( "t:SceneAsset" );
|
||||
for( int i = 0; i < sceneGuids.Length; i++ )
|
||||
{
|
||||
string scenePath = AssetDatabase.GUIDToAssetPath( sceneGuids[i] );
|
||||
if( !EditorApplication.isPlaying || openScenes.Contains( scenePath ) )
|
||||
scenesToSearch.Add( scenePath );
|
||||
}
|
||||
scenesToSearch.Add( AssetDatabase.GUIDToAssetPath( sceneGuids[i] ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -372,44 +397,74 @@ namespace AssetUsageDetectorNamespace
|
||||
EditorBuildSettingsScene[] scenesTemp = EditorBuildSettings.scenes;
|
||||
for( int i = 0; i < scenesTemp.Length; i++ )
|
||||
{
|
||||
if( ( searchInScenesInBuildTickedAll || scenesTemp[i].enabled ) && ( !EditorApplication.isPlaying || openScenes.Contains( scenesTemp[i].path ) ) )
|
||||
if( ( searchInScenesInBuildTickedAll || scenesTemp[i].enabled ) )
|
||||
scenesToSearch.Add( scenesTemp[i].path );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// In Play mode, only open scenes can be searched
|
||||
if( isInPlayMode )
|
||||
{
|
||||
HashSet<string> openScenes = new HashSet<string>();
|
||||
for( int i = 0; i < EditorSceneManager.loadedSceneCount; i++ )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneAt( i );
|
||||
if( scene.IsValid() )
|
||||
openScenes.Add( scene.path );
|
||||
}
|
||||
|
||||
scenesToSearch.RemoveWhere( ( path ) =>
|
||||
{
|
||||
if( !openScenes.Contains( path ) )
|
||||
{
|
||||
Debug.Log( "Can't search unloaded scenes while in play mode, skipped " + path );
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} );
|
||||
}
|
||||
|
||||
// By default, search only serializable variables for references
|
||||
searchSerializableVariablesOnly = !searchParameters.searchNonSerializableVariables;
|
||||
|
||||
// Initialize the nodes of searched asset(s)
|
||||
foreach( Object obj in objectsToSearchSet )
|
||||
{
|
||||
BeginSearchObject( obj );
|
||||
|
||||
string objHash = obj.Hash();
|
||||
ReferenceNode referenceNode;
|
||||
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
|
||||
searchedObjects[objHash] = PopReferenceNode( obj );
|
||||
if( searchParameters.dontSearchInSourceAssets )
|
||||
searchedObjects.Add( objHash, PopReferenceNode( obj ) );
|
||||
else
|
||||
{
|
||||
BeginSearchObject( obj );
|
||||
|
||||
ReferenceNode referenceNode;
|
||||
if( !searchedObjects.TryGetValue( objHash, out referenceNode ) || referenceNode == null )
|
||||
searchedObjects[objHash] = PopReferenceNode( obj );
|
||||
}
|
||||
}
|
||||
|
||||
// Progressbar values
|
||||
int searchProgress = 0;
|
||||
int searchTotalProgress = scenesToSearch.Count;
|
||||
if( EditorApplication.isPlaying )
|
||||
if( isInPlayMode && searchParameters.searchInScenes != SceneSearchMode.None )
|
||||
searchTotalProgress++; // DontDestroyOnLoad scene
|
||||
|
||||
// Don't search assets if searched object(s) are all scene objects as assets can't hold references to scene objects
|
||||
if( searchParameters.searchInAssetsFolder && assetsToSearchSet.Count > 0 )
|
||||
{
|
||||
currentSearchResultGroup = new SearchResultGroup( "Project View (Assets)", false );
|
||||
currentSearchResultGroup = new SearchResultGroup( "Project Window (Assets)", SearchResultGroup.GroupType.Assets );
|
||||
|
||||
// Get the paths of all assets that are to be searched
|
||||
IEnumerable<string> assetPaths;
|
||||
if( searchParameters.searchInAssetsSubset == null )
|
||||
{
|
||||
string[] allAssetPaths = AssetDatabase.GetAllAssetPaths();
|
||||
searchTotalProgress += allAssetPaths.Length;
|
||||
assetPaths = allAssetPaths;
|
||||
|
||||
if( searchParameters.showDetailedProgressBar )
|
||||
searchTotalProgress += allAssetPaths.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -429,8 +484,10 @@ namespace AssetUsageDetectorNamespace
|
||||
folderContentsSet.Add( AssetDatabase.GetAssetPath( obj ) );
|
||||
}
|
||||
|
||||
searchTotalProgress += folderContentsSet.Count;
|
||||
assetPaths = folderContentsSet;
|
||||
|
||||
if( searchParameters.showDetailedProgressBar )
|
||||
searchTotalProgress += folderContentsSet.Count;
|
||||
}
|
||||
|
||||
// Calculate the path(s) of the assets that won't be searched for references
|
||||
@@ -455,9 +512,12 @@ namespace AssetUsageDetectorNamespace
|
||||
if( searchParameters.dontSearchInSourceAssets )
|
||||
excludedAssetsPathsSet.UnionWith( assetsToSearchPathsSet );
|
||||
|
||||
if( EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching assets", 0f ) )
|
||||
throw new Exception( "Search aborted" );
|
||||
|
||||
foreach( string path in assetPaths )
|
||||
{
|
||||
if( searchParameters.showProgressBar && ++searchProgress % 30 == 1 && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching assets", (float) searchProgress / searchTotalProgress ) )
|
||||
if( searchParameters.showDetailedProgressBar && ++searchProgress % 30 == 1 && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching assets", (float) searchProgress / searchTotalProgress ) )
|
||||
throw new Exception( "Search aborted" );
|
||||
|
||||
if( excludedAssetsPathsSet.Contains( path ) )
|
||||
@@ -490,7 +550,7 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Search non-serializable variables for references only if we are currently searching a scene and the editor is in play mode
|
||||
if( EditorApplication.isPlaying )
|
||||
if( isInPlayMode )
|
||||
searchSerializableVariablesOnly = false;
|
||||
|
||||
if( scenesToSearch.Count > 0 )
|
||||
@@ -523,7 +583,7 @@ namespace AssetUsageDetectorNamespace
|
||||
|
||||
foreach( string scenePath in scenesToSearch )
|
||||
{
|
||||
if( searchParameters.showProgressBar && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching scene: " + scenePath, (float) ++searchProgress / searchTotalProgress ) )
|
||||
if( EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching scene: " + scenePath, (float) ++searchProgress / searchTotalProgress ) )
|
||||
throw new Exception( "Search aborted" );
|
||||
|
||||
// Search scene for references
|
||||
@@ -533,17 +593,17 @@ namespace AssetUsageDetectorNamespace
|
||||
if( excludedScenesPathsSet.Contains( scenePath ) )
|
||||
continue;
|
||||
|
||||
SearchScene( scenePath, searchResult, initialSceneSetup );
|
||||
SearchScene( scenePath, searchResult, searchParameters.lazySceneSearch, initialSceneSetup );
|
||||
}
|
||||
}
|
||||
|
||||
// Search through all the GameObjects under the DontDestroyOnLoad scene (if exists)
|
||||
if( EditorApplication.isPlaying )
|
||||
if( isInPlayMode && searchParameters.searchInScenes != SceneSearchMode.None )
|
||||
{
|
||||
if( searchParameters.showProgressBar && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching scene: DontDestroyOnLoad", 1f ) )
|
||||
if( searchParameters.showDetailedProgressBar && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Searching scene: DontDestroyOnLoad", 1f ) )
|
||||
throw new Exception( "Search aborted" );
|
||||
|
||||
currentSearchResultGroup = new SearchResultGroup( "DontDestroyOnLoad", false );
|
||||
currentSearchResultGroup = new SearchResultGroup( "DontDestroyOnLoad", SearchResultGroup.GroupType.DontDestroyOnLoad );
|
||||
|
||||
GameObject[] rootGameObjects = GetDontDestroyOnLoadObjects();
|
||||
for( int i = 0; i < rootGameObjects.Length; i++ )
|
||||
@@ -558,12 +618,32 @@ namespace AssetUsageDetectorNamespace
|
||||
// Log some c00l stuff to console
|
||||
Debug.Log( "Searched " + searchedObjectsCount + " objects in " + ( EditorApplication.timeSinceStartup - searchStartTime ).ToString( "F2" ) + " seconds" );
|
||||
|
||||
return new SearchResult( true, searchResult, initialSceneSetup );
|
||||
return new SearchResult( true, searchResult, initialSceneSetup, this, searchParameters );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
Debug.LogException( e );
|
||||
|
||||
if( callStack.Count > 0 )
|
||||
{
|
||||
StringBuilder sb = new StringBuilder( callStack.Count * 50 );
|
||||
sb.AppendLine( "Stack contents: " );
|
||||
for( int i = 0; i < callStack.Count; i++ )
|
||||
{
|
||||
sb.Append( i ).Append( ": " );
|
||||
|
||||
Object unityObject = callStack[i] as Object;
|
||||
if( unityObject != null )
|
||||
sb.Append( unityObject.name ).Append( " (" ).Append( unityObject.GetType() ).AppendLine( ")" );
|
||||
else if( callStack[i] != null )
|
||||
sb.Append( callStack[i].GetType() ).AppendLine( " object" );
|
||||
else
|
||||
sb.AppendLine( "<<destroyed>>" );
|
||||
}
|
||||
|
||||
Debug.LogError( sb.ToString() );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
InitializeSearchResultNodes( searchResult );
|
||||
@@ -571,7 +651,7 @@ namespace AssetUsageDetectorNamespace
|
||||
catch
|
||||
{ }
|
||||
|
||||
return new SearchResult( false, searchResult, initialSceneSetup );
|
||||
return new SearchResult( false, searchResult, initialSceneSetup, this, searchParameters );
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -579,6 +659,12 @@ namespace AssetUsageDetectorNamespace
|
||||
currentObject = null;
|
||||
|
||||
EditorUtility.ClearProgressBar();
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
// If a prefab stage was open when the search was triggered, try reopening the prefab stage after the search is completed
|
||||
if( !string.IsNullOrEmpty( openPrefabStageAssetPath ) )
|
||||
AssetDatabase.OpenAsset( AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStageAssetPath ) );
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -590,7 +676,7 @@ namespace AssetUsageDetectorNamespace
|
||||
// If there are any empty groups after node initialization, remove those groups
|
||||
for( int i = searchResult.Count - 1; i >= 0; i-- )
|
||||
{
|
||||
if( searchResult[i].NumberOfReferences == 0 )
|
||||
if( !searchResult[i].PendingSearch && searchResult[i].NumberOfReferences == 0 )
|
||||
searchResult.RemoveAtFast( i );
|
||||
}
|
||||
}
|
||||
@@ -606,6 +692,16 @@ namespace AssetUsageDetectorNamespace
|
||||
|
||||
objectsToSearchSet.Add( obj );
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
// When searching for references of a prefab stage object, try adding its corresponding prefab asset to the searched assets, as well
|
||||
if( openPrefabStage != null && openPrefabStagePrefabAsset != null && obj is GameObject && openPrefabStage.IsPartOfPrefabContents( (GameObject) obj ) )
|
||||
{
|
||||
GameObject prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( openPrefabStagePrefabAsset );
|
||||
if( prefabStageObjectSource != null )
|
||||
AddSearchedObjectToFilteredSets( prefabStageObjectSource );
|
||||
}
|
||||
#endif
|
||||
|
||||
bool isAsset = obj.IsAsset();
|
||||
if( isAsset )
|
||||
{
|
||||
@@ -614,6 +710,34 @@ namespace AssetUsageDetectorNamespace
|
||||
string assetPath = AssetDatabase.GetAssetPath( obj );
|
||||
if( !string.IsNullOrEmpty( assetPath ) )
|
||||
assetsToSearchPathsSet.Add( assetPath );
|
||||
|
||||
GameObject go = null;
|
||||
if( obj is GameObject )
|
||||
go = (GameObject) obj;
|
||||
else if( obj is Component )
|
||||
go = ( (Component) obj ).gameObject;
|
||||
|
||||
if( go != null )
|
||||
{
|
||||
Transform transform = go.transform;
|
||||
bool shouldAddRootPrefabEntry = true;
|
||||
for( int i = assetsToSearchRootPrefabs.Count - 1; i >= 0; i-- )
|
||||
{
|
||||
Transform rootTransform = assetsToSearchRootPrefabs[i].transform;
|
||||
|
||||
if( transform.IsChildOf( rootTransform ) )
|
||||
{
|
||||
shouldAddRootPrefabEntry = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if( rootTransform.IsChildOf( transform ) )
|
||||
assetsToSearchRootPrefabs.RemoveAt( i );
|
||||
}
|
||||
|
||||
if( shouldAddRootPrefabEntry )
|
||||
assetsToSearchRootPrefabs.Add( go );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -645,24 +769,32 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Search a scene for references
|
||||
private void SearchScene( string scenePath, List<SearchResultGroup> searchResult, SceneSetup[] initialSceneSetup )
|
||||
private void SearchScene( string scenePath, List<SearchResultGroup> searchResult, bool lazySearch, SceneSetup[] initialSceneSetup )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneByPath( scenePath );
|
||||
if( EditorApplication.isPlaying && !scene.isLoaded )
|
||||
if( isInPlayMode && !scene.isLoaded )
|
||||
return;
|
||||
|
||||
bool canContainSceneObjectReference = scene.isLoaded && ( !EditorSceneManager.preventCrossSceneReferences || sceneObjectsToSearchScenesSet.Contains( scenePath ) );
|
||||
if( !canContainSceneObjectReference )
|
||||
{
|
||||
bool canContainAssetReference = assetsToSearchSet.Count > 0 && ( EditorApplication.isPlaying || AssetHasAnyReference( scenePath ) );
|
||||
bool canContainAssetReference = assetsToSearchSet.Count > 0 && ( isInPlayMode || AssetHasAnyReference( scenePath ) );
|
||||
if( !canContainAssetReference )
|
||||
return;
|
||||
}
|
||||
|
||||
if( !EditorApplication.isPlaying )
|
||||
scene = EditorSceneManager.OpenScene( scenePath, OpenSceneMode.Additive );
|
||||
if( !scene.isLoaded )
|
||||
{
|
||||
if( lazySearch )
|
||||
{
|
||||
searchResult.Add( new SearchResultGroup( scenePath, SearchResultGroup.GroupType.Scene, true, true ) );
|
||||
return;
|
||||
}
|
||||
|
||||
currentSearchResultGroup = new SearchResultGroup( scenePath, true );
|
||||
scene = EditorSceneManager.OpenScene( scenePath, OpenSceneMode.Additive );
|
||||
}
|
||||
|
||||
currentSearchResultGroup = new SearchResultGroup( scenePath, SearchResultGroup.GroupType.Scene );
|
||||
|
||||
// Search through all the GameObjects in the scene
|
||||
GameObject[] rootGameObjects = scene.GetRootGameObjects();
|
||||
@@ -672,7 +804,7 @@ namespace AssetUsageDetectorNamespace
|
||||
// If no references are found in the scene and if the scene is not part of the initial scene setup, close it
|
||||
if( currentSearchResultGroup.NumberOfReferences == 0 )
|
||||
{
|
||||
if( !EditorApplication.isPlaying )
|
||||
if( !isInPlayMode )
|
||||
{
|
||||
bool sceneIsOneOfInitials = false;
|
||||
for( int i = 0; i < initialSceneSetup.Length; i++ )
|
||||
@@ -782,7 +914,7 @@ namespace AssetUsageDetectorNamespace
|
||||
else
|
||||
{
|
||||
result = PopReferenceNode( unityObject );
|
||||
SearchFieldsAndPropertiesOf( result );
|
||||
SearchWithSerializedObject( result );
|
||||
}
|
||||
|
||||
callStack.RemoveAt( callStack.Count - 1 );
|
||||
@@ -828,11 +960,10 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
Object prefab = PrefabUtility.GetCorrespondingObjectFromSource( go );
|
||||
if( objectsToSearchSet.Contains( prefab ) && go == PrefabUtility.GetNearestPrefabInstanceRoot( go ) )
|
||||
#else
|
||||
Object prefab = PrefabUtility.GetPrefabParent( go );
|
||||
if( objectsToSearchSet.Contains( prefab ) && go == PrefabUtility.FindRootGameObjectWithSameParentPrefab( go ) )
|
||||
#endif
|
||||
if( objectsToSearchSet.Contains( prefab ) && assetsToSearchRootPrefabs.ContainsFast( prefab as GameObject ) )
|
||||
referenceNode.AddLinkTo( GetReferenceNode( prefab ), "Prefab object" );
|
||||
}
|
||||
|
||||
@@ -907,7 +1038,7 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
#endif
|
||||
|
||||
SearchFieldsAndPropertiesOf( referenceNode );
|
||||
SearchWithSerializedObject( referenceNode );
|
||||
return referenceNode;
|
||||
}
|
||||
|
||||
@@ -1089,6 +1220,56 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
#endif
|
||||
|
||||
// Search through field and properties of an object for references with SerializedObject
|
||||
private void SearchWithSerializedObject( ReferenceNode referenceNode )
|
||||
{
|
||||
if( !isInPlayMode || referenceNode.nodeObject.IsAsset() )
|
||||
{
|
||||
SerializedObject so = new SerializedObject( (Object) referenceNode.nodeObject );
|
||||
SerializedProperty iterator = so.GetIterator();
|
||||
if( iterator.NextVisible( true ) )
|
||||
{
|
||||
bool enterChildren;
|
||||
do
|
||||
{
|
||||
ReferenceNode searchResult;
|
||||
switch( iterator.propertyType )
|
||||
{
|
||||
case SerializedPropertyType.ObjectReference:
|
||||
searchResult = SearchObject( iterator.objectReferenceValue );
|
||||
enterChildren = false;
|
||||
break;
|
||||
case SerializedPropertyType.ExposedReference:
|
||||
searchResult = SearchObject( iterator.exposedReferenceValue );
|
||||
enterChildren = false;
|
||||
break;
|
||||
case SerializedPropertyType.Generic:
|
||||
searchResult = null;
|
||||
enterChildren = true;
|
||||
break;
|
||||
default:
|
||||
searchResult = null;
|
||||
enterChildren = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if( searchResult != null )
|
||||
{
|
||||
if( searchResult != referenceNode )
|
||||
referenceNode.AddLinkTo( searchResult, "Variable: " + iterator.propertyPath );
|
||||
else
|
||||
PoolReferenceNode( searchResult );
|
||||
}
|
||||
} while( iterator.NextVisible( enterChildren ) );
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Use reflection algorithm as fallback
|
||||
SearchFieldsAndPropertiesOf( referenceNode );
|
||||
}
|
||||
|
||||
// Search through field and properties of an object for references
|
||||
private void SearchFieldsAndPropertiesOf( ReferenceNode referenceNode )
|
||||
{
|
||||
@@ -1106,20 +1287,43 @@ namespace AssetUsageDetectorNamespace
|
||||
if( variableValue == null )
|
||||
continue;
|
||||
|
||||
if( !( variableValue is IEnumerable ) || variableValue is Transform )
|
||||
referenceNode.AddLinkTo( SearchObject( variableValue ), ( variables[i].isProperty ? "Property: " : "Variable: " ) + variables[i].name );
|
||||
else
|
||||
// Values stored inside ICollection objects are searched using IEnumerable,
|
||||
// no need to have duplicate search entries
|
||||
if( !( variableValue is ICollection ) )
|
||||
{
|
||||
ReferenceNode searchResult = SearchObject( variableValue );
|
||||
if( searchResult != null )
|
||||
{
|
||||
if( searchResult != referenceNode )
|
||||
referenceNode.AddLinkTo( searchResult, ( variables[i].isProperty ? "Property: " : "Variable: " ) + variables[i].name );
|
||||
else
|
||||
PoolReferenceNode( searchResult );
|
||||
}
|
||||
}
|
||||
|
||||
if( variableValue is IEnumerable && !( variableValue is Transform ) )
|
||||
{
|
||||
// If the field is IEnumerable (possibly an array or collection), search through members of it
|
||||
// Note that Transform IEnumerable (children of the transform) is not iterated
|
||||
foreach( object arrayItem in (IEnumerable) variableValue )
|
||||
referenceNode.AddLinkTo( SearchObject( arrayItem ), ( variables[i].isProperty ? "Property (IEnumerable): " : "Variable (IEnumerable): " ) + variables[i].name );
|
||||
foreach( object element in (IEnumerable) variableValue )
|
||||
{
|
||||
ReferenceNode searchResult = SearchObject( element );
|
||||
if( searchResult != null )
|
||||
{
|
||||
if( searchResult != referenceNode )
|
||||
referenceNode.AddLinkTo( searchResult, ( variables[i].isProperty ? "Property (IEnumerable): " : "Variable (IEnumerable): " ) + variables[i].name );
|
||||
else
|
||||
PoolReferenceNode( searchResult );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( UnassignedReferenceException )
|
||||
{ }
|
||||
catch( MissingReferenceException )
|
||||
{ }
|
||||
catch( MissingComponentException )
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1149,12 +1353,14 @@ namespace AssetUsageDetectorNamespace
|
||||
FieldInfo[] fields = currType.GetFields( fieldModifiers );
|
||||
for( int i = 0; i < fields.Length; i++ )
|
||||
{
|
||||
FieldInfo field = fields[i];
|
||||
|
||||
// Skip obsolete fields
|
||||
if( Attribute.IsDefined( fields[i], typeof( ObsoleteAttribute ) ) )
|
||||
if( Attribute.IsDefined( field, typeof( ObsoleteAttribute ) ) )
|
||||
continue;
|
||||
|
||||
// Skip primitive types
|
||||
Type variableType = fields[i].FieldType;
|
||||
Type variableType = field.FieldType;
|
||||
if( variableType.IsPrimitiveUnityType() )
|
||||
continue;
|
||||
|
||||
@@ -1162,16 +1368,20 @@ namespace AssetUsageDetectorNamespace
|
||||
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
|
||||
continue;
|
||||
|
||||
// Searching pointer variables for reference throws ArgumentException
|
||||
if( variableType.IsPointer )
|
||||
continue;
|
||||
|
||||
// Additional filtering for fields:
|
||||
// 1- Ignore "m_RectTransform", "m_CanvasRenderer" and "m_Canvas" fields of Graphic components
|
||||
string fieldName = fields[i].Name;
|
||||
string fieldName = field.Name;
|
||||
if( typeof( Graphic ).IsAssignableFrom( currType ) &&
|
||||
( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) )
|
||||
continue;
|
||||
|
||||
VariableGetVal getter = fields[i].CreateGetter( type );
|
||||
VariableGetVal getter = field.CreateGetter( type );
|
||||
if( getter != null )
|
||||
validVariables.Add( new VariableGetterHolder( fields[i], getter, fields[i].IsSerializable() ) );
|
||||
validVariables.Add( new VariableGetterHolder( field, getter, field.IsSerializable() ) );
|
||||
}
|
||||
|
||||
currType = currType.BaseType;
|
||||
@@ -1186,12 +1396,14 @@ namespace AssetUsageDetectorNamespace
|
||||
PropertyInfo[] properties = currType.GetProperties( propertyModifiers );
|
||||
for( int i = 0; i < properties.Length; i++ )
|
||||
{
|
||||
PropertyInfo property = properties[i];
|
||||
|
||||
// Skip obsolete properties
|
||||
if( Attribute.IsDefined( properties[i], typeof( ObsoleteAttribute ) ) )
|
||||
if( Attribute.IsDefined( property, typeof( ObsoleteAttribute ) ) )
|
||||
continue;
|
||||
|
||||
// Skip primitive types
|
||||
Type variableType = properties[i].PropertyType;
|
||||
Type variableType = property.PropertyType;
|
||||
if( variableType.IsPrimitiveUnityType() )
|
||||
continue;
|
||||
|
||||
@@ -1199,15 +1411,28 @@ namespace AssetUsageDetectorNamespace
|
||||
if( typeof( Type ).IsAssignableFrom( variableType ) || variableType.Namespace == reflectionNameSpace )
|
||||
continue;
|
||||
|
||||
// Searching pointer variables for reference throws ArgumentException
|
||||
if( variableType.IsPointer )
|
||||
continue;
|
||||
|
||||
// Skip properties without a getter function
|
||||
MethodInfo propertyGetter = property.GetGetMethod( true );
|
||||
if( propertyGetter == null )
|
||||
continue;
|
||||
|
||||
// Skip indexer properties
|
||||
if( property.GetIndexParameters().Length > 0 )
|
||||
continue;
|
||||
|
||||
// No need to check properties with 'override' keyword
|
||||
if( properties[i].IsOverridden() )
|
||||
if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
|
||||
continue;
|
||||
|
||||
// Additional filtering for properties:
|
||||
// 1- Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of Component's to get more useful results
|
||||
// 2- Ignore "canvasRenderer" and "canvas" properties of Graphic components
|
||||
// 3 & 4- Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
|
||||
string propertyName = properties[i].Name;
|
||||
string propertyName = property.Name;
|
||||
if( typeof( Component ).IsAssignableFrom( currType ) && ( propertyName == "gameObject" ||
|
||||
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
|
||||
continue;
|
||||
@@ -1230,9 +1455,9 @@ namespace AssetUsageDetectorNamespace
|
||||
continue;
|
||||
else
|
||||
{
|
||||
VariableGetVal getter = properties[i].CreateGetter();
|
||||
VariableGetVal getter = property.CreateGetter();
|
||||
if( getter != null )
|
||||
validVariables.Add( new VariableGetterHolder( properties[i], getter, properties[i].IsSerializable() ) );
|
||||
validVariables.Add( new VariableGetterHolder( property, getter, property.IsSerializable() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1251,6 +1476,10 @@ namespace AssetUsageDetectorNamespace
|
||||
// Check if the asset at specified path depends on any of the references
|
||||
private bool AssetHasAnyReference( string assetPath )
|
||||
{
|
||||
// "Find references of" assets can contain internal references
|
||||
if( assetsToSearchPathsSet.Contains( assetPath ) )
|
||||
return true;
|
||||
|
||||
CacheEntry cacheEntry;
|
||||
if( !assetDependencyCache.TryGetValue( assetPath, out cacheEntry ) )
|
||||
{
|
||||
@@ -1271,13 +1500,16 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
// If a dependency was renamed (which doesn't affect the verified hash, unfortunately),
|
||||
// force refresh the asset's dependencies and search it again
|
||||
FileInfo assetFile = new FileInfo( dependencies[i] );
|
||||
if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
|
||||
if( !Directory.Exists( dependencies[i] ) ) // Calling FileInfo.Length on a directory throws FileNotFoundException
|
||||
{
|
||||
cacheEntry.Refresh( assetPath );
|
||||
cacheEntry.searchResult = CacheEntry.Result.Unknown;
|
||||
FileInfo assetFile = new FileInfo( dependencies[i] );
|
||||
if( !assetFile.Exists || assetFile.Length != fileSizes[i] )
|
||||
{
|
||||
cacheEntry.Refresh( assetPath );
|
||||
cacheEntry.searchResult = CacheEntry.Result.Unknown;
|
||||
|
||||
return AssetHasAnyReference( assetPath );
|
||||
return AssetHasAnyReference( assetPath );
|
||||
}
|
||||
}
|
||||
|
||||
if( assetsToSearchPathsSet.Contains( dependencies[i] ) )
|
||||
@@ -1445,7 +1677,7 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
for( int i = 0; i < allAssets.Length; i++ )
|
||||
{
|
||||
if( i % 30 == 0 && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Generating cache for the first time", (float) i / allAssets.Length ) )
|
||||
if( i % 30 == 0 && EditorUtility.DisplayCancelableProgressBar( "Please wait...", "Generating cache for the first time (optional)", (float) i / allAssets.Length ) )
|
||||
{
|
||||
EditorUtility.ClearProgressBar();
|
||||
Debug.LogWarning( "Initial cache generation cancelled, cache will be generated on the fly as more and more assets are searched." );
|
||||
|
||||
@@ -21,6 +21,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
private const string PREFS_SEARCH_FIELDS = "AUD_Fields";
|
||||
private const string PREFS_SEARCH_PROPERTIES = "AUD_Properties";
|
||||
private const string PREFS_SEARCH_NON_SERIALIZABLES = "AUD_NonSerializables";
|
||||
private const string PREFS_LAZY_SCENE_SEARCH = "AUD_LazySceneSearch";
|
||||
private const string PREFS_PATH_DRAWING_MODE = "AUD_PathDrawing";
|
||||
private const string PREFS_SHOW_PROGRESS = "AUD_Progress";
|
||||
private const string PREFS_SHOW_TOOLTIPS = "AUD_Tooltips";
|
||||
@@ -36,7 +37,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
private bool searchInScenesInBuild = true; // Scenes in build
|
||||
private bool searchInScenesInBuildTickedOnly = true; // Scenes in build (ticked only or not)
|
||||
private bool searchInAllScenes = true; // All scenes (including scenes that are not in build)
|
||||
private bool searchInAssetsFolder = true; // Assets in Project view
|
||||
private bool searchInAssetsFolder = true; // Assets in Project window
|
||||
private bool dontSearchInSourceAssets = false; // objectsToSearch won't be searched for internal references
|
||||
|
||||
private List<Object> searchInAssetsSubset = new List<Object>() { null }; // If not empty, only these assets are searched for references
|
||||
@@ -49,13 +50,14 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
private string errorMessage = string.Empty;
|
||||
|
||||
private bool lazySceneSearch = false;
|
||||
private bool searchNonSerializableVariables = true;
|
||||
private bool noAssetDatabaseChanges = false;
|
||||
private bool showProgressBar = false;
|
||||
private bool showDetailedProgressBar = false;
|
||||
|
||||
private BindingFlags fieldModifiers, propertyModifiers;
|
||||
|
||||
private SearchResultDrawParameters searchResultDrawParameters = new SearchResultDrawParameters( PathDrawingMode.ShortRelevantParts, false );
|
||||
private SearchResultDrawParameters searchResultDrawParameters = new SearchResultDrawParameters( PathDrawingMode.ShortRelevantParts, false, false );
|
||||
|
||||
private double nextPlayModeRefreshTime = 0f;
|
||||
|
||||
@@ -76,20 +78,31 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
window.titleContent = new GUIContent( "Asset Usage Detector" );
|
||||
window.minSize = new Vector2( 325f, 220f );
|
||||
|
||||
window.LoadPrefs();
|
||||
window.Show();
|
||||
}
|
||||
|
||||
// Quickly initiate search for the selected assets
|
||||
[MenuItem( "GameObject/Search for References", priority = 49 )]
|
||||
[MenuItem( "Assets/Search for References", priority = 1000 )]
|
||||
private static void SearchSelectedAssetReferences()
|
||||
private static void SearchSelectedAssetReferences( MenuCommand command )
|
||||
{
|
||||
ShowAndSearch( Selection.objects );
|
||||
// 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
|
||||
if( command.context )
|
||||
{
|
||||
EditorApplication.update -= CallSearchSelectedAssetReferencesOnce;
|
||||
EditorApplication.update += CallSearchSelectedAssetReferencesOnce;
|
||||
}
|
||||
else
|
||||
ShowAndSearch( Selection.objects );
|
||||
}
|
||||
|
||||
// Show the menu item only if there is a selection in the Editor
|
||||
[MenuItem( "GameObject/Search for References", validate = true )]
|
||||
[MenuItem( "Assets/Search for References", validate = true )]
|
||||
private static bool SearchSelectedAssetReferencesValidate()
|
||||
private static bool SearchSelectedAssetReferencesValidate( MenuCommand command )
|
||||
{
|
||||
return Selection.objects.Length > 0;
|
||||
}
|
||||
@@ -112,6 +125,12 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
ShowAndSearchInternal( searchParameters.objectsToSearch, searchParameters );
|
||||
}
|
||||
|
||||
private static void CallSearchSelectedAssetReferencesOnce()
|
||||
{
|
||||
EditorApplication.update -= CallSearchSelectedAssetReferencesOnce;
|
||||
SearchSelectedAssetReferences( new MenuCommand( null ) );
|
||||
}
|
||||
|
||||
private static void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsageDetector.Parameters searchParameters )
|
||||
{
|
||||
if( visibleWindow != null && !visibleWindow.ReturnToSetupPhase( true ) )
|
||||
@@ -138,8 +157,9 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
visibleWindow.fieldModifiers = searchParameters.fieldModifiers;
|
||||
visibleWindow.propertyModifiers = searchParameters.propertyModifiers;
|
||||
visibleWindow.searchNonSerializableVariables = searchParameters.searchNonSerializableVariables;
|
||||
visibleWindow.lazySceneSearch = searchParameters.lazySceneSearch;
|
||||
visibleWindow.noAssetDatabaseChanges = searchParameters.noAssetDatabaseChanges;
|
||||
visibleWindow.showProgressBar = searchParameters.showProgressBar;
|
||||
visibleWindow.showDetailedProgressBar = searchParameters.showDetailedProgressBar;
|
||||
|
||||
visibleWindow.searchInAssetsSubset.Clear();
|
||||
if( searchParameters.searchInAssetsSubset != null )
|
||||
@@ -166,6 +186,11 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
visibleWindow.InitiateSearch();
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
LoadPrefs();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
visibleWindow = this;
|
||||
@@ -197,8 +222,9 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
EditorPrefs.SetInt( PREFS_SEARCH_PROPERTIES, (int) propertyModifiers );
|
||||
EditorPrefs.SetInt( PREFS_PATH_DRAWING_MODE, (int) searchResultDrawParameters.pathDrawingMode );
|
||||
EditorPrefs.SetBool( PREFS_SEARCH_NON_SERIALIZABLES, searchNonSerializableVariables );
|
||||
EditorPrefs.SetBool( PREFS_LAZY_SCENE_SEARCH, lazySceneSearch );
|
||||
EditorPrefs.SetBool( PREFS_SHOW_TOOLTIPS, searchResultDrawParameters.showTooltips );
|
||||
EditorPrefs.SetBool( PREFS_SHOW_PROGRESS, showProgressBar );
|
||||
EditorPrefs.SetBool( PREFS_SHOW_PROGRESS, showDetailedProgressBar );
|
||||
}
|
||||
|
||||
private void LoadPrefs()
|
||||
@@ -223,8 +249,9 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
}
|
||||
|
||||
searchNonSerializableVariables = EditorPrefs.GetBool( PREFS_SEARCH_NON_SERIALIZABLES, true );
|
||||
lazySceneSearch = EditorPrefs.GetBool( PREFS_LAZY_SCENE_SEARCH, false );
|
||||
searchResultDrawParameters.showTooltips = EditorPrefs.GetBool( PREFS_SHOW_TOOLTIPS, false );
|
||||
showProgressBar = EditorPrefs.GetBool( PREFS_SHOW_PROGRESS, false );
|
||||
showDetailedProgressBar = EditorPrefs.GetBool( PREFS_SHOW_PROGRESS, false );
|
||||
}
|
||||
|
||||
private SceneSearchMode GetSceneSearchMode( bool hideOptionsInPlayMode )
|
||||
@@ -279,7 +306,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
if( currentPhase == Phase.Processing )
|
||||
{
|
||||
// If we are stuck at this phase, then we have encountered an exception
|
||||
GUILayout.Label( ". . . Something went wrong, check console . . ." );
|
||||
GUILayout.Label( ". . . Search in progress or something went wrong (check console) . . ." );
|
||||
|
||||
restoreInitialSceneSetup = EditorGUILayout.ToggleLeft( "Restore initial scene setup (Recommended)", restoreInitialSceneSetup );
|
||||
|
||||
@@ -295,7 +322,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Box( "SEARCH IN", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
|
||||
searchInAssetsFolder = EditorGUILayout.ToggleLeft( "Project view (Assets folder)", searchInAssetsFolder );
|
||||
searchInAssetsFolder = EditorGUILayout.ToggleLeft( "Project window (Assets folder)", searchInAssetsFolder );
|
||||
|
||||
if( searchInAssetsFolder )
|
||||
{
|
||||
@@ -306,7 +333,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
searchInAssetsSubsetDrawer.Draw( searchInAssetsSubset );
|
||||
excludedAssetsDrawer.Draw( excludedAssets );
|
||||
|
||||
dontSearchInSourceAssets = EditorGUILayout.ToggleLeft( "Don't search 'Asset(s)' themselves for references", dontSearchInSourceAssets );
|
||||
dontSearchInSourceAssets = EditorGUILayout.ToggleLeft( "Don't search \"Find references of\" themselves for references", dontSearchInSourceAssets );
|
||||
|
||||
GUILayout.EndVertical();
|
||||
GUILayout.EndHorizontal();
|
||||
@@ -350,58 +377,66 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
GUILayout.Box( "SEARCH SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
//GUILayout.Box( "SEARCH SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
//GUILayout.BeginHorizontal();
|
||||
|
||||
GUILayout.Label( new GUIContent( "> Search depth: " + searchDepthLimit, "Depth limit for recursively searching variables of objects" ), Utilities.GL_WIDTH_250 );
|
||||
//GUILayout.Label( new GUIContent( "> Search depth: " + searchDepthLimit, "Depth limit for recursively searching variables of objects" ), Utilities.GL_WIDTH_250 );
|
||||
|
||||
searchDepthLimit = (int) GUILayout.HorizontalSlider( searchDepthLimit, 0, 4 );
|
||||
//searchDepthLimit = (int) GUILayout.HorizontalSlider( searchDepthLimit, 0, 4 );
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
//GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Label( "> Search variables:" );
|
||||
//GUILayout.Label( "> Search variables:" );
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
//GUILayout.BeginHorizontal();
|
||||
|
||||
GUILayout.Space( 35 );
|
||||
//GUILayout.Space( 35 );
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Public", ( fieldModifiers & BindingFlags.Public ) == BindingFlags.Public, Utilities.GL_WIDTH_100 ) )
|
||||
fieldModifiers |= BindingFlags.Public;
|
||||
else
|
||||
fieldModifiers &= ~BindingFlags.Public;
|
||||
//if( EditorGUILayout.ToggleLeft( "Public", ( fieldModifiers & BindingFlags.Public ) == BindingFlags.Public, Utilities.GL_WIDTH_100 ) )
|
||||
// fieldModifiers |= BindingFlags.Public;
|
||||
//else
|
||||
// fieldModifiers &= ~BindingFlags.Public;
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Non-public", ( fieldModifiers & BindingFlags.NonPublic ) == BindingFlags.NonPublic, Utilities.GL_WIDTH_100 ) )
|
||||
fieldModifiers |= BindingFlags.NonPublic;
|
||||
else
|
||||
fieldModifiers &= ~BindingFlags.NonPublic;
|
||||
//if( EditorGUILayout.ToggleLeft( "Non-public", ( fieldModifiers & BindingFlags.NonPublic ) == BindingFlags.NonPublic, Utilities.GL_WIDTH_100 ) )
|
||||
// fieldModifiers |= BindingFlags.NonPublic;
|
||||
//else
|
||||
// fieldModifiers &= ~BindingFlags.NonPublic;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
//GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Label( "> Search properties:" );
|
||||
//GUILayout.Label( "> Search properties:" );
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
//GUILayout.BeginHorizontal();
|
||||
|
||||
GUILayout.Space( 35 );
|
||||
//GUILayout.Space( 35 );
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Public", ( propertyModifiers & BindingFlags.Public ) == BindingFlags.Public, Utilities.GL_WIDTH_100 ) )
|
||||
propertyModifiers |= BindingFlags.Public;
|
||||
else
|
||||
propertyModifiers &= ~BindingFlags.Public;
|
||||
//if( EditorGUILayout.ToggleLeft( "Public", ( propertyModifiers & BindingFlags.Public ) == BindingFlags.Public, Utilities.GL_WIDTH_100 ) )
|
||||
// propertyModifiers |= BindingFlags.Public;
|
||||
//else
|
||||
// propertyModifiers &= ~BindingFlags.Public;
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Non-public", ( propertyModifiers & BindingFlags.NonPublic ) == BindingFlags.NonPublic, Utilities.GL_WIDTH_100 ) )
|
||||
propertyModifiers |= BindingFlags.NonPublic;
|
||||
else
|
||||
propertyModifiers &= ~BindingFlags.NonPublic;
|
||||
//if( EditorGUILayout.ToggleLeft( "Non-public", ( propertyModifiers & BindingFlags.NonPublic ) == BindingFlags.NonPublic, Utilities.GL_WIDTH_100 ) )
|
||||
// propertyModifiers |= BindingFlags.NonPublic;
|
||||
//else
|
||||
// propertyModifiers &= ~BindingFlags.NonPublic;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
//GUILayout.EndHorizontal();
|
||||
|
||||
//GUILayout.Space( 10 );
|
||||
|
||||
//searchNonSerializableVariables = EditorGUILayout.ToggleLeft( "Search non-serializable fields and properties", searchNonSerializableVariables );
|
||||
|
||||
//GUILayout.Space( 10 );
|
||||
|
||||
GUILayout.Box( "SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
|
||||
lazySceneSearch = EditorGUILayout.ToggleLeft( "Lazy scene search: scenes are searched in detail only when they are manually refreshed (faster search)", lazySceneSearch );
|
||||
noAssetDatabaseChanges = EditorGUILayout.ToggleLeft( "I haven't modified any assets/scenes since the last search (faster search)", noAssetDatabaseChanges );
|
||||
showDetailedProgressBar = EditorGUILayout.ToggleLeft( "Show detailed progress bar (slower search)", showDetailedProgressBar );
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
searchNonSerializableVariables = EditorGUILayout.ToggleLeft( "Include non-serializable fields and properties in search (slower search)", searchNonSerializableVariables );
|
||||
noAssetDatabaseChanges = EditorGUILayout.ToggleLeft( "I haven't modified any assets/scenes since the last search (faster search)", noAssetDatabaseChanges );
|
||||
showProgressBar = EditorGUILayout.ToggleLeft( "Show progress bar (slower search)", showProgressBar );
|
||||
|
||||
// Don't let the user press the GO button without any valid search location
|
||||
if( !searchInAllScenes && !searchInOpenScenes && !searchInScenesInBuild && !searchInAssetsFolder )
|
||||
GUI.enabled = false;
|
||||
@@ -437,35 +472,39 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
GUILayout.Box( "Don't forget to save scene(s) if you made any changes!", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
GUI.color = c;
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
if( searchResult.NumberOfGroups == 0 )
|
||||
GUILayout.Box( "No results found...", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
{
|
||||
GUILayout.Space( 10 );
|
||||
GUILayout.Box( "No references found...", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
}
|
||||
else
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
//GUILayout.Space( 10 );
|
||||
//GUILayout.BeginHorizontal();
|
||||
|
||||
// Select all the references after filtering them (select only the GameObject's)
|
||||
if( GUILayout.Button( "Select All\n(GameObject-wise)", Utilities.GL_HEIGHT_35 ) )
|
||||
{
|
||||
GameObject[] objects = searchResult.SelectAllAsGameObjects();
|
||||
if( objects != null && objects.Length > 0 )
|
||||
Selection.objects = objects;
|
||||
}
|
||||
//// Select all the references after filtering them (select only the GameObject's)
|
||||
//if( GUILayout.Button( "Select All\n(GameObject-wise)", Utilities.GL_HEIGHT_35 ) )
|
||||
//{
|
||||
// GameObject[] objects = searchResult.SelectAllAsGameObjects();
|
||||
// if( objects != null && objects.Length > 0 )
|
||||
// Selection.objects = objects;
|
||||
//}
|
||||
|
||||
// Select all the references without filtering them
|
||||
if( GUILayout.Button( "Select All\n(Object-wise)", Utilities.GL_HEIGHT_35 ) )
|
||||
{
|
||||
Object[] objects = searchResult.SelectAllAsObjects();
|
||||
if( objects != null && objects.Length > 0 )
|
||||
Selection.objects = objects;
|
||||
}
|
||||
//// Select all the references without filtering them
|
||||
//if( GUILayout.Button( "Select All\n(Object-wise)", Utilities.GL_HEIGHT_35 ) )
|
||||
//{
|
||||
// Object[] objects = searchResult.SelectAllAsObjects();
|
||||
// if( objects != null && objects.Length > 0 )
|
||||
// Selection.objects = objects;
|
||||
//}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
//GUILayout.EndHorizontal();
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
//GUILayout.Space( 10 );
|
||||
|
||||
searchResultDrawParameters.showTooltips = EditorGUILayout.ToggleLeft( "Show tooltips", searchResultDrawParameters.showTooltips );
|
||||
noAssetDatabaseChanges = EditorGUILayout.ToggleLeft( "I haven't modified any assets/scenes since the last search (faster Refresh)", noAssetDatabaseChanges );
|
||||
searchResultDrawParameters.noAssetDatabaseChanges = noAssetDatabaseChanges;
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
@@ -525,22 +564,45 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
SavePrefs();
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
// Try replacing prefab stage objects with their corresponding prefab assets
|
||||
var openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
||||
if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() )
|
||||
{
|
||||
GameObject openPrefabStagePrefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStage.prefabAssetPath );
|
||||
if( openPrefabStagePrefabAsset != null )
|
||||
{
|
||||
for( int i = 0; i < objectsToSearch.Count; i++ )
|
||||
{
|
||||
if( objectsToSearch[i].obj != null && !objectsToSearch[i].obj.Equals( null ) &&
|
||||
objectsToSearch[i].obj is GameObject && openPrefabStage.IsPartOfPrefabContents( (GameObject) objectsToSearch[i].obj ) )
|
||||
{
|
||||
GameObject prefabStageObjectSource = ( (GameObject) objectsToSearch[i].obj ).FollowSymmetricHierarchy( openPrefabStagePrefabAsset );
|
||||
if( prefabStageObjectSource != null )
|
||||
objectsToSearch[i].obj = prefabStageObjectSource;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
// Start searching
|
||||
searchResult = core.Run( new AssetUsageDetector.Parameters()
|
||||
{
|
||||
objectsToSearch = !objectsToSearch.IsEmpty() ? new ObjectToSearchEnumerator( objectsToSearch ) : null,
|
||||
objectsToSearch = new ObjectToSearchEnumerator( objectsToSearch ).ToArray(),
|
||||
searchInScenes = GetSceneSearchMode( true ),
|
||||
searchInAssetsFolder = searchInAssetsFolder,
|
||||
searchInAssetsSubset = !searchInAssetsSubset.IsEmpty() ? searchInAssetsSubset : null,
|
||||
excludedAssetsFromSearch = !excludedAssets.IsEmpty() ? excludedAssets : null,
|
||||
searchInAssetsSubset = !searchInAssetsSubset.IsEmpty() ? searchInAssetsSubset.ToArray() : null,
|
||||
excludedAssetsFromSearch = !excludedAssets.IsEmpty() ? excludedAssets.ToArray() : null,
|
||||
dontSearchInSourceAssets = dontSearchInSourceAssets,
|
||||
excludedScenesFromSearch = !excludedScenes.IsEmpty() ? excludedScenes : null,
|
||||
fieldModifiers = fieldModifiers,
|
||||
propertyModifiers = propertyModifiers,
|
||||
searchDepthLimit = searchDepthLimit,
|
||||
searchNonSerializableVariables = searchNonSerializableVariables,
|
||||
excludedScenesFromSearch = !excludedScenes.IsEmpty() ? excludedScenes.ToArray() : null,
|
||||
//fieldModifiers = fieldModifiers,
|
||||
//propertyModifiers = propertyModifiers,
|
||||
//searchDepthLimit = searchDepthLimit,
|
||||
//searchNonSerializableVariables = searchNonSerializableVariables,
|
||||
lazySceneSearch = lazySceneSearch,
|
||||
noAssetDatabaseChanges = noAssetDatabaseChanges,
|
||||
showProgressBar = showProgressBar
|
||||
showDetailedProgressBar = showDetailedProgressBar
|
||||
} );
|
||||
|
||||
currentPhase = Phase.Complete;
|
||||
|
||||
@@ -112,5 +112,19 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
public Object[] ToArray()
|
||||
{
|
||||
int count = 0;
|
||||
foreach( Object obj in this )
|
||||
count++;
|
||||
|
||||
Object[] result = new Object[count];
|
||||
int index = 0;
|
||||
foreach( Object obj in this )
|
||||
result[index++] = obj;
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,8 +46,22 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
{
|
||||
if( draggedObjects[i] != null && !draggedObjects[i].Equals( null ) )
|
||||
{
|
||||
bool replacedNullElement = false;
|
||||
for( int j = 0; j < list.Count; j++ )
|
||||
{
|
||||
if( IsElementNull( list[j] ) )
|
||||
{
|
||||
list[j] = CreateElement( draggedObjects[i] );
|
||||
|
||||
replacedNullElement = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( !replacedNullElement )
|
||||
list.Add( CreateElement( draggedObjects[i] ) );
|
||||
|
||||
hasChanged = true;
|
||||
list.Add( CreateElement( draggedObjects[i] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -107,6 +121,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
protected abstract T CreateElement( Object source );
|
||||
protected abstract Object GetObjectFromElement( T element );
|
||||
protected abstract void SetObjectOfElement( List<T> list, int index, Object value );
|
||||
protected abstract bool IsElementNull( T element );
|
||||
protected abstract void PostElementDrawer( T element );
|
||||
}
|
||||
|
||||
@@ -131,6 +146,11 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
list[index] = value;
|
||||
}
|
||||
|
||||
protected override bool IsElementNull( Object element )
|
||||
{
|
||||
return element == null || element.Equals( null );
|
||||
}
|
||||
|
||||
protected override void PostElementDrawer( Object element )
|
||||
{
|
||||
}
|
||||
@@ -138,7 +158,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
public class ObjectToSearchListDrawer : ListDrawer<ObjectToSearch>
|
||||
{
|
||||
public ObjectToSearchListDrawer() : base( "Asset(s):", true )
|
||||
public ObjectToSearchListDrawer() : base( "Find references of:", true )
|
||||
{
|
||||
}
|
||||
|
||||
@@ -158,6 +178,11 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
list[index].RefreshSubAssets();
|
||||
}
|
||||
|
||||
protected override bool IsElementNull( ObjectToSearch element )
|
||||
{
|
||||
return element == null || element.obj == null || element.obj.Equals( null );
|
||||
}
|
||||
|
||||
protected override void PostElementDrawer( ObjectToSearch element )
|
||||
{
|
||||
List<ObjectToSearch.SubAsset> subAssetsToSearch = element.subAssets;
|
||||
|
||||
@@ -11,14 +11,17 @@ namespace AssetUsageDetectorNamespace
|
||||
[Serializable]
|
||||
public class SearchResultDrawParameters
|
||||
{
|
||||
public SearchResult searchResult;
|
||||
public PathDrawingMode pathDrawingMode;
|
||||
public bool showTooltips;
|
||||
public bool noAssetDatabaseChanges;
|
||||
public string tooltip;
|
||||
|
||||
public SearchResultDrawParameters( PathDrawingMode pathDrawingMode, bool showTooltips )
|
||||
public SearchResultDrawParameters( PathDrawingMode pathDrawingMode, bool showTooltips, bool noAssetDatabaseChanges )
|
||||
{
|
||||
this.pathDrawingMode = pathDrawingMode;
|
||||
this.showTooltips = showTooltips;
|
||||
this.noAssetDatabaseChanges = noAssetDatabaseChanges;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,7 +34,9 @@ namespace AssetUsageDetectorNamespace
|
||||
public class SerializableResultGroup
|
||||
{
|
||||
public string title;
|
||||
public bool clickable;
|
||||
public SearchResultGroup.GroupType type;
|
||||
public bool isExpanded;
|
||||
public bool pendingSearch;
|
||||
|
||||
public List<int> initialSerializedNodes;
|
||||
}
|
||||
@@ -51,6 +56,9 @@ namespace AssetUsageDetectorNamespace
|
||||
private List<SearchResultGroup> result;
|
||||
private SceneSetup[] initialSceneSetup;
|
||||
|
||||
private AssetUsageDetector searchHandler;
|
||||
private AssetUsageDetector.Parameters m_searchParameters;
|
||||
|
||||
private List<SerializableNode> serializedNodes;
|
||||
private List<SerializableResultGroup> serializedGroups;
|
||||
|
||||
@@ -59,8 +67,9 @@ namespace AssetUsageDetectorNamespace
|
||||
|
||||
public bool SearchCompletedSuccessfully { get { return success; } }
|
||||
public bool InitialSceneSetupConfigured { get { return initialSceneSetup != null && initialSceneSetup.Length > 0; } }
|
||||
public AssetUsageDetector.Parameters SearchParameters { get { return m_searchParameters; } }
|
||||
|
||||
public SearchResult( bool success, List<SearchResultGroup> result, SceneSetup[] initialSceneSetup )
|
||||
public SearchResult( bool success, List<SearchResultGroup> result, SceneSetup[] initialSceneSetup, AssetUsageDetector searchHandler, AssetUsageDetector.Parameters searchParameters )
|
||||
{
|
||||
if( result == null )
|
||||
result = new List<SearchResultGroup>( 0 );
|
||||
@@ -68,10 +77,107 @@ namespace AssetUsageDetectorNamespace
|
||||
this.success = success;
|
||||
this.result = result;
|
||||
this.initialSceneSetup = initialSceneSetup;
|
||||
this.searchHandler = searchHandler;
|
||||
this.m_searchParameters = searchParameters;
|
||||
}
|
||||
|
||||
public void RefreshSearchResultGroup( SearchResultGroup searchResultGroup, bool noAssetDatabaseChanges )
|
||||
{
|
||||
if( searchResultGroup == null )
|
||||
{
|
||||
Debug.LogError( "SearchResultGroup is null!" );
|
||||
return;
|
||||
}
|
||||
|
||||
int searchResultGroupIndex = -1;
|
||||
for( int i = 0; i < result.Count; i++ )
|
||||
{
|
||||
if( result[i] == searchResultGroup )
|
||||
{
|
||||
searchResultGroupIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( searchResultGroupIndex < 0 )
|
||||
{
|
||||
Debug.LogError( "SearchResultGroup is not a part of SearchResult!" );
|
||||
return;
|
||||
}
|
||||
|
||||
if( searchResultGroup.Type == SearchResultGroup.GroupType.Scene && EditorApplication.isPlaying && !EditorSceneManager.GetSceneByPath( searchResultGroup.Title ).isLoaded )
|
||||
{
|
||||
Debug.LogError( "Can't search unloaded scene while in Play Mode!" );
|
||||
return;
|
||||
}
|
||||
|
||||
if( searchHandler == null )
|
||||
searchHandler = new AssetUsageDetector();
|
||||
|
||||
SceneSearchMode searchInScenes = m_searchParameters.searchInScenes;
|
||||
Object[] searchInScenesSubset = m_searchParameters.searchInScenesSubset;
|
||||
bool searchInAssetsFolder = m_searchParameters.searchInAssetsFolder;
|
||||
Object[] searchInAssetsSubset = m_searchParameters.searchInAssetsSubset;
|
||||
|
||||
try
|
||||
{
|
||||
if( searchResultGroup.Type == SearchResultGroup.GroupType.Assets )
|
||||
{
|
||||
m_searchParameters.searchInScenes = SceneSearchMode.None;
|
||||
m_searchParameters.searchInScenesSubset = null;
|
||||
}
|
||||
else if( searchResultGroup.Type == SearchResultGroup.GroupType.Scene )
|
||||
{
|
||||
m_searchParameters.searchInScenes = SceneSearchMode.None;
|
||||
m_searchParameters.searchInScenesSubset = new Object[1] { AssetDatabase.LoadAssetAtPath<SceneAsset>( searchResultGroup.Title ) };
|
||||
m_searchParameters.searchInAssetsFolder = false;
|
||||
m_searchParameters.searchInAssetsSubset = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_searchParameters.searchInScenes = (SceneSearchMode) 1024; // A unique value to search only the DontDestroyOnLoad scene
|
||||
m_searchParameters.searchInScenesSubset = null;
|
||||
m_searchParameters.searchInAssetsFolder = false;
|
||||
m_searchParameters.searchInAssetsSubset = null;
|
||||
}
|
||||
|
||||
m_searchParameters.lazySceneSearch = false;
|
||||
m_searchParameters.noAssetDatabaseChanges = noAssetDatabaseChanges;
|
||||
|
||||
// Make sure the AssetDatabase is up-to-date
|
||||
AssetDatabase.SaveAssets();
|
||||
|
||||
List<SearchResultGroup> searchResult = searchHandler.Run( m_searchParameters ).result;
|
||||
if( searchResult != null )
|
||||
{
|
||||
int newSearchResultGroupIndex = -1;
|
||||
for( int i = 0; i < searchResult.Count; i++ )
|
||||
{
|
||||
if( searchResultGroup.Title == searchResult[i].Title )
|
||||
{
|
||||
newSearchResultGroupIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( newSearchResultGroupIndex >= 0 )
|
||||
result[searchResultGroupIndex] = searchResult[newSearchResultGroupIndex];
|
||||
else
|
||||
searchResultGroup.Clear();
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
m_searchParameters.searchInScenes = searchInScenes;
|
||||
m_searchParameters.searchInScenesSubset = searchInScenesSubset;
|
||||
m_searchParameters.searchInAssetsFolder = searchInAssetsFolder;
|
||||
m_searchParameters.searchInAssetsSubset = searchInAssetsSubset;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawOnGUI( SearchResultDrawParameters parameters )
|
||||
{
|
||||
parameters.searchResult = this;
|
||||
parameters.tooltip = null;
|
||||
|
||||
for( int i = 0; i < result.Count; i++ )
|
||||
@@ -232,7 +338,7 @@ namespace AssetUsageDetectorNamespace
|
||||
|
||||
for( int i = 0; i < serializedGroups.Count; i++ )
|
||||
{
|
||||
result.Add( new SearchResultGroup( serializedGroups[i].title, serializedGroups[i].clickable ) );
|
||||
result.Add( new SearchResultGroup( serializedGroups[i].title, serializedGroups[i].type, serializedGroups[i].isExpanded, serializedGroups[i].pendingSearch ) );
|
||||
result[i].Deserialize( serializedGroups[i], allNodes );
|
||||
}
|
||||
|
||||
@@ -244,6 +350,8 @@ namespace AssetUsageDetectorNamespace
|
||||
// Custom class to hold the results for a single scene or Assets folder
|
||||
public class SearchResultGroup
|
||||
{
|
||||
public enum GroupType { Assets = 0, Scene = 1, DontDestroyOnLoad = 2 };
|
||||
|
||||
// Custom struct to hold a single path to a reference
|
||||
public struct ReferencePath
|
||||
{
|
||||
@@ -257,8 +365,10 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
}
|
||||
|
||||
private readonly string title;
|
||||
private readonly bool clickable;
|
||||
public string Title { get; private set; }
|
||||
public GroupType Type { get; private set; }
|
||||
public bool IsExpanded { get; private set; }
|
||||
public bool PendingSearch { get; private set; }
|
||||
|
||||
private readonly List<ReferenceNode> references;
|
||||
private List<ReferencePath> referencePathsShortUnique;
|
||||
@@ -267,10 +377,12 @@ namespace AssetUsageDetectorNamespace
|
||||
public int NumberOfReferences { get { return references.Count; } }
|
||||
public ReferenceNode this[int index] { get { return references[index]; } }
|
||||
|
||||
public SearchResultGroup( string title, bool clickable )
|
||||
public SearchResultGroup( string title, GroupType type, bool isExpanded = true, bool pendingSearch = false )
|
||||
{
|
||||
this.title = title;
|
||||
this.clickable = clickable;
|
||||
Title = title;
|
||||
Type = type;
|
||||
IsExpanded = isExpanded;
|
||||
PendingSearch = pendingSearch;
|
||||
|
||||
references = new List<ReferenceNode>();
|
||||
referencePathsShortUnique = null;
|
||||
@@ -283,6 +395,17 @@ namespace AssetUsageDetectorNamespace
|
||||
references.Add( node );
|
||||
}
|
||||
|
||||
// Removes all nodes
|
||||
public void Clear()
|
||||
{
|
||||
references.Clear();
|
||||
|
||||
if( referencePathsShortUnique != null )
|
||||
referencePathsShortUnique.Clear();
|
||||
if( referencePathsShortest != null )
|
||||
referencePathsShortest.Clear();
|
||||
}
|
||||
|
||||
// Initializes commonly used variables of the nodes
|
||||
public void InitializeNodes( Func<object, ReferenceNode> nodeGetter )
|
||||
{
|
||||
@@ -368,6 +491,9 @@ namespace AssetUsageDetectorNamespace
|
||||
// Add all the Object's in this container to the set
|
||||
public void AddObjectsTo( HashSet<Object> objectsSet )
|
||||
{
|
||||
if( PendingSearch )
|
||||
return;
|
||||
|
||||
CalculateShortestPathsToReferences();
|
||||
|
||||
for( int i = 0; i < referencePathsShortUnique.Count; i++ )
|
||||
@@ -381,6 +507,9 @@ namespace AssetUsageDetectorNamespace
|
||||
// Add all the GameObject's in this container to the set
|
||||
public void AddGameObjectsTo( HashSet<GameObject> gameObjectsSet )
|
||||
{
|
||||
if( PendingSearch )
|
||||
return;
|
||||
|
||||
CalculateShortestPathsToReferences();
|
||||
|
||||
for( int i = 0; i < referencePathsShortUnique.Count; i++ )
|
||||
@@ -397,7 +526,7 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Calculate short unique paths to the references
|
||||
public void CalculateShortestPathsToReferences()
|
||||
private void CalculateShortestPathsToReferences()
|
||||
{
|
||||
if( referencePathsShortUnique != null )
|
||||
return;
|
||||
@@ -439,51 +568,83 @@ namespace AssetUsageDetectorNamespace
|
||||
Color c = GUI.color;
|
||||
GUI.color = Color.cyan;
|
||||
|
||||
if( GUILayout.Button( title, Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH, Utilities.GL_HEIGHT_40 ) && clickable )
|
||||
Rect rect = EditorGUILayout.GetControlRect( Utilities.GL_EXPAND_WIDTH, Utilities.GL_HEIGHT_40 );
|
||||
float width = rect.width;
|
||||
rect.width = 40f;
|
||||
if( GUI.Button( rect, IsExpanded ? "v" : ">" ) )
|
||||
{
|
||||
IsExpanded = !IsExpanded;
|
||||
GUIUtility.ExitGUI();
|
||||
}
|
||||
|
||||
rect.x += 40f;
|
||||
rect.width = width - ( parameters.searchResult != null ? 140f : 40f );
|
||||
if( GUI.Button( rect, Title, Utilities.BoxGUIStyle ) && Type == GroupType.Scene )
|
||||
{
|
||||
// If the container (scene, usually) is clicked, highlight it on Project view
|
||||
AssetDatabase.LoadAssetAtPath<SceneAsset>( title ).SelectInEditor();
|
||||
AssetDatabase.LoadAssetAtPath<SceneAsset>( Title ).SelectInEditor();
|
||||
}
|
||||
|
||||
GUI.color = Color.yellow;
|
||||
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.Full )
|
||||
if( parameters.searchResult != null )
|
||||
{
|
||||
for( int i = 0; i < references.Count; i++ )
|
||||
rect.x += width - 140f;
|
||||
rect.width = 100f;
|
||||
if( GUI.Button( rect, "Refresh" ) )
|
||||
{
|
||||
GUILayout.Space( 5 );
|
||||
references[i].DrawOnGUIRecursively( parameters, null );
|
||||
parameters.searchResult.RefreshSearchResultGroup( this, parameters.noAssetDatabaseChanges );
|
||||
GUIUtility.ExitGUI();
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
if( IsExpanded )
|
||||
{
|
||||
if( referencePathsShortUnique == null )
|
||||
CalculateShortestPathsToReferences();
|
||||
GUI.color = Color.yellow;
|
||||
|
||||
List<ReferencePath> pathsToDraw;
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.ShortRelevantParts )
|
||||
pathsToDraw = referencePathsShortUnique;
|
||||
if( PendingSearch )
|
||||
GUILayout.Box( "Lazy Search: this scene potentially has some references, hit Refresh to find them", Utilities.BoxGUIStyle );
|
||||
else if( references.Count == 0 )
|
||||
GUILayout.Box( "No references found...", Utilities.BoxGUIStyle );
|
||||
else
|
||||
pathsToDraw = referencePathsShortest;
|
||||
|
||||
for( int i = 0; i < pathsToDraw.Count; i++ )
|
||||
{
|
||||
GUILayout.Space( 5 );
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
ReferencePath path = pathsToDraw[i];
|
||||
path.startNode.DrawOnGUI( parameters, null );
|
||||
|
||||
ReferenceNode currentNode = path.startNode;
|
||||
for( int j = 0; j < path.pathLinksToFollow.Length; j++ )
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.Full )
|
||||
{
|
||||
ReferenceNode.Link link = currentNode[path.pathLinksToFollow[j]];
|
||||
link.targetNode.DrawOnGUI( parameters, link.description );
|
||||
currentNode = link.targetNode;
|
||||
for( int i = 0; i < references.Count; i++ )
|
||||
{
|
||||
GUILayout.Space( 5 );
|
||||
references[i].DrawOnGUIRecursively( parameters, null );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( referencePathsShortUnique == null )
|
||||
CalculateShortestPathsToReferences();
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
List<ReferencePath> pathsToDraw;
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.ShortRelevantParts )
|
||||
pathsToDraw = referencePathsShortUnique;
|
||||
else
|
||||
pathsToDraw = referencePathsShortest;
|
||||
|
||||
for( int i = 0; i < pathsToDraw.Count; i++ )
|
||||
{
|
||||
GUILayout.Space( 5 );
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
ReferencePath path = pathsToDraw[i];
|
||||
path.startNode.DrawOnGUI( parameters, null );
|
||||
|
||||
ReferenceNode currentNode = path.startNode;
|
||||
for( int j = 0; j < path.pathLinksToFollow.Length; j++ )
|
||||
{
|
||||
ReferenceNode.Link link = currentNode[path.pathLinksToFollow[j]];
|
||||
link.targetNode.DrawOnGUI( parameters, link.description );
|
||||
currentNode = link.targetNode;
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -497,8 +658,10 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
SearchResult.SerializableResultGroup serializedResultGroup = new SearchResult.SerializableResultGroup()
|
||||
{
|
||||
title = title,
|
||||
clickable = clickable
|
||||
title = Title,
|
||||
type = Type,
|
||||
isExpanded = IsExpanded,
|
||||
pendingSearch = PendingSearch
|
||||
};
|
||||
|
||||
if( references != null )
|
||||
|
||||
@@ -12,26 +12,37 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
public static class Utilities
|
||||
{
|
||||
// A set of commonly used Unity types
|
||||
private static readonly HashSet<Type> primitiveUnityTypes = new HashSet<Type>() {
|
||||
typeof( string ), typeof( Vector4 ), typeof( Vector3 ), typeof( Vector2 ), typeof( Rect ),
|
||||
typeof( Quaternion ), typeof( Color ), typeof( Color32 ), typeof( LayerMask ),
|
||||
typeof( Matrix4x4 ), typeof( AnimationCurve ), typeof( Gradient ), typeof( RectOffset ),
|
||||
typeof( bool[] ), typeof( byte[] ), typeof( sbyte[] ), typeof( char[] ), typeof( decimal[] ),
|
||||
typeof( double[] ), typeof( float[] ), typeof( int[] ), typeof( uint[] ), typeof( long[] ),
|
||||
typeof( ulong[] ), typeof( short[] ), typeof( ushort[] ), typeof( string[] ),
|
||||
typeof( Vector4[] ), typeof( Vector3[] ), typeof( Vector2[] ), typeof( Rect[] ),
|
||||
typeof( Quaternion[] ), typeof( Color[] ), typeof( Color32[] ), typeof( LayerMask[] ),
|
||||
typeof( Matrix4x4[] ), typeof( AnimationCurve[] ), typeof( Gradient[] ), typeof( RectOffset[] ),
|
||||
typeof( List<bool> ), typeof( List<byte> ), typeof( List<sbyte> ), typeof( List<char> ), typeof( List<decimal> ),
|
||||
typeof( List<double> ), typeof( List<float> ), typeof( List<int> ), typeof( List<uint> ), typeof( List<long> ),
|
||||
typeof( List<ulong> ), typeof( List<short> ), typeof( List<ushort> ), typeof( List<string> ),
|
||||
typeof( List<Vector4> ), typeof( List<Vector3> ), typeof( List<Vector2> ), typeof( List<Rect> ),
|
||||
typeof( List<Quaternion> ), typeof( List<Color> ), typeof( List<Color32> ), typeof( List<LayerMask> ),
|
||||
typeof( List<Matrix4x4> ), typeof( List<AnimationCurve> ), typeof( List<Gradient> ), typeof( List<RectOffset> )
|
||||
private static readonly HashSet<Type> primitiveUnityTypes = new HashSet<Type>()
|
||||
{
|
||||
typeof( string ), typeof( Vector4 ), typeof( Vector3 ), typeof( Vector2 ), typeof( Rect ),
|
||||
typeof( Quaternion ), typeof( Color ), typeof( Color32 ), typeof( LayerMask ), typeof( Bounds ),
|
||||
typeof( Matrix4x4 ), typeof( AnimationCurve ), typeof( Gradient ), typeof( RectOffset ),
|
||||
typeof( bool[] ), typeof( byte[] ), typeof( sbyte[] ), typeof( char[] ), typeof( decimal[] ),
|
||||
typeof( double[] ), typeof( float[] ), typeof( int[] ), typeof( uint[] ), typeof( long[] ),
|
||||
typeof( ulong[] ), typeof( short[] ), typeof( ushort[] ), typeof( string[] ),
|
||||
typeof( Vector4[] ), typeof( Vector3[] ), typeof( Vector2[] ), typeof( Rect[] ),
|
||||
typeof( Quaternion[] ), typeof( Color[] ), typeof( Color32[] ), typeof( LayerMask[] ), typeof( Bounds[] ),
|
||||
typeof( Matrix4x4[] ), typeof( AnimationCurve[] ), typeof( Gradient[] ), typeof( RectOffset[] ),
|
||||
typeof( List<bool> ), typeof( List<byte> ), typeof( List<sbyte> ), typeof( List<char> ), typeof( List<decimal> ),
|
||||
typeof( List<double> ), typeof( List<float> ), typeof( List<int> ), typeof( List<uint> ), typeof( List<long> ),
|
||||
typeof( List<ulong> ), typeof( List<short> ), typeof( List<ushort> ), typeof( List<string> ),
|
||||
typeof( List<Vector4> ), typeof( List<Vector3> ), typeof( List<Vector2> ), typeof( List<Rect> ),
|
||||
typeof( List<Quaternion> ), typeof( List<Color> ), typeof( List<Color32> ), typeof( List<LayerMask> ), typeof( List<Bounds> ),
|
||||
typeof( List<Matrix4x4> ), typeof( List<AnimationCurve> ), typeof( List<Gradient> ), typeof( List<RectOffset> ),
|
||||
#if UNITY_2017_2_OR_NEWER
|
||||
typeof( Vector3Int ), typeof( Vector2Int ), typeof( RectInt ), typeof( BoundsInt ),
|
||||
typeof( Vector3Int[] ), typeof( Vector2Int[] ), typeof( RectInt[] ), typeof( BoundsInt[] ),
|
||||
typeof( List<Vector3Int> ), typeof( List<Vector2Int> ), typeof( List<RectInt> ), typeof( List<BoundsInt> )
|
||||
#endif
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> folderContentsSet = new HashSet<string>();
|
||||
|
||||
#if UNITY_2018_3_OR_NEWER
|
||||
private static int previousPingedPrefabInstanceId;
|
||||
private static double previousPingedPrefabPingTime;
|
||||
#endif
|
||||
|
||||
public static readonly GUILayoutOption GL_EXPAND_WIDTH = GUILayout.ExpandWidth( true );
|
||||
public static readonly GUILayoutOption GL_EXPAND_HEIGHT = GUILayout.ExpandHeight( true );
|
||||
public static readonly GUILayoutOption GL_WIDTH_25 = GUILayout.Width( 25 );
|
||||
@@ -142,9 +153,9 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
Event e = Event.current;
|
||||
|
||||
// If CTRL key is pressed, do a multi-select;
|
||||
// If CTRL or Shift keys are pressed, do a multi-select;
|
||||
// otherwise select only the clicked object and ping it in editor
|
||||
if( !e.control )
|
||||
if( !e.control && !e.shift )
|
||||
Selection.activeObject = obj.PingInEditor();
|
||||
else
|
||||
{
|
||||
@@ -211,23 +222,26 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
{
|
||||
string assetPath = AssetDatabase.GetAssetPath( objTR.gameObject );
|
||||
var openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
||||
|
||||
// Try to open the prefab stage of pinged prefabs if they are double clicked
|
||||
if( previousPingedPrefabInstanceId == objTR.GetInstanceID() && EditorApplication.timeSinceStartup - previousPingedPrefabPingTime <= 0.3f &&
|
||||
( openPrefabStage == null || !openPrefabStage.stageHandle.IsValid() || assetPath != openPrefabStage.prefabAssetPath ) )
|
||||
{
|
||||
AssetDatabase.OpenAsset( objTR.gameObject );
|
||||
openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
||||
}
|
||||
|
||||
previousPingedPrefabInstanceId = objTR.GetInstanceID();
|
||||
previousPingedPrefabPingTime = EditorApplication.timeSinceStartup;
|
||||
|
||||
if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() && assetPath == openPrefabStage.prefabAssetPath )
|
||||
{
|
||||
// Ping the object in prefab stage
|
||||
Transform targetParent = objTR;
|
||||
Transform selectedObject = ( (GameObject) obj ).transform;
|
||||
objTR = openPrefabStage.prefabContentsRoot.transform;
|
||||
while( targetParent != selectedObject )
|
||||
GameObject prefabStageGO = FollowSymmetricHierarchy( (GameObject) obj, openPrefabStage.prefabContentsRoot );
|
||||
if( prefabStageGO != null )
|
||||
{
|
||||
Transform temp = selectedObject;
|
||||
while( temp.parent != targetParent )
|
||||
temp = temp.parent;
|
||||
|
||||
objTR = objTR.GetChild( temp.GetSiblingIndex() );
|
||||
targetParent = temp;
|
||||
objTR = prefabStageGO.transform;
|
||||
selection = objTR.gameObject;
|
||||
}
|
||||
|
||||
selection = objTR.gameObject;
|
||||
}
|
||||
#if UNITY_2019_1_OR_NEWER
|
||||
else if( obj != objTR.gameObject )
|
||||
@@ -253,6 +267,38 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
return selection;
|
||||
}
|
||||
|
||||
public static GameObject FollowSymmetricHierarchy( this GameObject go, GameObject symmetricRoot )
|
||||
{
|
||||
Transform target = go.transform;
|
||||
Transform root1 = target.root;
|
||||
Transform root2 = symmetricRoot.transform;
|
||||
while( root1 != target )
|
||||
{
|
||||
Transform temp = target;
|
||||
while( temp.parent != root1 )
|
||||
temp = temp.parent;
|
||||
|
||||
Transform newRoot2;
|
||||
int siblingIndex = temp.GetSiblingIndex();
|
||||
if( siblingIndex < root2.childCount )
|
||||
{
|
||||
newRoot2 = root2.GetChild( siblingIndex );
|
||||
if( newRoot2.name != temp.name )
|
||||
newRoot2 = root2.Find( temp.name );
|
||||
}
|
||||
else
|
||||
newRoot2 = root2.Find( temp.name );
|
||||
|
||||
if( newRoot2 == null )
|
||||
return null;
|
||||
|
||||
root2 = newRoot2;
|
||||
root1 = temp;
|
||||
}
|
||||
|
||||
return root2.gameObject;
|
||||
}
|
||||
|
||||
// Check if the field is serializable
|
||||
public static bool IsSerializable( this FieldInfo fieldInfo )
|
||||
{
|
||||
@@ -310,13 +356,6 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
return type.IsPrimitive || primitiveUnityTypes.Contains( type ) || type.IsEnum;
|
||||
}
|
||||
|
||||
// Check if property has override keyword
|
||||
public static bool IsOverridden( this PropertyInfo propertyInfo )
|
||||
{
|
||||
MethodInfo mi = propertyInfo.GetGetMethod( true );
|
||||
return mi.GetBaseDefinition().DeclaringType != mi.DeclaringType;
|
||||
}
|
||||
|
||||
// Get <get> function for a field
|
||||
public static VariableGetVal CreateGetter( this FieldInfo fieldInfo, Type type )
|
||||
{
|
||||
@@ -339,22 +378,12 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
// Get <get> function for a property
|
||||
public static VariableGetVal CreateGetter( this PropertyInfo propertyInfo )
|
||||
{
|
||||
// Ignore indexer properties
|
||||
if( propertyInfo.GetIndexParameters().Length > 0 )
|
||||
return null;
|
||||
|
||||
// Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs
|
||||
if( propertyInfo.DeclaringType.IsValueType )
|
||||
return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null;
|
||||
|
||||
MethodInfo mi = propertyInfo.GetGetMethod( true );
|
||||
if( mi != null )
|
||||
{
|
||||
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
|
||||
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, mi ) ).GetValue;
|
||||
}
|
||||
|
||||
return null;
|
||||
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
|
||||
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
|
||||
}
|
||||
|
||||
// Check if all open scenes are saved (not dirty)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
|
||||
namespace AssetUsageDetectorNamespace.Extras
|
||||
{
|
||||
@@ -32,7 +33,15 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
public object Get( object obj )
|
||||
{
|
||||
return getter( obj );
|
||||
try
|
||||
{
|
||||
return getter( obj );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
Debug.LogException( e );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ E-mail: yasirkula@gmail.com
|
||||
This tool helps you find usages of the selected asset(s) and/or Object(s) in your Unity project, i.e. lists the objects that refer to them.
|
||||
|
||||
2. HOW TO
|
||||
Open "Window-Asset Usage Detector" window, configure the settings and hit GO! You can also use the "Assets-Search For References" shortcut to find the selected object(s)' references.
|
||||
Open "Window-Asset Usage Detector" window, configure the settings and hit GO! You can also right click an object and select "Search For References".
|
||||
|
||||
3. KNOWN LIMITATIONS
|
||||
- static variables are not searched
|
||||
|
||||
@@ -14,14 +14,12 @@ This editor extension helps you figure out at which places an asset or GameObjec
|
||||
|
||||
- Import [AssetUsageDetector.unitypackage](https://github.com/yasirkula/UnityAssetUsageDetector/releases) to your project
|
||||
- Open **Window - Asset Usage Detector** window, configure the settings and hit **GO!**
|
||||
- or, use the **Assets - Search For References** shortcut to find the selected object(s)' references
|
||||
|
||||
**NOTE:** If your project uses an older version of AssetUsageDetector, delete the older version before updating the plugin.
|
||||
- or, right click an object and select **Search For References**
|
||||
|
||||
### C. FEATURES
|
||||
|
||||
- You can search for references of any object that extends *UnityEngine.Object*
|
||||
- Seaches every corner of your project with its *reflection* based search algorithm (even non-Unity objects, structs and data types like dictionaries are searched)
|
||||
- Extensive search algorithm that uses *SerializedObject* and *reflection* to find references
|
||||
- Can search in multiple scenes at once
|
||||
- Can show complete paths to the references or only the most relevant parts of the paths (see the demonstration below)
|
||||
|
||||
@@ -31,4 +29,4 @@ This editor extension helps you figure out at which places an asset or GameObjec
|
||||
|
||||
- *static* variables are not searched
|
||||
- GUIText materials are not searched
|
||||
- Textures in Lens Flare's can not be searched
|
||||
- Textures in Lens Flare's can not be searched
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 40 KiB |
Reference in New Issue
Block a user