mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-19 14:32:37 +08:00
Fixes #10
This commit is contained in:
Binary file not shown.
@@ -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() ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<AssetUsageDetectorWindow>();
|
||||
|
||||
if( window.objectsToSearch == null )
|
||||
window.objectsToSearch = new List<ObjectToSearch>();
|
||||
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<ObjectToSearch>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -28,79 +28,89 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
}
|
||||
}
|
||||
|
||||
public class ObjectToSearchEnumerator : IEnumerable<Object>, IEnumerator<Object>
|
||||
public class ObjectToSearchEnumerator : IEnumerable<Object>
|
||||
{
|
||||
public Object Current
|
||||
public class Enumerator : IEnumerator<Object>
|
||||
{
|
||||
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<ObjectToSearch> source;
|
||||
private int index;
|
||||
private int subAssetIndex;
|
||||
|
||||
public Enumerator( List<ObjectToSearch> 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<ObjectToSearch.SubAsset> 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<ObjectToSearch> source;
|
||||
private int index;
|
||||
private int subAssetIndex;
|
||||
private readonly List<ObjectToSearch> source;
|
||||
|
||||
public ObjectToSearchEnumerator( List<ObjectToSearch> 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<ObjectToSearch.SubAsset> 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<Object> GetEnumerator()
|
||||
{
|
||||
return this;
|
||||
return new Enumerator( source );
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return this;
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<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> ), typeof( List<Assembly> )
|
||||
};
|
||||
|
||||
private static readonly HashSet<string> folderContentsSet = new HashSet<string>();
|
||||
@@ -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 <get> function for a field
|
||||
|
||||
Reference in New Issue
Block a user