@@ -173,11 +173,6 @@ private static string ExtractGUIDFromString( string str )
173173 private bool searchMonoBehavioursForScript ;
174174 private bool searchTextureReferences ;
175175 private bool searchShaderGraphsForSubGraphs ;
176- private bool searchSerializableVariablesOnly ;
177- private bool prevSearchSerializableVariablesOnly ;
178-
179- private BindingFlags fieldModifiers , propertyModifiers ;
180- private BindingFlags prevFieldModifiers , prevPropertyModifiers ;
181176
182177 // Unity's internal function that returns a SerializedProperty's corresponding FieldInfo
183178 private delegate FieldInfo FieldInfoGetter ( SerializedProperty p , out Type t ) ;
@@ -246,17 +241,6 @@ private void InitializeSearchFunctionsData( Parameters searchParameters )
246241 } ;
247242 }
248243
249- fieldModifiers = searchParameters . fieldModifiers | BindingFlags . Instance | BindingFlags . DeclaredOnly ;
250- propertyModifiers = searchParameters . propertyModifiers | BindingFlags . Instance | BindingFlags . DeclaredOnly ;
251- searchSerializableVariablesOnly = ! searchParameters . searchNonSerializableVariables ;
252-
253- if ( prevFieldModifiers != fieldModifiers || prevPropertyModifiers != propertyModifiers || prevSearchSerializableVariablesOnly != searchSerializableVariablesOnly )
254- typeToVariables . Clear ( ) ;
255-
256- prevFieldModifiers = fieldModifiers ;
257- prevPropertyModifiers = propertyModifiers ;
258- prevSearchSerializableVariablesOnly = searchSerializableVariablesOnly ;
259-
260244 searchPrefabConnections = false ;
261245 searchMonoBehavioursForScript = false ;
262246 searchTextureReferences = false ;
@@ -739,7 +723,7 @@ private ReferenceNode SearchMonoScript( object obj )
739723 VariableGetterHolder [ ] variables = GetFilteredVariablesForType ( scriptType ) ;
740724 for ( int i = 0 ; i < variables . Length ; i ++ )
741725 {
742- if ( variables [ i ] . isSerializable && ! variables [ i ] . IsProperty )
726+ if ( ! variables [ i ] . IsProperty )
743727 {
744728 Object defaultValue = scriptImporter . GetDefaultReference ( variables [ i ] . Name ) ;
745729 if ( objectsToSearchSet . Contains ( defaultValue ) )
@@ -1502,10 +1486,6 @@ private void SearchVariablesWithReflection( ReferenceNode referenceNode )
15021486 VariableGetterHolder [ ] variables = GetFilteredVariablesForType ( referenceNode . nodeObject . GetType ( ) ) ;
15031487 for ( int i = 0 ; i < variables . Length ; i ++ )
15041488 {
1505- // When possible, don't search non-serializable variables
1506- if ( searchSerializableVariablesOnly && ! variables [ i ] . isSerializable )
1507- continue ;
1508-
15091489 try
15101490 {
15111491 object variableValue = variables [ i ] . Get ( referenceNode . nodeObject ) ;
@@ -1603,126 +1583,107 @@ private VariableGetterHolder[] GetFilteredVariablesForType( Type type )
16031583
16041584 validVariables . Clear ( ) ;
16051585
1606- // Filter the fields
1607- if ( fieldModifiers != ( BindingFlags . Instance | BindingFlags . DeclaredOnly ) )
1586+ Type currType = type ;
1587+ while ( currType != typeof ( object ) )
16081588 {
1609- Type currType = type ;
1610- while ( currType != typeof ( object ) )
1589+ foreach ( FieldInfo field in currType . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . DeclaredOnly ) )
16111590 {
1612- FieldInfo [ ] fields = currType . GetFields ( fieldModifiers ) ;
1613- for ( int i = 0 ; i < fields . Length ; i ++ )
1614- {
1615- FieldInfo field = fields [ i ] ;
1616-
1617- // Skip obsolete fields
1618- if ( Attribute . IsDefined ( field , typeof ( ObsoleteAttribute ) ) )
1619- continue ;
1620-
1621- // Skip primitive types
1622- if ( field . FieldType . IsIgnoredUnityType ( ) )
1623- continue ;
1591+ // Skip obsolete fields
1592+ if ( Attribute . IsDefined ( field , typeof ( ObsoleteAttribute ) ) )
1593+ continue ;
16241594
1625- // "ref struct"s can't be accessed via reflection
1626- if ( field . FieldType . IsByRefLike )
1627- continue ;
1595+ // Skip primitive types
1596+ if ( field . FieldType . IsIgnoredUnityType ( ) )
1597+ continue ;
16281598
1629- // Additional filtering for fields:
1630- // 1- Ignore "m_RectTransform", "m_CanvasRenderer" and "m_Canvas" fields of Graphic components
1631- string fieldName = field . Name ;
1632- if ( typeof ( Graphic ) . IsAssignableFrom ( currType ) &&
1633- ( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) )
1634- continue ;
1599+ // "ref struct"s can't be accessed via reflection
1600+ if ( field . FieldType . IsByRefLike )
1601+ continue ;
16351602
1636- VariableGetVal getter = field . CreateGetter ( type ) ;
1637- if ( getter != null )
1638- validVariables . Add ( new VariableGetterHolder ( field , getter , searchSerializableVariablesOnly ? field . IsSerializable ( ) : true ) ) ;
1639- }
1603+ // Additional filtering for fields:
1604+ // 1- Ignore "m_RectTransform", "m_CanvasRenderer" and "m_Canvas" fields of Graphic components
1605+ string fieldName = field . Name ;
1606+ if ( typeof ( Graphic ) . IsAssignableFrom ( currType ) &&
1607+ ( fieldName == "m_RectTransform" || fieldName == "m_CanvasRenderer" || fieldName == "m_Canvas" ) )
1608+ continue ;
16401609
1641- currType = currType . BaseType ;
1610+ VariableGetVal getter = field . CreateGetter ( type ) ;
1611+ if ( getter != null )
1612+ validVariables . Add ( new VariableGetterHolder ( field , getter ) ) ;
16421613 }
1643- }
16441614
1645- if ( propertyModifiers != ( BindingFlags . Instance | BindingFlags . DeclaredOnly ) )
1646- {
1647- Type currType = type ;
1648- while ( currType != typeof ( object ) )
1615+ foreach ( PropertyInfo property in currType . GetProperties ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance | BindingFlags . DeclaredOnly ) )
16491616 {
1650- PropertyInfo [ ] properties = currType . GetProperties ( propertyModifiers ) ;
1651- for ( int i = 0 ; i < properties . Length ; i ++ )
1652- {
1653- PropertyInfo property = properties [ i ] ;
1654-
1655- // Skip obsolete properties
1656- if ( Attribute . IsDefined ( property , typeof ( ObsoleteAttribute ) ) )
1657- continue ;
1617+ // Skip obsolete properties
1618+ if ( Attribute . IsDefined ( property , typeof ( ObsoleteAttribute ) ) )
1619+ continue ;
16581620
1659- // Skip primitive types
1660- if ( property . PropertyType . IsIgnoredUnityType ( ) )
1661- continue ;
1621+ // Skip primitive types
1622+ if ( property . PropertyType . IsIgnoredUnityType ( ) )
1623+ continue ;
16621624
1663- // "ref struct"s can't be accessed via reflection
1664- if ( property . PropertyType . IsByRefLike )
1665- continue ;
1625+ // "ref struct"s can't be accessed via reflection
1626+ if ( property . PropertyType . IsByRefLike )
1627+ continue ;
16661628
1667- // Skip properties without a getter function
1668- MethodInfo propertyGetter = property . GetGetMethod ( true ) ;
1669- if ( propertyGetter == null )
1670- continue ;
1629+ // Skip properties without a getter function
1630+ MethodInfo propertyGetter = property . GetGetMethod ( true ) ;
1631+ if ( propertyGetter == null )
1632+ continue ;
16711633
1672- // Skip indexer properties
1673- if ( property . GetIndexParameters ( ) . Length > 0 )
1674- continue ;
1634+ // Skip indexer properties
1635+ if ( property . GetIndexParameters ( ) . Length > 0 )
1636+ continue ;
16751637
1676- // No need to check properties with 'override' keyword
1677- if ( propertyGetter . GetBaseDefinition ( ) . DeclaringType != propertyGetter . DeclaringType )
1678- continue ;
1638+ // No need to check properties with 'override' keyword
1639+ if ( propertyGetter . GetBaseDefinition ( ) . DeclaringType != propertyGetter . DeclaringType )
1640+ continue ;
16791641
1680- string propertyName = property . Name ;
1642+ string propertyName = property . Name ;
16811643
1682- // Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of components to get more useful results
1683- if ( typeof ( Component ) . IsAssignableFrom ( currType ) && ( propertyName == "gameObject" ||
1684- propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
1685- continue ;
1686- // Ignore "canvasRenderer" and "canvas" properties of Graphic components to get more useful results
1687- else if ( typeof ( Graphic ) . IsAssignableFrom ( currType ) &&
1688- ( propertyName == "canvasRenderer" || propertyName == "canvas" ) )
1689- continue ;
1690- // Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
1691- else if ( typeof ( MeshFilter ) . IsAssignableFrom ( currType ) && propertyName == "mesh" )
1692- continue ;
1693- // Same as above
1694- else if ( ( propertyName == "material" || propertyName == "materials" ) &&
1695- ( typeof ( Renderer ) . IsAssignableFrom ( currType ) || typeof ( Collider ) . IsAssignableFrom ( currType ) ||
1696- typeof ( Collider2D ) . IsAssignableFrom ( currType ) ) )
1697- continue ;
1698- // Ignore certain Material properties that are already searched via SearchMaterial function (also, if a material doesn't have a _Color or _BaseColor
1699- // property and its "color" property is called, it logs an error to the console, so this rule helps avoid that scenario, as well)
1700- else if ( ( propertyName == "color" || propertyName == "mainTexture" ) && typeof ( Material ) . IsAssignableFrom ( currType ) )
1701- continue ;
1702- // Ignore "parameters" property of Animator since it doesn't contain any useful data and logs a warning to the console when Animator is inactive
1703- else if ( typeof ( Animator ) . IsAssignableFrom ( currType ) && propertyName == "parameters" )
1704- continue ;
1705- // Ignore "spriteAnimator" property of TMP_Text component because this property adds a TMP_SpriteAnimator component to the object if it doesn't exist
1706- else if ( propertyName == "spriteAnimator" && currType . Name == "TMP_Text" )
1707- continue ;
1708- // Ignore "meshFilter" property of TextMeshPro and TMP_SubMesh components because this property adds a MeshFilter component to the object if it doesn't exist
1709- else if ( propertyName == "meshFilter" && ( currType . Name == "TextMeshPro" || currType . Name == "TMP_SubMesh" ) )
1710- continue ;
1711- // Ignore "users" property of TerrainData because it returns the Terrains in the scene that use that TerrainData. This causes issues with callStack because TerrainData
1712- // is already in callStack when Terrains are searched via "users" property of it and hence, Terrain->TerrainData references for that TerrainData can't be found in scenes
1713- // (this is how callStack works, it prevents searching an object if it's already in callStack to avoid infinite recursion)
1714- else if ( propertyName == "users" && typeof ( TerrainData ) . IsAssignableFrom ( currType ) )
1715- continue ;
1716- else
1717- {
1718- VariableGetVal getter = property . CreateGetter ( ) ;
1719- if ( getter != null )
1720- validVariables . Add ( new VariableGetterHolder ( property , getter , searchSerializableVariablesOnly ? property . IsSerializable ( ) : true ) ) ;
1721- }
1644+ // Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of components to get more useful results
1645+ if ( typeof ( Component ) . IsAssignableFrom ( currType ) && ( propertyName == "gameObject" ||
1646+ propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
1647+ continue ;
1648+ // Ignore "canvasRenderer" and "canvas" properties of Graphic components to get more useful results
1649+ else if ( typeof ( Graphic ) . IsAssignableFrom ( currType ) &&
1650+ ( propertyName == "canvasRenderer" || propertyName == "canvas" ) )
1651+ continue ;
1652+ // Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
1653+ else if ( typeof ( MeshFilter ) . IsAssignableFrom ( currType ) && propertyName == "mesh" )
1654+ continue ;
1655+ // Same as above
1656+ else if ( ( propertyName == "material" || propertyName == "materials" ) &&
1657+ ( typeof ( Renderer ) . IsAssignableFrom ( currType ) || typeof ( Collider ) . IsAssignableFrom ( currType ) ||
1658+ typeof ( Collider2D ) . IsAssignableFrom ( currType ) ) )
1659+ continue ;
1660+ // Ignore certain Material properties that are already searched via SearchMaterial function (also, if a material doesn't have a _Color or _BaseColor
1661+ // property and its "color" property is called, it logs an error to the console, so this rule helps avoid that scenario, as well)
1662+ else if ( ( propertyName == "color" || propertyName == "mainTexture" ) && typeof ( Material ) . IsAssignableFrom ( currType ) )
1663+ continue ;
1664+ // Ignore "parameters" property of Animator since it doesn't contain any useful data and logs a warning to the console when Animator is inactive
1665+ else if ( typeof ( Animator ) . IsAssignableFrom ( currType ) && propertyName == "parameters" )
1666+ continue ;
1667+ // Ignore "spriteAnimator" property of TMP_Text component because this property adds a TMP_SpriteAnimator component to the object if it doesn't exist
1668+ else if ( propertyName == "spriteAnimator" && currType . Name == "TMP_Text" )
1669+ continue ;
1670+ // Ignore "meshFilter" property of TextMeshPro and TMP_SubMesh components because this property adds a MeshFilter component to the object if it doesn't exist
1671+ else if ( propertyName == "meshFilter" && ( currType . Name == "TextMeshPro" || currType . Name == "TMP_SubMesh" ) )
1672+ continue ;
1673+ // Ignore "users" property of TerrainData because it returns the Terrains in the scene that use that TerrainData. This causes issues with callStack because TerrainData
1674+ // is already in callStack when Terrains are searched via "users" property of it and hence, Terrain->TerrainData references for that TerrainData can't be found in scenes
1675+ // (this is how callStack works, it prevents searching an object if it's already in callStack to avoid infinite recursion)
1676+ else if ( propertyName == "users" && typeof ( TerrainData ) . IsAssignableFrom ( currType ) )
1677+ continue ;
1678+ else
1679+ {
1680+ VariableGetVal getter = property . CreateGetter ( ) ;
1681+ if ( getter != null )
1682+ validVariables . Add ( new VariableGetterHolder ( property , getter ) ) ;
17221683 }
1723-
1724- currType = currType . BaseType ;
17251684 }
1685+
1686+ currType = currType . BaseType ;
17261687 }
17271688
17281689 result = validVariables . ToArray ( ) ;
0 commit comments