diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.Editor.asmdef b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.Editor.asmdef index c2e6e32..f02732f 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.Editor.asmdef +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.Editor.asmdef @@ -18,6 +18,11 @@ "name": "com.unity.addressables", "expression": "0.0.0", "define": "ASSET_USAGE_ADDRESSABLES" + }, + { + "name": "com.unity.visualeffectgraph", + "expression": "0.0.0", + "define": "ASSET_USAGE_VFX_GRAPH" } ], "noEngineReferences": false diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index f1ac18a..aa7a107 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -1031,7 +1031,7 @@ namespace AssetUsageDetectorNamespace callStack.Add( unityObject ); // Search the Object in detail - Func searchFunction; + Func searchFunction; if( assetPath != null && extensionToSearchFunction.TryGetValue( Utilities.GetFileExtension( assetPath ), out searchFunction ) && AssetDatabase.IsMainAsset( unityObject ) ) result = searchFunction( unityObject ); else if( typeToSearchFunction.TryGetValue( unityObject.GetType(), out searchFunction ) ) diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs index 6690ee4..1446dc7 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs @@ -161,9 +161,9 @@ namespace AssetUsageDetectorNamespace #endregion // Dictionary to quickly find the function to search a specific type with - private Dictionary> typeToSearchFunction; + private Dictionary> typeToSearchFunction; // Dictionary to associate special file extensions with their search functions - private Dictionary> extensionToSearchFunction; + private Dictionary> extensionToSearchFunction; // An optimization to fetch & filter fields and properties of a class only once private readonly Dictionary typeToVariables = new Dictionary( 4096 ); @@ -199,19 +199,28 @@ namespace AssetUsageDetectorNamespace // Unity's internal function that returns a SerializedProperty's corresponding FieldInfo private delegate FieldInfo FieldInfoGetter( SerializedProperty p, out Type t ); - private FieldInfoGetter fieldInfoGetter; +#if UNITY_2019_3_OR_NEWER + private readonly FieldInfoGetter fieldInfoGetter = (FieldInfoGetter) Delegate.CreateDelegate( typeof( FieldInfoGetter ), typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoAndStaticTypeFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); +#else + private readonly FieldInfoGetter fieldInfoGetter = (FieldInfoGetter) Delegate.CreateDelegate( typeof( FieldInfoGetter ), typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); +#endif #if ASSET_USAGE_ADDRESSABLES - private delegate Sprite[] SpriteAtlasPackedSpritesGetter( SpriteAtlas atlas ); - private SpriteAtlasPackedSpritesGetter spriteAtlasPackedSpritesGetter; - private PropertyInfo assetReferenceSubObjectTypeGetter; + private readonly Func spriteAtlasPackedSpritesGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); + private readonly PropertyInfo assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); +#endif + +#if ASSET_USAGE_VFX_GRAPH + private readonly Func vfxResourceGetter = (Func) Delegate.CreateDelegate( typeof( Func ), typeof( Editor ).Assembly.GetType( "UnityEditor.VFX.VisualEffectResource" ).GetMethod( "GetResourceAtPath", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) ); + private readonly MethodInfo vfxResourceContentsGetter = typeof( Editor ).Assembly.GetType( "UnityEditor.VFX.VisualEffectResource" ).GetMethod( "GetContents", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); + private readonly MethodInfo vfxSerializableObjectValueGetter = Array.Find( Array.Find( AppDomain.CurrentDomain.GetAssemblies(), ( assembly ) => assembly.GetName().Name == "Unity.VisualEffectGraph.Editor" ).GetType( "UnityEditor.VFX.VFXSerializableObject" ).GetMethods( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ), ( methodInfo ) => methodInfo.Name == "Get" && !methodInfo.IsGenericMethod ); #endif private void InitializeSearchFunctionsData( Parameters searchParameters ) { if( typeToSearchFunction == null ) { - typeToSearchFunction = new Dictionary>() + typeToSearchFunction = new Dictionary>() { { typeof( GameObject ), SearchGameObject }, { typeof( Material ), SearchMaterial }, @@ -234,7 +243,7 @@ namespace AssetUsageDetectorNamespace if( extensionToSearchFunction == null ) { - extensionToSearchFunction = new Dictionary>() + extensionToSearchFunction = new Dictionary>() { { "compute", SearchShaderSecondaryAsset }, { "cginc", SearchShaderSecondaryAsset }, @@ -250,6 +259,11 @@ namespace AssetUsageDetectorNamespace #if UNITY_2018_1_OR_NEWER { "shadergraph", SearchShaderGraph }, { "shadersubgraph", SearchShaderGraph }, +#endif +#if ASSET_USAGE_VFX_GRAPH + { "vfx", SearchVFXGraphAsset }, + { "vfxoperator", SearchVFXGraphAsset }, + { "vfxblock", SearchVFXGraphAsset }, #endif }; } @@ -271,6 +285,9 @@ namespace AssetUsageDetectorNamespace #if UNITY_2018_1_OR_NEWER searchShaderGraphsForSubGraphs = false; #endif +#if ASSET_USAGE_VFX_GRAPH + bool searchVFXGraphs = false; +#endif foreach( Object obj in objectsToSearchSet ) { @@ -292,6 +309,10 @@ namespace AssetUsageDetectorNamespace #if UNITY_2017_3_OR_NEWER else if( obj is UnityEditorInternal.AssemblyDefinitionAsset ) assemblyDefinitionFilesToSearch[AssetDatabase.GetAssetPath( obj )] = obj; +#endif +#if ASSET_USAGE_VFX_GRAPH + else if( !searchVFXGraphs && ( obj is Shader || obj is Mesh || obj.GetType().Name.StartsWithFast( "PointCache" ) || obj.GetType().Name == "ShaderGraphVfxAsset" ) ) + searchVFXGraphs = true; #endif } @@ -349,23 +370,19 @@ namespace AssetUsageDetectorNamespace } #endif -#if UNITY_2019_3_OR_NEWER - MethodInfo fieldInfoGetterMethod = typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoAndStaticTypeFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ); -#else - MethodInfo fieldInfoGetterMethod = typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ); -#endif - fieldInfoGetter = (FieldInfoGetter) Delegate.CreateDelegate( typeof( FieldInfoGetter ), fieldInfoGetterMethod ); - -#if ASSET_USAGE_ADDRESSABLES - MethodInfo spriteAtlasPackedSpritesGetterMethod = typeof( SpriteAtlasExtensions ).GetMethod( "GetPackedSprites", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ); - spriteAtlasPackedSpritesGetter = (SpriteAtlasPackedSpritesGetter) Delegate.CreateDelegate( typeof( SpriteAtlasPackedSpritesGetter ), spriteAtlasPackedSpritesGetterMethod ); - assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance ); +#if ASSET_USAGE_VFX_GRAPH + if( searchTextureReferences || searchVFXGraphs ) + { + alwaysSearchedExtensionsSet.Add( "vfx" ); + alwaysSearchedExtensionsSet.Add( "vfxoperator" ); + alwaysSearchedExtensionsSet.Add( "vfxblock" ); + } #endif } - private ReferenceNode SearchGameObject( Object unityObject ) + private ReferenceNode SearchGameObject( object obj ) { - GameObject go = (GameObject) unityObject; + GameObject go = (GameObject) obj; ReferenceNode referenceNode = PopReferenceNode( go ); // Check if this GameObject's prefab is one of the selected assets @@ -397,9 +414,9 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchComponent( Object unityObject ) + private ReferenceNode SearchComponent( object obj ) { - Component component = (Component) unityObject; + Component component = (Component) obj; // Ignore Transform component (no object field to search for) if( component is Transform ) @@ -610,11 +627,11 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchMaterial( Object unityObject ) + private ReferenceNode SearchMaterial( object obj ) { const string TEXTURE_PROPERTY_PREFIX = "m_SavedProperties.m_TexEnvs["; - Material material = (Material) unityObject; + Material material = (Material) obj; ReferenceNode referenceNode = PopReferenceNode( material ); // We used to search only the shader and the Texture properties in this function but it has changed for 2 major reasons: @@ -689,14 +706,14 @@ namespace AssetUsageDetectorNamespace } // Searches default Texture values assigned to shader properties, as well as #include references in shader source code - private ReferenceNode SearchShader( Object unityObject ) + private ReferenceNode SearchShader( object obj ) { - Shader shader = (Shader) unityObject; + Shader shader = (Shader) obj; ReferenceNode referenceNode = PopReferenceNode( shader ); if( searchTextureReferences ) { - ShaderImporter shaderImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( unityObject ) ) as ShaderImporter; + ShaderImporter shaderImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( shader ) ) as ShaderImporter; if( shaderImporter != null ) { int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader ); @@ -731,20 +748,20 @@ namespace AssetUsageDetectorNamespace } // Searches .compute, .cginc, .cg, .hlsl and .glslinc assets for #include references - private ReferenceNode SearchShaderSecondaryAsset( Object unityObject ) + private ReferenceNode SearchShaderSecondaryAsset( object obj ) { if( shaderIncludesToSearchSet.Count == 0 ) return null; - ReferenceNode referenceNode = PopReferenceNode( unityObject ); + ReferenceNode referenceNode = PopReferenceNode( obj ); SearchShaderSourceCodeForCGIncludes( referenceNode ); return referenceNode; } // Searches class/interface inheritances and default UnityEngine.Object values assigned to script variables - private ReferenceNode SearchMonoScript( Object unityObject ) + private ReferenceNode SearchMonoScript( object obj ) { - MonoScript script = (MonoScript) unityObject; + MonoScript script = (MonoScript) obj; Type scriptType = script.GetClass(); if( scriptType == null || ( !scriptType.IsSubclassOf( typeof( MonoBehaviour ) ) && !scriptType.IsSubclassOf( typeof( ScriptableObject ) ) ) ) return null; @@ -758,7 +775,7 @@ namespace AssetUsageDetectorNamespace referenceNode.AddLinkTo( GetReferenceNode( monoScriptsToSearch[i] ), monoScriptsToSearchTypes[i].IsInterface ? "Implements interface" : "Extends class" ); } - MonoImporter scriptImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( unityObject ) ) as MonoImporter; + MonoImporter scriptImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( script ) ) as MonoImporter; if( scriptImporter != null ) { VariableGetterHolder[] variables = GetFilteredVariablesForType( scriptType ); @@ -781,9 +798,9 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchAnimatorController( Object unityObject ) + private ReferenceNode SearchAnimatorController( object obj ) { - RuntimeAnimatorController controller = (RuntimeAnimatorController) unityObject; + RuntimeAnimatorController controller = (RuntimeAnimatorController) obj; ReferenceNode referenceNode = PopReferenceNode( controller ); if( controller is AnimatorController ) @@ -845,9 +862,9 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchAnimatorStateMachine( Object unityObject ) + private ReferenceNode SearchAnimatorStateMachine( object obj ) { - AnimatorStateMachine animatorStateMachine = (AnimatorStateMachine) unityObject; + AnimatorStateMachine animatorStateMachine = (AnimatorStateMachine) obj; ReferenceNode referenceNode = PopReferenceNode( animatorStateMachine ); ChildAnimatorStateMachine[] stateMachines = animatorStateMachine.stateMachines; @@ -877,9 +894,9 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchAnimatorState( Object unityObject ) + private ReferenceNode SearchAnimatorState( object obj ) { - AnimatorState animatorState = (AnimatorState) unityObject; + AnimatorState animatorState = (AnimatorState) obj; ReferenceNode referenceNode = PopReferenceNode( animatorState ); referenceNode.AddLinkTo( SearchObject( animatorState.motion ), "Motion" ); @@ -906,15 +923,15 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchAnimatorStateTransition( Object unityObject ) + private ReferenceNode SearchAnimatorStateTransition( object obj ) { // Don't search AnimatorStateTransition objects, it will just return duplicate results of SearchAnimatorStateMachine - return PopReferenceNode( unityObject ); + return PopReferenceNode( obj ); } - private ReferenceNode SearchBlendTree( Object unityObject ) + private ReferenceNode SearchBlendTree( object obj ) { - BlendTree blendTree = (BlendTree) unityObject; + BlendTree blendTree = (BlendTree) obj; ReferenceNode referenceNode = PopReferenceNode( blendTree ); ChildMotion[] children = blendTree.children; @@ -929,9 +946,9 @@ namespace AssetUsageDetectorNamespace return referenceNode; } - private ReferenceNode SearchAnimationClip( Object unityObject ) + private ReferenceNode SearchAnimationClip( object obj ) { - AnimationClip clip = (AnimationClip) unityObject; + AnimationClip clip = (AnimationClip) obj; ReferenceNode referenceNode = PopReferenceNode( clip ); // Get all curves from animation clip @@ -983,17 +1000,17 @@ namespace AssetUsageDetectorNamespace } // TerrainData's properties like tree/detail/layer definitions aren't exposed to SerializedObject so use reflection instead - private ReferenceNode SearchTerrainData( Object unityObject ) + private ReferenceNode SearchTerrainData( object obj ) { - ReferenceNode referenceNode = PopReferenceNode( unityObject ); + ReferenceNode referenceNode = PopReferenceNode( obj ); SearchVariablesWithReflection( referenceNode ); return referenceNode; } #if UNITY_2017_1_OR_NEWER - private ReferenceNode SearchSpriteAtlas( Object unityObject ) + private ReferenceNode SearchSpriteAtlas( object obj ) { - SpriteAtlas spriteAtlas = (SpriteAtlas) unityObject; + SpriteAtlas spriteAtlas = (SpriteAtlas) obj; ReferenceNode referenceNode = PopReferenceNode( spriteAtlas ); SerializedObject spriteAtlasSO = new SerializedObject( spriteAtlas ); @@ -1083,13 +1100,13 @@ namespace AssetUsageDetectorNamespace #if UNITY_2017_3_OR_NEWER // Find references from an Assembly Definition File to its Assembly Definition References - private ReferenceNode SearchAssemblyDefinitionFile( Object unityObject ) + private ReferenceNode SearchAssemblyDefinitionFile( object obj ) { if( assemblyDefinitionFilesToSearch.Count == 0 ) return null; - AssemblyDefinitionReferences assemblyDefinitionFile = JsonUtility.FromJson( ( (TextAsset) unityObject ).text ); - ReferenceNode referenceNode = PopReferenceNode( unityObject ); + AssemblyDefinitionReferences assemblyDefinitionFile = JsonUtility.FromJson( ( (TextAsset) obj ).text ); + ReferenceNode referenceNode = PopReferenceNode( obj ); if( !string.IsNullOrEmpty( assemblyDefinitionFile.reference ) ) { @@ -1123,15 +1140,15 @@ namespace AssetUsageDetectorNamespace #if UNITY_2018_1_OR_NEWER // Searches Shader Graph assets for references - private ReferenceNode SearchShaderGraph( Object unityObject ) + private ReferenceNode SearchShaderGraph( object obj ) { if( !searchTextureReferences && !searchShaderGraphsForSubGraphs && shaderIncludesToSearchSet.Count == 0 ) return null; - ReferenceNode referenceNode = PopReferenceNode( unityObject ); + ReferenceNode referenceNode = PopReferenceNode( obj ); // Shader Graph assets are JSON files, they must be crawled manually to find references - string graphJson = File.ReadAllText( AssetDatabase.GetAssetPath( unityObject ) ); + string graphJson = File.ReadAllText( AssetDatabase.GetAssetPath( (Object) obj ) ); if( graphJson.IndexOf( "\"m_ObjectId\"", 0, Mathf.Min( 200, graphJson.Length ) ) >= 0 ) { // New Shader Graph serialization format is used: https://github.com/Unity-Technologies/Graphics/pull/222 @@ -1252,6 +1269,19 @@ namespace AssetUsageDetectorNamespace } #endif +#if ASSET_USAGE_VFX_GRAPH + private ReferenceNode SearchVFXGraphAsset( object obj ) + { + ReferenceNode referenceNode = PopReferenceNode( obj ); + + object vfxResource = vfxResourceGetter( AssetDatabase.GetAssetPath( (Object) obj ) ); + foreach( Object vfxResourceContent in (Object[]) vfxResourceContentsGetter.Invoke( vfxResource, null ) ) + referenceNode.AddLinkTo( SearchObject( vfxResourceContent ) ); + + return referenceNode; + } +#endif + // Find references from an Animation/Animator component to the objects that it animates private void SearchAnimatedObjects( ReferenceNode referenceNode ) { @@ -1427,14 +1457,23 @@ namespace AssetUsageDetectorNamespace #endif case SerializedPropertyType.Generic: #if ASSET_USAGE_ADDRESSABLES - AssetReference assetReference; - if( searchParameters.addressablesSupport && iterator.type.StartsWithFast( "AssetReference" ) && ( assetReference = GetRawSerializedPropertyValue( iterator ) as AssetReference ) != null ) + if( searchParameters.addressablesSupport && iterator.type.StartsWithFast( "AssetReference" ) && GetRawSerializedPropertyValue( iterator ) is AssetReference assetReference ) { propertyValue = GetAddressablesAssetReferenceValue( assetReference ); searchResult = SearchObject( PreferablyGameObject( propertyValue ) ); enterChildren = false; } else +#endif +#if ASSET_USAGE_VFX_GRAPH + if( vfxSerializableObjectValueGetter != null && iterator.type == "VFXSerializableObject" && GetRawSerializedPropertyValue( iterator ) is object vfxSerializableObject ) + { + object vfxSerializableObjectValue = vfxSerializableObjectValueGetter.Invoke( vfxSerializableObject, null ); + propertyValue = vfxSerializableObjectValue as Object; + searchResult = SearchObject( PreferablyGameObject( vfxSerializableObjectValue ) ); + enterChildren = false; + } + else #endif { propertyValue = null; diff --git a/Plugins/AssetUsageDetector/README.txt b/Plugins/AssetUsageDetector/README.txt index c14c2aa..2ae615e 100644 --- a/Plugins/AssetUsageDetector/README.txt +++ b/Plugins/AssetUsageDetector/README.txt @@ -1,4 +1,4 @@ -= Asset Usage Detector (v2.3.1) = += Asset Usage Detector (v2.4.0) = Online documentation available at: https://github.com/yasirkula/UnityAssetUsageDetector E-mail: yasirkula@gmail.com diff --git a/package.json b/package.json index 35157e1..395f1cb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.yasirkula.assetusagedetector", "displayName": "Asset Usage Detector", - "version": "2.3.1", + "version": "2.4.0", "documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector", "changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases", "licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",