Skip to content

Commit d9cc52c

Browse files
committed
Fixed some properties not being searched in edge cases that may occur when Addressables or VFX Graph packages are present in the project
1 parent e6641be commit d9cc52c

1 file changed

Lines changed: 69 additions & 67 deletions

File tree

Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs

Lines changed: 69 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,15 +1436,16 @@ private void SearchShaderSourceCodeForCGIncludes( ReferenceNode referenceNode )
14361436
// Search through variables of an object with SerializedObject
14371437
private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, bool forceUseSerializedObject = false )
14381438
{
1439-
if( !isInPlayMode || referenceNode.nodeObject.IsAsset() || forceUseSerializedObject )
1439+
Object unityObject = (Object) referenceNode.nodeObject;
1440+
if( !isInPlayMode || unityObject.IsAsset() || forceUseSerializedObject )
14401441
{
14411442
#if ASSET_USAGE_ADDRESSABLES
14421443
// See: https://github.com/yasirkula/UnityAssetUsageDetector/issues/29
1443-
if( searchParameters.addressablesSupport && ( (Object) referenceNode.nodeObject ).name == "Deprecated EditorExtensionImpl" )
1444+
if( searchParameters.addressablesSupport && unityObject.name == "Deprecated EditorExtensionImpl" )
14441445
return;
14451446
#endif
14461447

1447-
SerializedObject so = new SerializedObject( (Object) referenceNode.nodeObject );
1448+
SerializedObject so = new SerializedObject( unityObject );
14481449
SerializedProperty iterator = so.GetIterator();
14491450
SerializedProperty iteratorVisible = so.GetIterator();
14501451
if( iterator.Next( true ) )
@@ -1456,90 +1457,91 @@ private void SearchVariablesWithSerializedObject( ReferenceNode referenceNode, b
14561457
// Iterate over NextVisible properties AND the properties that have corresponding FieldInfos (internal Unity
14571458
// properties don't have FieldInfos so we are skipping them, which is good because search results found in
14581459
// those properties aren't interesting and mostly confusing)
1459-
bool isVisible = iteratingVisible && SerializedProperty.EqualContents( iterator, iteratorVisible );
1460-
if( isVisible )
1461-
iteratingVisible = iteratorVisible.NextVisible( iteratorVisible.propertyType == SerializedPropertyType.Generic );
1462-
else
1460+
bool shouldMoveVisibleIterator = iteratingVisible && SerializedProperty.EqualContents( iterator, iteratorVisible );
1461+
bool isVisible = shouldMoveVisibleIterator || iterator.type == "Array";
1462+
if( !isVisible )
14631463
{
14641464
Type propFieldType;
1465-
isVisible = iterator.type == "Array" || fieldInfoGetter( iterator, out propFieldType ) != null;
1465+
isVisible = fieldInfoGetter( iterator, out propFieldType ) != null;
14661466
}
14671467

14681468
if( !isVisible )
1469-
{
14701469
enterChildren = false;
1471-
continue;
1472-
}
1473-
1474-
Object propertyValue;
1475-
ReferenceNode searchResult;
1476-
switch( iterator.propertyType )
1470+
else
14771471
{
1478-
case SerializedPropertyType.ObjectReference:
1479-
propertyValue = iterator.objectReferenceValue;
1480-
searchResult = SearchObject( PreferablyGameObject( propertyValue ) );
1481-
enterChildren = false;
1482-
break;
1483-
case SerializedPropertyType.ExposedReference:
1484-
propertyValue = iterator.exposedReferenceValue;
1485-
searchResult = SearchObject( PreferablyGameObject( propertyValue ) );
1486-
enterChildren = false;
1487-
break;
1472+
Object propertyValue;
1473+
ReferenceNode searchResult;
1474+
switch( iterator.propertyType )
1475+
{
1476+
case SerializedPropertyType.ObjectReference:
1477+
propertyValue = iterator.objectReferenceValue;
1478+
searchResult = SearchObject( PreferablyGameObject( propertyValue ) );
1479+
enterChildren = false;
1480+
break;
1481+
case SerializedPropertyType.ExposedReference:
1482+
propertyValue = iterator.exposedReferenceValue;
1483+
searchResult = SearchObject( PreferablyGameObject( propertyValue ) );
1484+
enterChildren = false;
1485+
break;
14881486
#if UNITY_2019_3_OR_NEWER
1489-
case SerializedPropertyType.ManagedReference:
1490-
object managedReferenceValue = GetRawSerializedPropertyValue( iterator );
1491-
propertyValue = managedReferenceValue as Object;
1492-
searchResult = SearchObject( PreferablyGameObject( managedReferenceValue ) );
1493-
enterChildren = false;
1494-
break;
1487+
case SerializedPropertyType.ManagedReference:
1488+
object managedReferenceValue = GetRawSerializedPropertyValue( iterator );
1489+
propertyValue = managedReferenceValue as Object;
1490+
searchResult = SearchObject( PreferablyGameObject( managedReferenceValue ) );
1491+
enterChildren = false;
1492+
break;
14951493
#endif
1496-
case SerializedPropertyType.Generic:
1494+
case SerializedPropertyType.Generic:
14971495
#if ASSET_USAGE_ADDRESSABLES
1498-
if( searchParameters.addressablesSupport && iterator.type.StartsWithFast( "AssetReference" ) && GetRawSerializedPropertyValue( iterator ) is AssetReference assetReference )
1499-
{
1500-
propertyValue = GetAddressablesAssetReferenceValue( assetReference );
1501-
searchResult = SearchObject( PreferablyGameObject( propertyValue ) );
1502-
enterChildren = false;
1503-
}
1504-
else
1496+
if( searchParameters.addressablesSupport && iterator.type.StartsWithFast( "AssetReference" ) && GetRawSerializedPropertyValue( iterator ) is AssetReference assetReference )
1497+
{
1498+
propertyValue = GetAddressablesAssetReferenceValue( assetReference );
1499+
searchResult = SearchObject( PreferablyGameObject( propertyValue ) );
1500+
enterChildren = false;
1501+
}
1502+
else
15051503
#endif
15061504
#if ASSET_USAGE_VFX_GRAPH
1507-
if( vfxSerializableObjectValueGetter != null && iterator.type == "VFXSerializableObject" && GetRawSerializedPropertyValue( iterator ) is object vfxSerializableObject )
1508-
{
1509-
object vfxSerializableObjectValue = vfxSerializableObjectValueGetter.Invoke( vfxSerializableObject, null );
1510-
propertyValue = vfxSerializableObjectValue as Object;
1511-
searchResult = SearchObject( PreferablyGameObject( vfxSerializableObjectValue ) );
1512-
enterChildren = false;
1513-
}
1514-
else
1505+
if( vfxSerializableObjectValueGetter != null && iterator.type == "VFXSerializableObject" && GetRawSerializedPropertyValue( iterator ) is object vfxSerializableObject )
1506+
{
1507+
object vfxSerializableObjectValue = vfxSerializableObjectValueGetter.Invoke( vfxSerializableObject, null );
1508+
propertyValue = vfxSerializableObjectValue as Object;
1509+
searchResult = SearchObject( PreferablyGameObject( vfxSerializableObjectValue ) );
1510+
enterChildren = false;
1511+
}
1512+
else
15151513
#endif
1516-
{
1514+
{
1515+
propertyValue = null;
1516+
searchResult = null;
1517+
enterChildren = true;
1518+
}
1519+
1520+
break;
1521+
default:
15171522
propertyValue = null;
15181523
searchResult = null;
1519-
enterChildren = true;
1520-
}
1521-
1522-
break;
1523-
default:
1524-
propertyValue = null;
1525-
searchResult = null;
1526-
enterChildren = false;
1527-
break;
1528-
}
1529-
1530-
if( searchResult != null && searchResult != referenceNode )
1531-
{
1532-
string propertyPath = iterator.propertyPath;
1524+
enterChildren = false;
1525+
break;
1526+
}
15331527

1534-
// m_RD.texture is a redundant reference that shows up when searching sprites
1535-
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
1528+
if( searchResult != null && searchResult != referenceNode )
15361529
{
1537-
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
1530+
string propertyPath = iterator.propertyPath;
1531+
1532+
// m_RD.texture is a redundant reference that shows up when searching sprites
1533+
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
1534+
{
1535+
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
15381536

1539-
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
1540-
searchParameters.searchRefactoring( new SerializedPropertyMatch( (Object) referenceNode.nodeObject, propertyValue, iterator ) );
1537+
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
1538+
searchParameters.searchRefactoring( new SerializedPropertyMatch( unityObject, propertyValue, iterator ) );
1539+
}
15411540
}
15421541
}
1542+
1543+
if( shouldMoveVisibleIterator )
1544+
iteratingVisible = iteratorVisible.NextVisible( enterChildren );
15431545
} while( iterator.Next( enterChildren ) );
15441546

15451547
return;

0 commit comments

Comments
 (0)