- Bugfix for System.Reflection.Assembly variables throwing exceptions on .NET 4.0 runtime

- Preferences in Asset Usage Detector window are now saved
- Moved utility/internal classes into separate namespace
This commit is contained in:
Süleyman Yasir KULA
2019-07-06 13:06:20 +03:00
parent 93b8b267eb
commit 64408e3eb2
9 changed files with 135 additions and 28 deletions
Binary file not shown.
@@ -1,5 +1,6 @@
// Asset Usage Detector - by Suleyman Yasir KULA (yasirkula@gmail.com)
using AssetUsageDetectorNamespace.Extras;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
@@ -24,6 +25,7 @@ namespace AssetUsageDetectorNamespace
{
[Flags]
public enum SceneSearchMode { None = 0, OpenScenes = 1, ScenesInBuildSettingsAll = 2, ScenesInBuildSettingsTickedOnly = 4, AllScenes = 8 };
public enum PathDrawingMode { Full = 0, ShortRelevantParts = 1, Shortest = 2 };
public class AssetUsageDetector
{
@@ -389,15 +391,7 @@ namespace AssetUsageDetectorNamespace
searchResult.Add( currentSearchResultGroup );
}
for( int i = 0; i < searchResult.Count; i++ )
searchResult[i].InitializeNodes();
// 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 )
searchResult.RemoveAt( i );
}
InitializeSearchResultNodes( searchResult );
// Log some c00l stuff to console
Debug.Log( "Searched " + searchedObjectsCount + " objects in " + ( EditorApplication.timeSinceStartup - searchStartTime ).ToString( "F2" ) + " seconds" );
@@ -407,6 +401,14 @@ namespace AssetUsageDetectorNamespace
catch( Exception e )
{
Debug.LogException( e );
try
{
InitializeSearchResultNodes( searchResult );
}
catch
{ }
return new SearchResult( false, searchResult, initialSceneSetup );
}
finally
@@ -416,6 +418,19 @@ namespace AssetUsageDetectorNamespace
}
}
private void InitializeSearchResultNodes( List<SearchResultGroup> searchResult )
{
for( int i = 0; i < searchResult.Count; i++ )
searchResult[i].InitializeNodes();
// 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 )
searchResult.RemoveAt( i );
}
}
// Checks if object is asset or scene object and adds it to the corresponding HashSet(s)
private void AddSearchedObjectToFilteredSets( Object obj )
{
@@ -1101,6 +1116,8 @@ namespace AssetUsageDetectorNamespace
string title = "Please wait...";
string message = "Generating cache for the first time";
double startTime = EditorApplication.timeSinceStartup;
HashSet<string> temp = new HashSet<string>();
EditorUtility.DisplayProgressBar( title, message, 0f );
@@ -1111,6 +1128,9 @@ namespace AssetUsageDetectorNamespace
}
EditorUtility.ClearProgressBar();
Debug.Log( "Cache generated in " + ( EditorApplication.timeSinceStartup - startTime ).ToString( "F2" ) + " seconds" );
SaveCache();
}
}
}
@@ -8,15 +8,22 @@ using System.Collections.Generic;
using System.Reflection;
using Object = UnityEngine.Object;
namespace AssetUsageDetectorNamespace
namespace AssetUsageDetectorNamespace.Extras
{
public enum Phase { Setup, Processing, Complete };
public enum PathDrawingMode { Full, ShortRelevantParts, Shortest };
public class AssetUsageDetectorWindow : EditorWindow
{
private const float PLAY_MODE_REFRESH_INTERVAL = 1f; // Interval to refresh the editor window in play mode
private const string PREFS_SEARCH_SCENES = "AUD_SceneSearch";
private const string PREFS_SEARCH_ASSETS = "AUD_AssetsSearch";
private const string PREFS_SEARCH_DEPTH_LIMIT = "AUD_Depth";
private const string PREFS_SEARCH_FIELDS = "AUD_Fields";
private const string PREFS_SEARCH_PROPERTIES = "AUD_Properties";
private const string PREFS_PATH_DRAWING_MODE = "AUD_PathDrawing";
private const string PREFS_SHOW_TOOLTIPS = "AUD_Tooltips";
private AssetUsageDetector core = new AssetUsageDetector();
private SearchResult searchResult; // Overall search results
@@ -39,9 +46,7 @@ namespace AssetUsageDetectorNamespace
private string errorMessage = string.Empty;
private bool noAssetDatabaseChanges = false;
// Fetch public, protected and private non-static fields and properties from objects by default
private BindingFlags fieldModifiers = BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic;
private BindingFlags propertyModifiers = BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic;
private BindingFlags fieldModifiers, propertyModifiers;
private SearchResultDrawParameters searchResultDrawParameters = new SearchResultDrawParameters( PathDrawingMode.ShortRelevantParts, false );
@@ -56,24 +61,71 @@ namespace AssetUsageDetectorNamespace
AssetUsageDetectorWindow window = GetWindow<AssetUsageDetectorWindow>();
window.titleContent = new GUIContent( "Asset Usage Detector" );
window.minSize = new Vector2( 325f, 220f );
window.Show();
}
private void OnDisable()
{
if( core != null )
core.SaveCache();
window.LoadPrefs();
window.Show();
}
private void OnDestroy()
{
if( searchResult != null && currentPhase == Phase.Complete && !EditorApplication.isPlaying && searchResult.InitialSceneSetupConfigured )
if( core != null )
core.SaveCache();
SavePrefs();
if( searchResult != null && currentPhase == Phase.Complete && !EditorApplication.isPlaying && searchResult.IsSceneSetupDifferentThanCurrentSetup() )
{
if( EditorUtility.DisplayDialog( "Scenes", "Restore initial scene setup?", "Yes", "Leave it as is" ) )
searchResult.RestoreInitialSceneSetup();
}
}
private void SavePrefs()
{
SceneSearchMode sceneSearchMode = SceneSearchMode.None;
if( searchInOpenScenes )
sceneSearchMode |= SceneSearchMode.OpenScenes;
if( searchInScenesInBuild )
sceneSearchMode |= searchInScenesInBuildTickedOnly ? SceneSearchMode.ScenesInBuildSettingsTickedOnly : SceneSearchMode.ScenesInBuildSettingsAll;
if( searchInAllScenes )
sceneSearchMode |= SceneSearchMode.AllScenes;
EditorPrefs.SetInt( PREFS_SEARCH_SCENES, (int) sceneSearchMode );
EditorPrefs.SetBool( PREFS_SEARCH_ASSETS, searchInAssetsFolder );
EditorPrefs.SetInt( PREFS_SEARCH_DEPTH_LIMIT, searchDepthLimit );
EditorPrefs.SetInt( PREFS_SEARCH_FIELDS, (int) fieldModifiers );
EditorPrefs.SetInt( PREFS_SEARCH_PROPERTIES, (int) propertyModifiers );
EditorPrefs.SetInt( PREFS_PATH_DRAWING_MODE, (int) searchResultDrawParameters.pathDrawingMode );
EditorPrefs.SetBool( PREFS_SHOW_TOOLTIPS, searchResultDrawParameters.showTooltips );
}
private void LoadPrefs()
{
SceneSearchMode sceneSearchMode = (SceneSearchMode) EditorPrefs.GetInt( PREFS_SEARCH_SCENES, (int) ( SceneSearchMode.OpenScenes | SceneSearchMode.ScenesInBuildSettingsTickedOnly | SceneSearchMode.AllScenes ) );
searchInOpenScenes = ( sceneSearchMode & SceneSearchMode.OpenScenes ) == SceneSearchMode.OpenScenes;
searchInScenesInBuild = ( sceneSearchMode & SceneSearchMode.ScenesInBuildSettingsAll ) == SceneSearchMode.ScenesInBuildSettingsAll || ( sceneSearchMode & SceneSearchMode.ScenesInBuildSettingsTickedOnly ) == SceneSearchMode.ScenesInBuildSettingsTickedOnly;
searchInScenesInBuildTickedOnly = ( sceneSearchMode & SceneSearchMode.ScenesInBuildSettingsAll ) != SceneSearchMode.ScenesInBuildSettingsAll;
searchInAllScenes = ( sceneSearchMode & SceneSearchMode.AllScenes ) == SceneSearchMode.AllScenes;
searchInAssetsFolder = EditorPrefs.GetBool( PREFS_SEARCH_ASSETS, true );
searchDepthLimit = EditorPrefs.GetInt( PREFS_SEARCH_DEPTH_LIMIT, 4 );
// Fetch public, protected and private non-static fields and properties from objects by default
fieldModifiers = (BindingFlags) EditorPrefs.GetInt( PREFS_SEARCH_FIELDS, (int) ( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic ) );
propertyModifiers = (BindingFlags) EditorPrefs.GetInt( PREFS_SEARCH_PROPERTIES, (int) ( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic ) );
try
{
searchResultDrawParameters.pathDrawingMode = (PathDrawingMode) EditorPrefs.GetInt( PREFS_PATH_DRAWING_MODE, (int) PathDrawingMode.ShortRelevantParts );
}
catch
{
searchResultDrawParameters.pathDrawingMode = PathDrawingMode.ShortRelevantParts;
}
searchResultDrawParameters.showTooltips = EditorPrefs.GetBool( PREFS_SHOW_TOOLTIPS, false );
}
private void Update()
{
// Refresh the window at a regular interval in play mode to update the tooltip
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
namespace AssetUsageDetectorNamespace
namespace AssetUsageDetectorNamespace.Extras
{
public class EmptyEnumerator<T> : IEnumerable<T>, IEnumerator<T>
{
@@ -4,7 +4,7 @@ using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace AssetUsageDetectorNamespace
namespace AssetUsageDetectorNamespace.Extras
{
[Serializable]
public class ObjectToSearch
@@ -1,4 +1,5 @@
using System;
using AssetUsageDetectorNamespace.Extras;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.SceneManagement;
@@ -121,6 +122,38 @@ namespace AssetUsageDetectorNamespace
return null;
}
// Returns if RestoreInitialSceneSetup will have any effect on the current scene setup
public bool IsSceneSetupDifferentThanCurrentSetup()
{
if( initialSceneSetup == null )
return false;
SceneSetup[] sceneFinalSetup = EditorSceneManager.GetSceneManagerSetup();
if( initialSceneSetup.Length != sceneFinalSetup.Length )
return true;
for( int i = 0; i < sceneFinalSetup.Length; i++ )
{
bool sceneIsOneOfInitials = false;
for( int j = 0; j < initialSceneSetup.Length; j++ )
{
if( sceneFinalSetup[i].path == initialSceneSetup[j].path )
{
if( sceneFinalSetup[i].isLoaded != initialSceneSetup[j].isLoaded )
return true;
sceneIsOneOfInitials = true;
break;
}
}
if( !sceneIsOneOfInitials )
return true;
}
return false;
}
// Close the scenes that were not part of the initial scene setup
// Returns true if initial scene setup is restored successfully
public bool RestoreInitialSceneSetup()
@@ -5,7 +5,7 @@ using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace AssetUsageDetectorNamespace
namespace AssetUsageDetectorNamespace.Extras
{
public static class Utilities
{
@@ -13,7 +13,9 @@ namespace AssetUsageDetectorNamespace
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( Matrix4x4 ), typeof( AnimationCurve ), typeof( Gradient ), typeof( RectOffset ),
typeof( Assembly ) // Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime
};
private static readonly HashSet<string> folderContentsSet = new HashSet<string>();
@@ -1,7 +1,7 @@
using System;
using System.Reflection;
namespace AssetUsageDetectorNamespace
namespace AssetUsageDetectorNamespace.Extras
{
// Delegate to get the value of a variable (either field or property)
public delegate object VariableGetVal( object obj );
+1 -1
View File
@@ -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
Simply open Window-Asset Usage Detector, select some assets, tweak the settings and GO!
Open Window - Asset Usage Detector window, configure the settings and hit GO!
3. KNOWN LIMITATIONS
- static variables are not searched