Skip to content

Commit 6edf344

Browse files
committed
Fixed prefabs' child objects' references being marked as redundant even though they aren't
1 parent e80ad3b commit 6edf344

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,7 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
13701370
{
13711371
bool iteratingVisible = iteratorVisible.NextVisible( true );
13721372
bool searchPrefabOverridesOnly = ShouldExcludeRedundantPrefabReferences( unityObject );
1373+
GameObject unityObjectPrefabInstanceRoot = searchPrefabOverridesOnly ? PrefabUtility.GetOutermostPrefabInstanceRoot(unityObject) : null;
13731374
bool enterChildren;
13741375
do
13751376
{
@@ -1451,7 +1452,7 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
14511452
// m_RD.texture is a redundant reference that shows up when searching sprites
14521453
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
14531454
{
1454-
if( searchPrefabOverridesOnly && !iterator.prefabOverride )
1455+
if (searchPrefabOverridesOnly && !iterator.prefabOverride && ObjectBelongsToDifferentPrefabInstance(propertyValue, unityObjectPrefabInstanceRoot))
14551456
{
14561457
currentSearchResultGroup.NumberOfRedundantReferences++;
14571458
enterChildren = false;
@@ -1463,6 +1464,22 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
14631464
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
14641465
searchParameters.searchRefactoring( new SerializedPropertyMatch( unityObject, propertyValue, iterator ) );
14651466
}
1467+
1468+
/// Searching for references of a prefab instance's child object in either a scene or prefab mode should show the references coming from
1469+
/// that prefab instance to the child object. That's because even though <see cref="SerializedProperty.prefabOverride"/> returns
1470+
/// false for the variable that points to the child GameObject, that child GameObject is essentially different than its counterpart
1471+
/// in the prefab asset (it's an instance/clone of it after all). So no references will be reported unless we intervene.
1472+
bool ObjectBelongsToDifferentPrefabInstance(Object obj, GameObject prefabInstanceRoot)
1473+
{
1474+
if (obj == null)
1475+
return true;
1476+
1477+
GameObject objPrefabInstanceRoot = PrefabUtility.GetOutermostPrefabInstanceRoot(obj);
1478+
if (objPrefabInstanceRoot == null)
1479+
return true;
1480+
1481+
return objPrefabInstanceRoot != prefabInstanceRoot;
1482+
}
14661483
}
14671484
}
14681485
}

0 commit comments

Comments
 (0)