Compare commits

..

1 Commits

Author SHA1 Message Date
Süleyman Yasir KULA 6280305a63 - 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
2020-07-16 22:39:41 +03:00
5 changed files with 43 additions and 7 deletions
@@ -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 );
}
@@ -165,6 +165,9 @@ namespace AssetUsageDetectorNamespace
private readonly Dictionary<Type, VariableGetterHolder[]> typeToVariables = new Dictionary<Type, VariableGetterHolder[]>( 4096 );
private readonly List<VariableGetterHolder> validVariables = new List<VariableGetterHolder>( 32 );
// Dictionary to store whether or not instances of a Type can be searched with SearchVariablesWithSerializedObject
private readonly Dictionary<Type, bool> typesSearchabilityWithSerializedObject = new Dictionary<Type, bool>( 4096 );
// Path(s) of .cginc, .cg, .hlsl and .glslinc assets in assetsToSearchSet
private readonly HashSet<string> shaderIncludesToSearchSet = new HashSet<string>();
@@ -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;
}
@@ -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;
}
@@ -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 )
{
+1 -1
View File
@@ -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!"
}