From 6280305a63227d8a5fc23b944d98cb8cd5a242cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=BCleyman=20Yasir=20KULA?= Date: Thu, 16 Jul 2020 22:39:41 +0300 Subject: [PATCH] - Searching for references of a prefab stage object in newest Unity versions wasn't working, fixed - Fields marked with HideInInspector (and with SerializeReference, possibly) weren't searched for references when SearchVariablesWithSerializedObject was used, fixed --- .../Editor/AssetUsageDetector.cs | 2 +- .../AssetUsageDetectorSearchFunctions.cs | 34 +++++++++++++++++++ .../Editor/AssetUsageDetectorWindow.cs | 4 +-- .../AssetUsageDetector/Editor/Utilities.cs | 8 +++-- package.json | 2 +- 5 files changed, 43 insertions(+), 7 deletions(-) diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs index 74ee259..6a98605 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs @@ -613,7 +613,7 @@ namespace AssetUsageDetectorNamespace // When searching for references of a prefab stage object, try adding its corresponding prefab asset to the searched assets, as well if( openPrefabStage != null && openPrefabStagePrefabAsset != null && obj is GameObject && openPrefabStage.IsPartOfPrefabContents( (GameObject) obj ) ) { - GameObject prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( openPrefabStagePrefabAsset ); + GameObject prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( openPrefabStage.prefabContentsRoot, openPrefabStagePrefabAsset ); if( prefabStageObjectSource != null ) AddSearchedObjectToFilteredSets( prefabStageObjectSource, expandGameObjects ); } diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs index 83c1c9f..77d0852 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs @@ -165,6 +165,9 @@ namespace AssetUsageDetectorNamespace private readonly Dictionary typeToVariables = new Dictionary( 4096 ); private readonly List validVariables = new List( 32 ); + // Dictionary to store whether or not instances of a Type can be searched with SearchVariablesWithSerializedObject + private readonly Dictionary typesSearchabilityWithSerializedObject = new Dictionary( 4096 ); + // Path(s) of .cginc, .cg, .hlsl and .glslinc assets in assetsToSearchSet private readonly HashSet shaderIncludesToSearchSet = new HashSet(); @@ -964,6 +967,22 @@ namespace AssetUsageDetectorNamespace { if( !isInPlayMode || referenceNode.nodeObject.IsAsset() ) { + // Some Types can be searched with SearchVariablesWithReflection only + Type nodeObjectType = referenceNode.nodeObject.GetType(); + bool typeSearchability; + if( !typesSearchabilityWithSerializedObject.TryGetValue( nodeObjectType, out typeSearchability ) ) + { + // typesSearchabilityWithSerializedObject is updated when GetFilteredVariablesForType is called + GetFilteredVariablesForType( nodeObjectType ); + typeSearchability = typesSearchabilityWithSerializedObject[nodeObjectType]; + } + + if( !typeSearchability ) + { + SearchVariablesWithReflection( referenceNode ); + return; + } + SerializedObject so = new SerializedObject( (Object) referenceNode.nodeObject ); SerializedProperty iterator = so.GetIterator(); if( iterator.NextVisible( true ) ) @@ -1065,6 +1084,10 @@ namespace AssetUsageDetectorNamespace if( typeToVariables.TryGetValue( type, out result ) ) return result; + // SearchVariablesWithSerializedObject function can't iterate over fields that have HideInInspector + // or SerializeReference attributes. Types that have such variables must be searched with reflection + bool searchabilityWithSerializedObject = true; + // This is the first time this type of object is seen, filter and cache its variables // Variable filtering process: // 1- skip Obsolete variables @@ -1103,7 +1126,17 @@ namespace AssetUsageDetectorNamespace VariableGetVal getter = field.CreateGetter( type ); if( getter != null ) + { validVariables.Add( new VariableGetterHolder( field, getter, searchSerializableVariablesOnly ? field.IsSerializable() : true ) ); + + if( searchabilityWithSerializedObject && Attribute.IsDefined( field, typeof( HideInInspector ) ) ) + searchabilityWithSerializedObject = false; + +#if UNITY_2019_3_OR_NEWER + if( searchabilityWithSerializedObject && Attribute.IsDefined( field, typeof( SerializeReference ) ) ) + searchabilityWithSerializedObject = false; +#endif + } } currType = currType.BaseType; @@ -1182,6 +1215,7 @@ namespace AssetUsageDetectorNamespace // Cache the filtered fields typeToVariables.Add( type, result ); + typesSearchabilityWithSerializedObject.Add( type, searchabilityWithSerializedObject ); return result; } diff --git a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs index 3d21800..0c1a34a 100644 --- a/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs +++ b/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs @@ -690,7 +690,7 @@ namespace AssetUsageDetectorNamespace Object obj = objectsToSearch[i].obj; if( obj != null && !obj.Equals( null ) && obj is GameObject && prefabStage.IsPartOfPrefabContents( (GameObject) obj ) ) { - GameObject prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( prefabAsset ); + GameObject prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( prefabStage.prefabContentsRoot, prefabAsset ); if( prefabStageObjectSource != null ) objectsToSearch[i].obj = prefabStageObjectSource; @@ -700,7 +700,7 @@ namespace AssetUsageDetectorNamespace obj = subAssets[j].subAsset; if( obj != null && !obj.Equals( null ) && obj is GameObject && prefabStage.IsPartOfPrefabContents( (GameObject) obj ) ) { - prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( prefabAsset ); + prefabStageObjectSource = ( (GameObject) obj ).FollowSymmetricHierarchy( prefabStage.prefabContentsRoot, prefabAsset ); if( prefabStageObjectSource != null ) subAssets[j].subAsset = prefabStageObjectSource; } diff --git a/Plugins/AssetUsageDetector/Editor/Utilities.cs b/Plugins/AssetUsageDetector/Editor/Utilities.cs index 7b389f8..7d164b0 100644 --- a/Plugins/AssetUsageDetector/Editor/Utilities.cs +++ b/Plugins/AssetUsageDetector/Editor/Utilities.cs @@ -250,7 +250,7 @@ namespace AssetUsageDetectorNamespace if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() && assetPath == openPrefabStage.prefabAssetPath ) { - GameObject prefabStageGO = FollowSymmetricHierarchy( (GameObject) obj, openPrefabStage.prefabContentsRoot ); + GameObject prefabStageGO = FollowSymmetricHierarchy( (GameObject) obj, ( (GameObject) obj ).transform.root.gameObject, openPrefabStage.prefabContentsRoot ); if( prefabStageGO != null ) { objTR = prefabStageGO.transform; @@ -281,10 +281,12 @@ namespace AssetUsageDetectorNamespace return selection; } - public static GameObject FollowSymmetricHierarchy( this GameObject go, GameObject symmetricRoot ) + // We are passing "go"s root Transform to thisRoot parameter. If we use go.transform.root instead, when we are in prefab mode on + // newer Unity versions, it points to the preview scene at the root of the prefab stage instead of pointing to the actual root of "go" + public static GameObject FollowSymmetricHierarchy( this GameObject go, GameObject thisRoot, GameObject symmetricRoot ) { Transform target = go.transform; - Transform root1 = target.root; + Transform root1 = thisRoot.transform; Transform root2 = symmetricRoot.transform; while( root1 != target ) { diff --git a/package.json b/package.json index f45a415..7805a01 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.yasirkula.assetusagedetector", "displayName": "Asset Usage Detector", - "version": "1.8.1", + "version": "1.8.2", "description": "This editor extension helps you figure out at which places an asset or GameObject is used, i.e. lists the objects that refer to the asset. It is possible to search for references in the Assets folder (Project view) and/or in the scene(s) of your project. You can also search for references while in Play mode!" }