mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-19 22:42:37 +08:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ff193224e9 | |||
| 51abec24d7 | |||
| a29dc59a75 | |||
| 96315ec435 | |||
| 12ed06d43c |
File diff suppressed because it is too large
Load Diff
@@ -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()
|
||||
@@ -206,7 +232,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
ParseSceneSearchMode( (SceneSearchMode) EditorPrefs.GetInt( PREFS_SEARCH_SCENES, (int) ( SceneSearchMode.OpenScenes | SceneSearchMode.ScenesInBuildSettingsTickedOnly | SceneSearchMode.AllScenes ) ) );
|
||||
|
||||
searchInAssetsFolder = EditorPrefs.GetBool( PREFS_SEARCH_ASSETS, true );
|
||||
dontSearchInSourceAssets = EditorPrefs.GetBool( PREFS_DONT_SEARCH_SOURCE_ASSETS, false );
|
||||
dontSearchInSourceAssets = EditorPrefs.GetBool( PREFS_DONT_SEARCH_SOURCE_ASSETS, true );
|
||||
searchDepthLimit = EditorPrefs.GetInt( PREFS_SEARCH_DEPTH_LIMIT, 4 );
|
||||
|
||||
// Fetch public, protected and private non-static fields and properties from objects by default
|
||||
@@ -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 )
|
||||
{
|
||||
@@ -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 );
|
||||
|
||||
@@ -475,7 +514,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Space( 35 );
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Full: Draw the complete paths to the references (can be slow with too many references)", searchResultDrawParameters.pathDrawingMode == PathDrawingMode.Full ) )
|
||||
if( EditorGUILayout.ToggleLeft( "Full: Draw the complete paths to the references", searchResultDrawParameters.pathDrawingMode == PathDrawingMode.Full ) )
|
||||
searchResultDrawParameters.pathDrawingMode = PathDrawingMode.Full;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
@@ -484,7 +523,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Space( 35 );
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Shorter: Draw only the most relevant unique parts of the complete paths that start with a UnityEngine.Object", searchResultDrawParameters.pathDrawingMode == PathDrawingMode.ShortRelevantParts ) )
|
||||
if( EditorGUILayout.ToggleLeft( "Shorter: Draw only the most relevant parts (that start with a UnityEngine.Object) of the complete paths", searchResultDrawParameters.pathDrawingMode == PathDrawingMode.ShortRelevantParts ) )
|
||||
searchResultDrawParameters.pathDrawingMode = PathDrawingMode.ShortRelevantParts;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
@@ -493,7 +532,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Space( 35 );
|
||||
|
||||
if( EditorGUILayout.ToggleLeft( "Shortest: Draw only the last two nodes of complete paths that are unique", searchResultDrawParameters.pathDrawingMode == PathDrawingMode.Shortest ) )
|
||||
if( EditorGUILayout.ToggleLeft( "Shortest: Draw only the last two nodes of complete paths", searchResultDrawParameters.pathDrawingMode == PathDrawingMode.Shortest ) )
|
||||
searchResultDrawParameters.pathDrawingMode = PathDrawingMode.Shortest;
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
@@ -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 )
|
||||
{
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -56,74 +56,95 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
if( target == null || target.Equals( null ) )
|
||||
return;
|
||||
|
||||
if( !target.IsAsset() || !AssetDatabase.IsMainAsset( target ) || target is SceneAsset )
|
||||
return;
|
||||
|
||||
if( includeTarget )
|
||||
if( !target.IsAsset() )
|
||||
{
|
||||
if( !currentSubAssets.Contains( target ) )
|
||||
GameObject go = target as GameObject;
|
||||
if( !go || !go.scene.IsValid() )
|
||||
return;
|
||||
|
||||
// If this is a scene object, add its child objects to the sub-assets list
|
||||
// but don't include them in the search by default
|
||||
Transform goTransform = go.transform;
|
||||
Transform[] children = go.GetComponentsInChildren<Transform>();
|
||||
for( int i = 0; i < children.Length; i++ )
|
||||
{
|
||||
subAssets.Add( new SubAsset( target, true ) );
|
||||
currentSubAssets.Add( target );
|
||||
if( ReferenceEquals( children[i], goTransform ) )
|
||||
continue;
|
||||
|
||||
subAssets.Add( new SubAsset( children[i].gameObject, false ) );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If asset is a directory, add all of its contents as sub-assets recursively
|
||||
if( target.IsFolder() )
|
||||
{
|
||||
foreach( string filePath in Utilities.EnumerateFolderContents( target ) )
|
||||
AddSubAssets( AssetDatabase.LoadAssetAtPath<Object>( filePath ), true );
|
||||
|
||||
if( !AssetDatabase.IsMainAsset( target ) || target is SceneAsset )
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Find sub-asset(s) of the asset (if any)
|
||||
Object[] assets = AssetDatabase.LoadAllAssetsAtPath( AssetDatabase.GetAssetPath( target ) );
|
||||
for( int i = 0; i < assets.Length; i++ )
|
||||
{
|
||||
Object asset = assets[i];
|
||||
if( asset == null || asset.Equals( null ) || asset is Component )
|
||||
continue;
|
||||
|
||||
if( currentSubAssets.Contains( asset ) )
|
||||
continue;
|
||||
|
||||
if( asset != target )
|
||||
if( includeTarget )
|
||||
{
|
||||
subAssets.Add( new SubAsset( asset, true ) );
|
||||
currentSubAssets.Add( asset );
|
||||
if( !currentSubAssets.Contains( target ) )
|
||||
{
|
||||
subAssets.Add( new SubAsset( target, true ) );
|
||||
currentSubAssets.Add( target );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If asset is a directory, add all of its contents as sub-assets recursively
|
||||
if( target.IsFolder() )
|
||||
{
|
||||
foreach( string filePath in Utilities.EnumerateFolderContents( target ) )
|
||||
AddSubAssets( AssetDatabase.LoadAssetAtPath<Object>( filePath ), true );
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// MonoScripts are a special case such that other MonoScript objects
|
||||
// that extend this MonoScript are also considered a sub-asset
|
||||
if( asset is MonoScript )
|
||||
// Find sub-asset(s) of the asset (if any)
|
||||
Object[] assets = AssetDatabase.LoadAllAssetsAtPath( AssetDatabase.GetAssetPath( target ) );
|
||||
for( int i = 0; i < assets.Length; i++ )
|
||||
{
|
||||
Type monoScriptType = ( (MonoScript) asset ).GetClass();
|
||||
if( monoScriptType == null || ( !monoScriptType.IsInterface && !typeof( Component ).IsAssignableFrom( monoScriptType ) ) )
|
||||
Object asset = assets[i];
|
||||
if( asset == null || asset.Equals( null ) || asset is Component )
|
||||
continue;
|
||||
|
||||
// Find all MonoScript objects in the project
|
||||
if( monoScriptsInProject == null )
|
||||
if( currentSubAssets.Contains( asset ) )
|
||||
continue;
|
||||
|
||||
if( asset != target )
|
||||
{
|
||||
string[] monoScriptGuids = AssetDatabase.FindAssets( "t:MonoScript" );
|
||||
monoScriptsInProject = new MonoScript[monoScriptGuids.Length];
|
||||
for( int k = 0; k < monoScriptGuids.Length; k++ )
|
||||
monoScriptsInProject[k] = AssetDatabase.LoadAssetAtPath<MonoScript>( AssetDatabase.GUIDToAssetPath( monoScriptGuids[k] ) );
|
||||
subAssets.Add( new SubAsset( asset, true ) );
|
||||
currentSubAssets.Add( asset );
|
||||
}
|
||||
|
||||
// Add any MonoScript objects that extend this MonoScript as a sub-asset
|
||||
for( int j = 0; j < monoScriptsInProject.Length; j++ )
|
||||
// MonoScripts are a special case such that other MonoScript objects
|
||||
// that extend this MonoScript are also considered a sub-asset
|
||||
if( asset is MonoScript )
|
||||
{
|
||||
Type otherMonoScriptType = monoScriptsInProject[j].GetClass();
|
||||
if( otherMonoScriptType == null || monoScriptType == otherMonoScriptType || !monoScriptType.IsAssignableFrom( otherMonoScriptType ) )
|
||||
Type monoScriptType = ( (MonoScript) asset ).GetClass();
|
||||
if( monoScriptType == null || ( !monoScriptType.IsInterface && !typeof( Component ).IsAssignableFrom( monoScriptType ) ) )
|
||||
continue;
|
||||
|
||||
if( !currentSubAssets.Contains( monoScriptsInProject[j] ) )
|
||||
// Find all MonoScript objects in the project
|
||||
if( monoScriptsInProject == null )
|
||||
{
|
||||
subAssets.Add( new SubAsset( monoScriptsInProject[j], true ) );
|
||||
currentSubAssets.Add( monoScriptsInProject[j] );
|
||||
string[] monoScriptGuids = AssetDatabase.FindAssets( "t:MonoScript" );
|
||||
monoScriptsInProject = new MonoScript[monoScriptGuids.Length];
|
||||
for( int k = 0; k < monoScriptGuids.Length; k++ )
|
||||
monoScriptsInProject[k] = AssetDatabase.LoadAssetAtPath<MonoScript>( AssetDatabase.GUIDToAssetPath( monoScriptGuids[k] ) );
|
||||
}
|
||||
|
||||
// Add any MonoScript objects that extend this MonoScript as a sub-asset
|
||||
for( int j = 0; j < monoScriptsInProject.Length; j++ )
|
||||
{
|
||||
Type otherMonoScriptType = monoScriptsInProject[j].GetClass();
|
||||
if( otherMonoScriptType == null || monoScriptType == otherMonoScriptType || !monoScriptType.IsAssignableFrom( otherMonoScriptType ) )
|
||||
continue;
|
||||
|
||||
if( !currentSubAssets.Contains( monoScriptsInProject[j] ) )
|
||||
{
|
||||
subAssets.Add( new SubAsset( monoScriptsInProject[j], true ) );
|
||||
currentSubAssets.Add( monoScriptsInProject[j] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace AssetUsageDetectorNamespace
|
||||
@@ -11,14 +12,19 @@ namespace AssetUsageDetectorNamespace
|
||||
[Serializable]
|
||||
public class SearchResultDrawParameters
|
||||
{
|
||||
public SearchResult searchResult;
|
||||
public PathDrawingMode pathDrawingMode;
|
||||
public bool showTooltips;
|
||||
public bool noAssetDatabaseChanges;
|
||||
public Rect guiRect;
|
||||
public bool shouldRefreshEditorWindow;
|
||||
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 +37,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;
|
||||
}
|
||||
@@ -47,10 +55,20 @@ namespace AssetUsageDetectorNamespace
|
||||
public List<string> linkDescriptions;
|
||||
}
|
||||
|
||||
private bool success;
|
||||
#pragma warning disable 1692
|
||||
#pragma warning disable IDE0044
|
||||
private bool success; // This is not readonly so that it can be serialized
|
||||
#pragma warning restore IDE0044
|
||||
#pragma warning restore 1692
|
||||
|
||||
private List<SearchResultGroup> result;
|
||||
private SceneSetup[] initialSceneSetup;
|
||||
|
||||
private AssetUsageDetector searchHandler;
|
||||
private AssetUsageDetector.Parameters m_searchParameters;
|
||||
|
||||
private float guiHeight; // Prevents scroll view's position from getting reset at domain reload
|
||||
|
||||
private List<SerializableNode> serializedNodes;
|
||||
private List<SerializableResultGroup> serializedGroups;
|
||||
|
||||
@@ -59,8 +77,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,15 +87,137 @@ 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();
|
||||
|
||||
SearchResult searchResult = searchHandler.Run( m_searchParameters );
|
||||
if( !searchResult.success )
|
||||
{
|
||||
EditorUtility.DisplayDialog( "Error", "Couldn't refresh, check console for more info.", "OK" );
|
||||
return;
|
||||
}
|
||||
|
||||
List<SearchResultGroup> searchResultGroups = searchResult.result;
|
||||
if( searchResultGroups != null )
|
||||
{
|
||||
int newSearchResultGroupIndex = -1;
|
||||
for( int i = 0; i < searchResultGroups.Count; i++ )
|
||||
{
|
||||
if( searchResultGroup.Title == searchResultGroups[i].Title )
|
||||
{
|
||||
newSearchResultGroupIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if( newSearchResultGroupIndex >= 0 )
|
||||
result[searchResultGroupIndex] = searchResultGroups[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;
|
||||
parameters.guiRect = EditorGUILayout.GetControlRect( Utilities.GL_EXPAND_WIDTH, Utilities.GL_HEIGHT_0 );
|
||||
|
||||
float guiInitialY = parameters.guiRect.y;
|
||||
|
||||
for( int i = 0; i < result.Count; i++ )
|
||||
{
|
||||
result[i].DrawOnGUI( parameters );
|
||||
|
||||
if( i < result.Count - 1 )
|
||||
{
|
||||
Rect rect = parameters.guiRect;
|
||||
rect.y += 10f;
|
||||
parameters.guiRect = rect;
|
||||
}
|
||||
}
|
||||
|
||||
if( Event.current.type == EventType.Repaint )
|
||||
guiHeight = parameters.guiRect.y - guiInitialY;
|
||||
|
||||
// To work nicely with GUILayout and scroll view
|
||||
EditorGUILayout.GetControlRect( Utilities.GL_EXPAND_WIDTH, GUILayout.Height( guiHeight ) );
|
||||
|
||||
if( parameters.tooltip != null )
|
||||
{
|
||||
// Show tooltip at mouse position
|
||||
@@ -84,7 +225,26 @@ namespace AssetUsageDetectorNamespace
|
||||
Vector2 size = Utilities.TooltipGUIStyle.CalcSize( new GUIContent( parameters.tooltip ) );
|
||||
size.x += 10f;
|
||||
|
||||
GUI.Box( new Rect( new Vector2( mousePos.x - size.x * 0.5f, mousePos.y - size.y ), size ), parameters.tooltip, Utilities.TooltipGUIStyle );
|
||||
Rect tooltipRect = new Rect( new Vector2( mousePos.x - size.x * 0.5f, mousePos.y - size.y ), size );
|
||||
if( tooltipRect.xMin < 0f )
|
||||
tooltipRect.x -= tooltipRect.xMin;
|
||||
else if( tooltipRect.xMax > parameters.guiRect.width )
|
||||
tooltipRect.x -= tooltipRect.xMax - parameters.guiRect.width;
|
||||
|
||||
GUI.Box( tooltipRect, parameters.tooltip, Utilities.TooltipGUIStyle );
|
||||
}
|
||||
|
||||
if( parameters.shouldRefreshEditorWindow )
|
||||
{
|
||||
parameters.shouldRefreshEditorWindow = false;
|
||||
|
||||
EditorWindow focusedWindow = EditorWindow.focusedWindow;
|
||||
if( focusedWindow != null )
|
||||
focusedWindow.Repaint();
|
||||
|
||||
EditorWindow mouseOverWindow = EditorWindow.mouseOverWindow;
|
||||
if( mouseOverWindow != null && mouseOverWindow != focusedWindow )
|
||||
mouseOverWindow.Repaint();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +324,16 @@ namespace AssetUsageDetectorNamespace
|
||||
if( EditorApplication.isPlaying || !EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo() )
|
||||
return false;
|
||||
|
||||
for( int i = 0; i < initialSceneSetup.Length; i++ )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneByPath( initialSceneSetup[i].path );
|
||||
if( !scene.isLoaded )
|
||||
scene = EditorSceneManager.OpenScene( initialSceneSetup[i].path, initialSceneSetup[i].isLoaded ? OpenSceneMode.Additive : OpenSceneMode.AdditiveWithoutLoading );
|
||||
|
||||
if( initialSceneSetup[i].isActive )
|
||||
EditorSceneManager.SetActiveScene( scene );
|
||||
}
|
||||
|
||||
SceneSetup[] sceneFinalSetup = EditorSceneManager.GetSceneManagerSetup();
|
||||
for( int i = 0; i < sceneFinalSetup.Length; i++ )
|
||||
{
|
||||
@@ -232,7 +402,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 +414,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,24 +429,38 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
}
|
||||
|
||||
private readonly string title;
|
||||
private readonly bool clickable;
|
||||
private const float HEADER_HEIGHT = 40f;
|
||||
|
||||
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;
|
||||
private List<ReferencePath> referencePathsShortest;
|
||||
|
||||
private float calculatedGUIWidth;
|
||||
private float calculatedGUIHeight;
|
||||
private PathDrawingMode calculatedGUIMode;
|
||||
private ReferenceNodeGUI[] guiNodes;
|
||||
|
||||
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;
|
||||
referencePathsShortest = null;
|
||||
|
||||
calculatedGUIWidth = 0f;
|
||||
guiNodes = null;
|
||||
}
|
||||
|
||||
// Add a reference to the list
|
||||
@@ -283,6 +469,21 @@ namespace AssetUsageDetectorNamespace
|
||||
references.Add( node );
|
||||
}
|
||||
|
||||
// Removes all nodes
|
||||
public void Clear()
|
||||
{
|
||||
references.Clear();
|
||||
|
||||
if( referencePathsShortUnique != null )
|
||||
referencePathsShortUnique.Clear();
|
||||
if( referencePathsShortest != null )
|
||||
referencePathsShortest.Clear();
|
||||
|
||||
PendingSearch = false;
|
||||
calculatedGUIWidth = 0f;
|
||||
guiNodes = null;
|
||||
}
|
||||
|
||||
// Initializes commonly used variables of the nodes
|
||||
public void InitializeNodes( Func<object, ReferenceNode> nodeGetter )
|
||||
{
|
||||
@@ -299,10 +500,24 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
}
|
||||
|
||||
for( int i = references.Count - 1; i >= 0; i-- )
|
||||
{
|
||||
references[i].VerifyLinksRecursively();
|
||||
|
||||
// Some links might be removed during verification
|
||||
if( references[i].NumberOfOutgoingLinks == 0 )
|
||||
references.RemoveAtFast( i );
|
||||
}
|
||||
|
||||
if( references.Count == 0 )
|
||||
return;
|
||||
|
||||
List<ReferenceNode> callStack = new List<ReferenceNode>( 8 );
|
||||
|
||||
// For simplicity's sake, get rid of root nodes that are already part of another node's hierarchy
|
||||
for( int i = references.Count - 1; i >= 0; i-- )
|
||||
{
|
||||
if( IsRootNodePartOfAnotherRootNode( i ) )
|
||||
if( IsRootNodePartOfAnotherRootNode( i, callStack ) )
|
||||
references.RemoveAtFast( i );
|
||||
}
|
||||
|
||||
@@ -320,6 +535,10 @@ namespace AssetUsageDetectorNamespace
|
||||
if( mainAsset == null || mainAsset.Equals( null ) )
|
||||
continue;
|
||||
|
||||
// We're already calling AssetDatabase.IsMainAsset but just in case...
|
||||
if( ReferenceEquals( nodeObject, mainAsset ) )
|
||||
continue;
|
||||
|
||||
if( nodeObject is Component && ( (Component) nodeObject ).gameObject == mainAsset )
|
||||
continue;
|
||||
|
||||
@@ -329,12 +548,12 @@ namespace AssetUsageDetectorNamespace
|
||||
references[i] = newRootNode;
|
||||
|
||||
// Make sure that the new root node isn't already a part of another node's hierarchy
|
||||
if( IsRootNodePartOfAnotherRootNode( i ) )
|
||||
if( IsRootNodePartOfAnotherRootNode( i, callStack ) )
|
||||
references.RemoveAtFast( i );
|
||||
}
|
||||
}
|
||||
|
||||
for( int i = references.Count - 1; i >= 0; i-- )
|
||||
for( int i = 0; i < references.Count; i++ )
|
||||
references[i].InitializeRecursively();
|
||||
}
|
||||
|
||||
@@ -350,7 +569,7 @@ namespace AssetUsageDetectorNamespace
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool IsRootNodePartOfAnotherRootNode( int index )
|
||||
private bool IsRootNodePartOfAnotherRootNode( int index, List<ReferenceNode> callStack )
|
||||
{
|
||||
ReferenceNode node = references[index];
|
||||
for( int i = references.Count - 1; i >= 0; i-- )
|
||||
@@ -358,7 +577,7 @@ namespace AssetUsageDetectorNamespace
|
||||
if( index == i )
|
||||
continue;
|
||||
|
||||
if( references[i].nodeObject == node.nodeObject || references[i].NodeExistsInChildrenRecursive( node ) )
|
||||
if( references[i].nodeObject == node.nodeObject || references[i].NodeExistsInChildrenRecursive( node, callStack ) )
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -368,6 +587,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 +603,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 +622,7 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Calculate short unique paths to the references
|
||||
public void CalculateShortestPathsToReferences()
|
||||
private void CalculateShortestPathsToReferences()
|
||||
{
|
||||
if( referencePathsShortUnique != null )
|
||||
return;
|
||||
@@ -436,60 +661,163 @@ namespace AssetUsageDetectorNamespace
|
||||
// Draw the results found for this container
|
||||
public void DrawOnGUI( SearchResultDrawParameters parameters )
|
||||
{
|
||||
if( Event.current.type == EventType.Repaint && ( guiNodes == null || parameters.pathDrawingMode != calculatedGUIMode || parameters.guiRect.width != calculatedGUIWidth ) )
|
||||
{
|
||||
GenerateGUINodes( parameters );
|
||||
parameters.shouldRefreshEditorWindow = true; // A repaint is needed to work nicely with GUILayout
|
||||
}
|
||||
|
||||
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 = parameters.guiRect;
|
||||
float width = rect.width;
|
||||
rect.width = 40f;
|
||||
rect.height = HEADER_HEIGHT;
|
||||
if( GUI.Button( rect, IsExpanded ? "v" : ">" ) )
|
||||
{
|
||||
// If the container (scene, usually) is clicked, highlight it on Project view
|
||||
AssetDatabase.LoadAssetAtPath<SceneAsset>( title ).SelectInEditor();
|
||||
IsExpanded = !IsExpanded;
|
||||
|
||||
parameters.shouldRefreshEditorWindow = true;
|
||||
GUIUtility.ExitGUI();
|
||||
}
|
||||
|
||||
GUI.color = Color.yellow;
|
||||
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.Full )
|
||||
rect.x += 40f;
|
||||
rect.width = width - ( parameters.searchResult != null ? 140f : 40f );
|
||||
if( GUI.Button( rect, Title, Utilities.BoxGUIStyle ) && Type == GroupType.Scene )
|
||||
{
|
||||
for( int i = 0; i < references.Count; i++ )
|
||||
if( Event.current.button != 1 )
|
||||
{
|
||||
GUILayout.Space( 5 );
|
||||
references[i].DrawOnGUIRecursively( parameters, null );
|
||||
// If the container (scene, usually) is left clicked, highlight it on Project view
|
||||
AssetDatabase.LoadAssetAtPath<SceneAsset>( Title ).SelectInEditor();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if( referencePathsShortUnique == null )
|
||||
CalculateShortestPathsToReferences();
|
||||
|
||||
List<ReferencePath> pathsToDraw;
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.ShortRelevantParts )
|
||||
pathsToDraw = referencePathsShortUnique;
|
||||
else
|
||||
pathsToDraw = referencePathsShortest;
|
||||
|
||||
for( int i = 0; i < pathsToDraw.Count; i++ )
|
||||
else if( !EditorApplication.isPlaying && EditorSceneManager.loadedSceneCount > 1 )
|
||||
{
|
||||
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++ )
|
||||
// Show context menu when SearchResultGroup's header is right clicked
|
||||
Scene scene = EditorSceneManager.GetSceneByPath( Title );
|
||||
if( scene.isLoaded )
|
||||
{
|
||||
ReferenceNode.Link link = currentNode[path.pathLinksToFollow[j]];
|
||||
link.targetNode.DrawOnGUI( parameters, link.description );
|
||||
currentNode = link.targetNode;
|
||||
GenericMenu contextMenu = new GenericMenu();
|
||||
contextMenu.AddItem( new GUIContent( "Close Scene" ), false, () =>
|
||||
{
|
||||
if( !scene.isDirty || EditorSceneManager.SaveModifiedScenesIfUserWantsTo( new Scene[1] { scene } ) )
|
||||
EditorSceneManager.CloseScene( scene, true );
|
||||
} );
|
||||
contextMenu.ShowAsContext();
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
}
|
||||
}
|
||||
|
||||
GUI.color = c;
|
||||
if( parameters.searchResult != null )
|
||||
{
|
||||
rect.x += width - 140f;
|
||||
rect.width = 100f;
|
||||
if( GUI.Button( rect, "Refresh" ) )
|
||||
{
|
||||
if( Utilities.AreScenesSaved() || ( EditorUtility.DisplayDialog( "Refresh", "Some scene(s) have unsaved changes, they must be saved before the refresh.", "Save Scenes", "Cancel" ) && EditorSceneManager.SaveOpenScenes() ) )
|
||||
{
|
||||
parameters.searchResult.RefreshSearchResultGroup( this, parameters.noAssetDatabaseChanges );
|
||||
GUIUtility.ExitGUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
rect = parameters.guiRect;
|
||||
rect.y += HEADER_HEIGHT + 5f;
|
||||
|
||||
if( IsExpanded )
|
||||
{
|
||||
GUI.color = Color.yellow;
|
||||
|
||||
if( PendingSearch )
|
||||
{
|
||||
rect.height = 25f;
|
||||
GUI.Box( rect, "Lazy Search: this scene potentially has some references, hit Refresh to find them", Utilities.BoxGUIStyle );
|
||||
}
|
||||
else if( references.Count == 0 )
|
||||
{
|
||||
rect.height = 25f;
|
||||
GUI.Box( rect, "No references found...", Utilities.BoxGUIStyle );
|
||||
}
|
||||
else if( guiNodes != null )
|
||||
{
|
||||
rect.height = calculatedGUIHeight;
|
||||
parameters.guiRect = rect;
|
||||
|
||||
for( int i = 0; i < guiNodes.Length; i++ )
|
||||
guiNodes[i].DrawOnGUI( parameters );
|
||||
}
|
||||
|
||||
rect.y += rect.height;
|
||||
}
|
||||
|
||||
parameters.guiRect = rect;
|
||||
GUI.color = c;
|
||||
}
|
||||
|
||||
private void GenerateGUINodes( SearchResultDrawParameters parameters )
|
||||
{
|
||||
float guiRectWidth = parameters.guiRect.width;
|
||||
|
||||
if( guiNodes == null || parameters.pathDrawingMode != calculatedGUIMode )
|
||||
{
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.Full )
|
||||
{
|
||||
List<ReferenceNode> stack = new List<ReferenceNode>( 8 );
|
||||
guiNodes = new ReferenceNodeGUI[references.Count];
|
||||
|
||||
for( int i = 0; i < guiNodes.Length; i++ )
|
||||
guiNodes[i] = references[i].GenerateGUINodeRecursive( stack, null );
|
||||
}
|
||||
else
|
||||
{
|
||||
if( referencePathsShortUnique == null )
|
||||
CalculateShortestPathsToReferences();
|
||||
|
||||
List<ReferencePath> pathsToDraw;
|
||||
if( parameters.pathDrawingMode == PathDrawingMode.ShortRelevantParts )
|
||||
pathsToDraw = referencePathsShortUnique;
|
||||
else
|
||||
pathsToDraw = referencePathsShortest;
|
||||
|
||||
guiNodes = new ReferenceNodeGUI[pathsToDraw.Count];
|
||||
|
||||
for( int i = 0; i < guiNodes.Length; i++ )
|
||||
{
|
||||
ReferencePath path = pathsToDraw[i];
|
||||
ReferenceNode currentNode = path.startNode;
|
||||
|
||||
ReferenceNodeGUI currentNodeGUI = currentNode.GenerateGUINode( null );
|
||||
guiNodes[i] = currentNodeGUI;
|
||||
|
||||
for( int j = 0; j < path.pathLinksToFollow.Length; j++ )
|
||||
{
|
||||
ReferenceNode.Link link = currentNode[path.pathLinksToFollow[j]];
|
||||
currentNodeGUI.links = new ReferenceNodeGUI[1] { link.targetNode.GenerateGUINode( link.description ) };
|
||||
|
||||
currentNode = link.targetNode;
|
||||
currentNodeGUI = currentNodeGUI.links[0];
|
||||
}
|
||||
|
||||
currentNodeGUI.links = new ReferenceNodeGUI[0];
|
||||
}
|
||||
}
|
||||
|
||||
for( int i = 0; i < guiNodes.Length; i++ )
|
||||
guiNodes[i].CalculateDepth();
|
||||
}
|
||||
|
||||
calculatedGUIMode = parameters.pathDrawingMode;
|
||||
calculatedGUIWidth = guiRectWidth;
|
||||
|
||||
calculatedGUIHeight = -5f; // will start from -5f + 5f = 0f
|
||||
for( int i = 0; i < guiNodes.Length; i++ )
|
||||
{
|
||||
calculatedGUIHeight += 5f;
|
||||
guiNodes[i].CalculateHeight( guiRectWidth );
|
||||
guiNodes[i].CalculateOffset( new Vector2( 0f, calculatedGUIHeight ) );
|
||||
calculatedGUIHeight += guiNodes[i].size.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Serialize this result group
|
||||
@@ -497,8 +825,10 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
SearchResult.SerializableResultGroup serializedResultGroup = new SearchResult.SerializableResultGroup()
|
||||
{
|
||||
title = title,
|
||||
clickable = clickable
|
||||
title = Title,
|
||||
type = Type,
|
||||
isExpanded = IsExpanded,
|
||||
pendingSearch = PendingSearch
|
||||
};
|
||||
|
||||
if( references != null )
|
||||
@@ -545,12 +875,12 @@ namespace AssetUsageDetectorNamespace
|
||||
private static int uid_last = 0;
|
||||
private readonly int uid;
|
||||
|
||||
public object nodeObject;
|
||||
private readonly List<Link> links;
|
||||
|
||||
internal object nodeObject;
|
||||
private int? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise
|
||||
private string description; // String to print on this node
|
||||
|
||||
private readonly List<Link> links;
|
||||
|
||||
public Object UnityObject { get { return instanceId.HasValue ? EditorUtility.InstanceIDToObject( instanceId.Value ) : null; } }
|
||||
|
||||
public int NumberOfOutgoingLinks { get { return links.Count; } }
|
||||
@@ -562,11 +892,6 @@ namespace AssetUsageDetectorNamespace
|
||||
uid = uid_last++;
|
||||
}
|
||||
|
||||
public ReferenceNode( object obj ) : this()
|
||||
{
|
||||
nodeObject = obj;
|
||||
}
|
||||
|
||||
// Add a one-way connection to another node
|
||||
public void AddLinkTo( ReferenceNode nextNode, string description = null )
|
||||
{
|
||||
@@ -596,6 +921,40 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
}
|
||||
|
||||
public void CopyReferencesTo( ReferenceNode other )
|
||||
{
|
||||
other.links.Clear();
|
||||
other.links.AddRange( links );
|
||||
}
|
||||
|
||||
// Remove any redundant connections that this node has
|
||||
public void VerifyLinksRecursively()
|
||||
{
|
||||
// Make sure that this functions isn't called multiple times per node (avoids StackOverflowException)
|
||||
if( description != null )
|
||||
return;
|
||||
|
||||
description = "";
|
||||
|
||||
for( int i = links.Count - 1; i >= 0; i-- )
|
||||
{
|
||||
// Links from a GameObject to its components should be omitted if the component has no references (these are redundant links)
|
||||
if( links[i].targetNode.links.Count == 0 && nodeObject is GameObject )
|
||||
{
|
||||
Component component = links[i].targetNode.nodeObject as Component;
|
||||
if( component != null && !component.Equals( null ) && component.gameObject == (GameObject) nodeObject )
|
||||
{
|
||||
links.RemoveAtFast( i );
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
links[i].targetNode.VerifyLinksRecursively();
|
||||
}
|
||||
|
||||
description = null;
|
||||
}
|
||||
|
||||
// Initialize node's commonly used variables
|
||||
public void InitializeRecursively()
|
||||
{
|
||||
@@ -626,20 +985,29 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Returns whether or not specified node is part of this node's siblings
|
||||
public bool NodeExistsInChildrenRecursive( ReferenceNode node )
|
||||
public bool NodeExistsInChildrenRecursive( ReferenceNode node, List<ReferenceNode> callStack )
|
||||
{
|
||||
if( callStack.ContainsFast( this ) )
|
||||
return false;
|
||||
|
||||
for( int i = 0; i < links.Count; i++ )
|
||||
{
|
||||
if( links[i].targetNode == node )
|
||||
return true;
|
||||
}
|
||||
|
||||
callStack.Add( this );
|
||||
|
||||
for( int i = 0; i < links.Count; i++ )
|
||||
{
|
||||
if( links[i].targetNode.NodeExistsInChildrenRecursive( node ) )
|
||||
if( links[i].targetNode.NodeExistsInChildrenRecursive( node, callStack ) )
|
||||
{
|
||||
callStack.RemoveAt( callStack.Count - 1 );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
callStack.RemoveAt( callStack.Count - 1 );
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -653,16 +1021,16 @@ namespace AssetUsageDetectorNamespace
|
||||
// Calculate short unique paths that start with this node
|
||||
public void CalculateShortUniquePaths( List<SearchResultGroup.ReferencePath> currentPaths )
|
||||
{
|
||||
CalculateShortUniquePaths( currentPaths, new List<ReferenceNode>( 8 ), new List<int>( 8 ) { -1 }, 0 );
|
||||
CalculateShortUniquePaths( currentPaths, new List<ReferenceNode>( 8 ), new List<int>( 8 ) { -1 }, 0, new List<ReferenceNode>( 8 ) );
|
||||
}
|
||||
|
||||
// Just some boring calculations to find the short unique paths recursively
|
||||
private void CalculateShortUniquePaths( List<SearchResultGroup.ReferencePath> shortestPaths, List<ReferenceNode> currentPath, List<int> currentPathIndices, int latestObjectIndexInPath )
|
||||
private void CalculateShortUniquePaths( List<SearchResultGroup.ReferencePath> shortestPaths, List<ReferenceNode> currentPath, List<int> currentPathIndices, int latestObjectIndexInPath, List<ReferenceNode> stack )
|
||||
{
|
||||
int currentIndex = currentPath.Count;
|
||||
currentPath.Add( this );
|
||||
|
||||
if( links.Count == 0 )
|
||||
if( links.Count == 0 || stack.ContainsFast( this ) )
|
||||
{
|
||||
// Check if the path to the reference is unique (not discovered so far)
|
||||
bool isUnique = true;
|
||||
@@ -703,7 +1071,11 @@ namespace AssetUsageDetectorNamespace
|
||||
for( int i = 0; i < links.Count; i++ )
|
||||
{
|
||||
currentPathIndices.Add( i );
|
||||
links[i].targetNode.CalculateShortUniquePaths( shortestPaths, currentPath, currentPathIndices, latestObjectIndexInPath );
|
||||
stack.Add( this );
|
||||
|
||||
links[i].targetNode.CalculateShortUniquePaths( shortestPaths, currentPath, currentPathIndices, latestObjectIndexInPath, stack );
|
||||
|
||||
stack.RemoveAt( stack.Count - 1 );
|
||||
currentPathIndices.RemoveAt( currentIndex + 1 );
|
||||
}
|
||||
}
|
||||
@@ -711,41 +1083,33 @@ namespace AssetUsageDetectorNamespace
|
||||
currentPath.RemoveAt( currentIndex );
|
||||
}
|
||||
|
||||
// Draw all the paths that start with this node on GUI recursively
|
||||
public void DrawOnGUIRecursively( SearchResultDrawParameters parameters, string linkToPrevNodeDescription )
|
||||
public ReferenceNodeGUI GenerateGUINode( string linkToPrevNodeDescription )
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
DrawOnGUI( parameters, linkToPrevNodeDescription );
|
||||
|
||||
if( links.Count > 0 )
|
||||
return new ReferenceNodeGUI()
|
||||
{
|
||||
GUILayout.BeginVertical();
|
||||
|
||||
for( int i = 0; i < links.Count; i++ )
|
||||
{
|
||||
ReferenceNode next = links[i].targetNode;
|
||||
next.DrawOnGUIRecursively( parameters, links[i].description );
|
||||
}
|
||||
|
||||
GUILayout.EndVertical();
|
||||
}
|
||||
|
||||
GUILayout.EndHorizontal();
|
||||
label = new GUIContent( string.IsNullOrEmpty( linkToPrevNodeDescription ) ? description : ( linkToPrevNodeDescription + "\n" + description ) ),
|
||||
instanceId = instanceId
|
||||
};
|
||||
}
|
||||
|
||||
// Draw only this node on GUI
|
||||
public void DrawOnGUI( SearchResultDrawParameters parameters, string linkToPrevNodeDescription )
|
||||
public ReferenceNodeGUI GenerateGUINodeRecursive( List<ReferenceNode> stack, string linkToPrevNodeDescription )
|
||||
{
|
||||
string label = string.IsNullOrEmpty( linkToPrevNodeDescription ) ? description : ( linkToPrevNodeDescription + "\n" + description );
|
||||
if( GUILayout.Button( label, Utilities.BoxGUIStyle, Utilities.GL_EXPAND_HEIGHT ) )
|
||||
ReferenceNodeGUI guiNode = GenerateGUINode( linkToPrevNodeDescription );
|
||||
|
||||
if( stack.ContainsFast( this ) )
|
||||
guiNode.links = new ReferenceNodeGUI[0];
|
||||
else
|
||||
{
|
||||
// If a reference is clicked, highlight it (either on Hierarchy view or Project view)
|
||||
UnityObject.SelectInEditor();
|
||||
stack.Add( this );
|
||||
|
||||
guiNode.links = new ReferenceNodeGUI[links.Count];
|
||||
for( int i = 0; i < links.Count; i++ )
|
||||
guiNode.links[i] = links[i].targetNode.GenerateGUINodeRecursive( stack, links[i].description );
|
||||
|
||||
stack.RemoveAt( stack.Count - 1 );
|
||||
}
|
||||
|
||||
if( parameters.showTooltips && Event.current.type == EventType.Repaint && GUILayoutUtility.GetLastRect().Contains( Event.current.mousePosition ) )
|
||||
parameters.tooltip = label;
|
||||
return guiNode;
|
||||
}
|
||||
|
||||
// Serialize this node and its connected nodes recursively
|
||||
@@ -803,4 +1167,95 @@ namespace AssetUsageDetectorNamespace
|
||||
return uid;
|
||||
}
|
||||
}
|
||||
|
||||
public class ReferenceNodeGUI
|
||||
{
|
||||
public GUIContent label;
|
||||
public ReferenceNodeGUI[] links;
|
||||
public int? instanceId;
|
||||
|
||||
private int depth;
|
||||
|
||||
private Vector2 offset;
|
||||
public Vector2 size;
|
||||
|
||||
public void CalculateDepth()
|
||||
{
|
||||
depth = 0;
|
||||
|
||||
for( int i = 0; i < links.Length; i++ )
|
||||
{
|
||||
links[i].CalculateDepth();
|
||||
if( links[i].depth > depth )
|
||||
depth = links[i].depth;
|
||||
}
|
||||
|
||||
depth++;
|
||||
}
|
||||
|
||||
public void CalculateHeight( float totalWidth )
|
||||
{
|
||||
size.x = totalWidth / depth;
|
||||
totalWidth -= size.x;
|
||||
size.y = Utilities.BoxGUIStyle.CalcHeight( label, size.x );
|
||||
float connectedNodesHeight = 0f;
|
||||
for( int i = 0; i < links.Length; i++ )
|
||||
{
|
||||
links[i].CalculateHeight( totalWidth );
|
||||
connectedNodesHeight += links[i].size.y;
|
||||
}
|
||||
|
||||
if( size.y < connectedNodesHeight )
|
||||
size.y = connectedNodesHeight;
|
||||
else if( size.y > connectedNodesHeight )
|
||||
ExpandHeightLeftToRight();
|
||||
}
|
||||
|
||||
private void ExpandHeightLeftToRight()
|
||||
{
|
||||
float connectedNodesHeight = 0f;
|
||||
for( int i = 0; i < links.Length; i++ )
|
||||
connectedNodesHeight += links[i].size.y;
|
||||
|
||||
if( size.y > connectedNodesHeight )
|
||||
{
|
||||
for( int i = 0; i < links.Length; i++ )
|
||||
{
|
||||
links[i].size.y = size.y * links[i].size.y / connectedNodesHeight;
|
||||
links[i].ExpandHeightLeftToRight();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CalculateOffset( Vector2 baseOffset )
|
||||
{
|
||||
offset = baseOffset;
|
||||
baseOffset.x += size.x;
|
||||
|
||||
for( int i = 0; i < links.Length; i++ )
|
||||
{
|
||||
links[i].CalculateOffset( baseOffset );
|
||||
baseOffset.y += links[i].size.y;
|
||||
}
|
||||
}
|
||||
|
||||
public void DrawOnGUI( SearchResultDrawParameters parameters )
|
||||
{
|
||||
Rect rect = parameters.guiRect;
|
||||
rect.position += offset;
|
||||
rect.size = size;
|
||||
|
||||
if( GUI.Button( rect, label, Utilities.BoxGUIStyle ) && instanceId.HasValue )
|
||||
{
|
||||
// If a reference is clicked, highlight it (either on Hierarchy view or Project view)
|
||||
EditorUtility.InstanceIDToObject( instanceId.Value ).SelectInEditor();
|
||||
}
|
||||
|
||||
for( int i = 0; i < links.Length; i++ )
|
||||
links[i].DrawOnGUI( parameters );
|
||||
|
||||
if( parameters.showTooltips && Event.current.type == EventType.Repaint && rect.Contains( Event.current.mousePosition ) )
|
||||
parameters.tooltip = label.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
using Unity.Collections;
|
||||
#endif
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
@@ -12,31 +15,48 @@ 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 string reflectionNamespace = typeof( Assembly ).Namespace;
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
private static readonly string nativeCollectionsNamespace = typeof( NativeArray<int> ).Namespace;
|
||||
#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 );
|
||||
public static readonly GUILayoutOption GL_WIDTH_100 = GUILayout.Width( 100 );
|
||||
public static readonly GUILayoutOption GL_WIDTH_250 = GUILayout.Width( 250 );
|
||||
public static readonly GUILayoutOption GL_HEIGHT_0 = GUILayout.Height( 0 );
|
||||
public static readonly GUILayoutOption GL_HEIGHT_30 = GUILayout.Height( 30 );
|
||||
public static readonly GUILayoutOption GL_HEIGHT_35 = GUILayout.Height( 35 );
|
||||
public static readonly GUILayoutOption GL_HEIGHT_40 = GUILayout.Height( 40 );
|
||||
@@ -142,9 +162,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 +231,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 +276,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 )
|
||||
{
|
||||
@@ -304,10 +359,27 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
return Attribute.IsDefined( type, typeof( SerializableAttribute ), false );
|
||||
}
|
||||
|
||||
// Check if the type is a common Unity type (let's call them primitives)
|
||||
public static bool IsPrimitiveUnityType( this Type type )
|
||||
// Check if instances of this type should be searched for references
|
||||
public static bool IsIgnoredUnityType( this Type type )
|
||||
{
|
||||
return type.IsPrimitive || primitiveUnityTypes.Contains( type ) || type.IsEnum;
|
||||
if( type.IsPrimitive || primitiveUnityTypes.Contains( type ) || type.IsEnum )
|
||||
return true;
|
||||
|
||||
#if UNITY_2018_1_OR_NEWER
|
||||
// Searching NativeArrays for reference can throw InvalidOperationException if the collection is disposed
|
||||
if( type.Namespace == nativeCollectionsNamespace )
|
||||
return true;
|
||||
#endif
|
||||
|
||||
// Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
|
||||
if( typeof( Type ).IsAssignableFrom( type ) || type.Namespace == reflectionNamespace )
|
||||
return true;
|
||||
|
||||
// Searching pointer variables for reference throws ArgumentException
|
||||
if( type.IsPointer )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get <get> function for a field
|
||||
|
||||
@@ -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