diff --git a/AssetUsageDetector.unitypackage b/AssetUsageDetector.unitypackage index 9ad4aa0..5e3930a 100644 Binary files a/AssetUsageDetector.unitypackage and b/AssetUsageDetector.unitypackage differ diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index a143846..22f4a1e 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -1042,11 +1042,7 @@ namespace AssetUsageDetectorNamespace continue; // Skip primitive types - Type fieldType = fields[i].FieldType; - if( fieldType.IsPrimitive || fieldType == typeof( string ) || fieldType.IsEnum ) - continue; - - if( fieldType.IsPrimitiveUnityType() ) + if( fields[i].FieldType.IsPrimitiveUnityType() ) continue; VariableGetVal getter = fields[i].CreateGetter( type ); @@ -1071,11 +1067,7 @@ namespace AssetUsageDetectorNamespace continue; // Skip primitive types - Type propertyType = properties[i].PropertyType; - if( propertyType.IsPrimitive || propertyType == typeof( string ) || propertyType.IsEnum ) - continue; - - if( propertyType.IsPrimitiveUnityType() ) + if( properties[i].PropertyType.IsPrimitiveUnityType() ) continue; // Additional filtering for properties: @@ -1104,7 +1096,7 @@ namespace AssetUsageDetectorNamespace else { VariableGetVal getter = properties[i].CreateGetter(); - if( getter != null ) + if( getter != null && !properties[i].IsOverridden() ) // No need to check properties with 'override' keyword validVariables.Add( new VariableGetterHolder( properties[i], getter, properties[i].IsSerializable() ) ); } } diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs index 42e2d19..3818463 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs @@ -52,6 +52,8 @@ namespace AssetUsageDetectorNamespace.Extras private Vector2 scrollPosition = Vector2.zero; + private static AssetUsageDetectorWindow visibleWindow = null; + // Add "Asset Usage Detector" menu item to the Window menu [MenuItem( "Window/Asset Usage Detector" )] private static void Init() @@ -88,29 +90,54 @@ namespace AssetUsageDetectorNamespace.Extras public static void ShowAndSearch( AssetUsageDetector.Parameters searchParameters ) { if( searchParameters == null ) - Debug.LogError( "searchParameters can't be null!" ); - else if( searchParameters.objectsToSearch.IsEmpty() ) - Debug.LogError( "searchParameters.objectsToSearch can't be empty!" ); - else if( !EditorApplication.isPlaying && !Utilities.AreScenesSaved() ) - Debug.LogError( "Save open scenes first!" ); - else { - Init(); - AssetUsageDetectorWindow window = GetWindow(); - - if( window.objectsToSearch == null ) - window.objectsToSearch = new List(); - else - window.objectsToSearch.Clear(); - - foreach( Object obj in searchParameters.objectsToSearch ) - window.objectsToSearch.Add( new ObjectToSearch( obj ) ); - - window.errorMessage = string.Empty; - window.currentPhase = Phase.Processing; - window.searchResult = window.core.Run( searchParameters ); - window.currentPhase = Phase.Complete; + Debug.LogError( "searchParameters can't be null!" ); + return; } + + if( searchParameters.objectsToSearch.IsEmpty() ) + { + Debug.LogError( "searchParameters.objectsToSearch can't be empty!" ); + return; + } + + if( !EditorApplication.isPlaying && !Utilities.AreScenesSaved() ) + { + if( visibleWindow == null || visibleWindow.currentPhase != Phase.Complete ) + { + Debug.LogError( "Save open scenes first!" ); + return; + } + + if( !visibleWindow.ReturnToSetupPhase( true ) ) + { + Debug.LogError( "Need to reset the previous search first!" ); + return; + } + } + + Init(); + + if( visibleWindow.objectsToSearch == null ) + visibleWindow.objectsToSearch = new List(); + else + visibleWindow.objectsToSearch.Clear(); + + foreach( Object obj in searchParameters.objectsToSearch ) + visibleWindow.objectsToSearch.Add( new ObjectToSearch( obj ) ); + + // This enumerator includes sub-assets in the search + searchParameters.objectsToSearch = new ObjectToSearchEnumerator( visibleWindow.objectsToSearch ); + + visibleWindow.errorMessage = string.Empty; + visibleWindow.currentPhase = Phase.Processing; + visibleWindow.searchResult = visibleWindow.core.Run( searchParameters ); + visibleWindow.currentPhase = Phase.Complete; + } + + private void OnEnable() + { + visibleWindow = this; } private void OnDestroy() @@ -125,6 +152,8 @@ namespace AssetUsageDetectorNamespace.Extras if( EditorUtility.DisplayDialog( "Scenes", "Restore initial scene setup?", "Yes", "Leave it as is" ) ) searchResult.RestoreInitialSceneSetup(); } + + visibleWindow = null; } private void SavePrefs() @@ -658,15 +687,17 @@ namespace AssetUsageDetectorNamespace.Extras } } - private void ReturnToSetupPhase( bool restoreInitialSceneSetup ) + private bool ReturnToSetupPhase( bool restoreInitialSceneSetup ) { if( searchResult != null && restoreInitialSceneSetup && !EditorApplication.isPlaying && !searchResult.RestoreInitialSceneSetup() ) - return; + return false; searchResult = null; errorMessage = string.Empty; currentPhase = Phase.Setup; + + return true; } } } \ No newline at end of file diff --git a/Plugins/AssetUsageDetector/Editor/Enumerators.cs b/Plugins/AssetUsageDetector/Editor/Enumerators.cs index 83a97f2..42df39d 100644 --- a/Plugins/AssetUsageDetector/Editor/Enumerators.cs +++ b/Plugins/AssetUsageDetector/Editor/Enumerators.cs @@ -28,79 +28,89 @@ namespace AssetUsageDetectorNamespace.Extras } } - public class ObjectToSearchEnumerator : IEnumerable, IEnumerator + public class ObjectToSearchEnumerator : IEnumerable { - public Object Current + public class Enumerator : IEnumerator { - get + public Object Current { - if( subAssetIndex < 0 ) - return source[index].obj; + get + { + if( subAssetIndex < 0 ) + return source[index].obj; - return source[index].subAssets[subAssetIndex].subAsset; + return source[index].subAssets[subAssetIndex].subAsset; + } + } + + object IEnumerator.Current { get { return Current; } } + + private List source; + private int index; + private int subAssetIndex; + + public Enumerator( List source ) + { + this.source = source; + Reset(); + } + + public void Dispose() + { + source = null; + } + + public bool MoveNext() + { + if( subAssetIndex < -1 ) + { + subAssetIndex = -1; + + if( ++index >= source.Count ) + return false; + + // Skip folder assets in the enumeration, AssetUsageDetector expands encountered folders automatically + // and we don't want that to happen as source[index].subAssets already contains the folder's contents + if( !source[index].obj.IsFolder() ) + return true; + } + + List subAssets = source[index].subAssets; + if( subAssets != null ) + { + while( ++subAssetIndex < subAssets.Count && !subAssets[subAssetIndex].shouldSearch ) + continue; + + if( subAssetIndex < subAssets.Count ) + return true; + } + + subAssetIndex = -2; + return MoveNext(); + } + + public void Reset() + { + index = -1; + subAssetIndex = -2; } } - object IEnumerator.Current { get { return Current; } } - - private List source; - private int index; - private int subAssetIndex; + private readonly List source; public ObjectToSearchEnumerator( List source ) { this.source = source; - Reset(); - } - - public void Dispose() - { - source = null; - } - - public bool MoveNext() - { - if( subAssetIndex < -1 ) - { - subAssetIndex = -1; - - if( ++index >= source.Count ) - return false; - - // Skip folder assets in the enumeration, AssetUsageDetector expands encountered folders automatically - // and we don't want that to happen as source[index].subAssets already contains the folder's contents - if( !source[index].obj.IsFolder() ) - return true; - } - - List subAssets = source[index].subAssets; - if( subAssets != null ) - { - while( ++subAssetIndex < subAssets.Count && !subAssets[subAssetIndex].shouldSearch ) - continue; - - if( subAssetIndex < subAssets.Count ) - return true; - } - - subAssetIndex = -2; - return MoveNext(); - } - - public void Reset() - { - index = -1; - subAssetIndex = -2; } public IEnumerator GetEnumerator() { - return this; + return new Enumerator( source ); } IEnumerator IEnumerable.GetEnumerator() { - return this; + return GetEnumerator(); } } } \ No newline at end of file diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index 5787f7e..8c13dd3 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -16,7 +16,19 @@ namespace AssetUsageDetectorNamespace.Extras 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( Assembly ) // Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime + typeof( Assembly ), // Searching assembly variables for reference throws InvalidCastException on .NET 4.0 runtime + 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( Assembly[] ), + typeof( List ), typeof( List ), typeof( List ), typeof( List ), typeof( List ), + typeof( List ), typeof( List ), typeof( List ), typeof( List ), typeof( List ), + typeof( List ), typeof( List ), typeof( List ), typeof( List ), + typeof( List ), typeof( List ), typeof( List ), typeof( List ), + typeof( List ), typeof( List ), typeof( List ), typeof( List ), + typeof( List ), typeof( List ), typeof( List ), typeof( List ), typeof( List ) }; private static readonly HashSet folderContentsSet = new HashSet(); @@ -296,7 +308,14 @@ namespace AssetUsageDetectorNamespace.Extras // Check if the type is a common Unity type (let's call them primitives) public static bool IsPrimitiveUnityType( this Type type ) { - return type.IsPrimitive || primitiveUnityTypes.Contains( type ); + 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 function for a field