Compare commits

..

4 Commits

Author SHA1 Message Date
Süleyman Yasir KULA 8e7369e44e Some properties in Project Settings like "Quality Settings->SRP Asset" weren't included in search, fixed 2021-03-28 19:57:44 +03:00
Süleyman Yasir KULA 7b60eeacf7 If an exception occurs while searching for references via reflection, the problematic object will be skipped and a warning will be logged to the console instead of interrupting the whole search 2021-03-17 16:13:24 +03:00
Süleyman Yasir KULA b8470536b9 - Following variables of ParticleSystem are now included in search: LightsModule->Light, CollisionModule->Plane, TriggerModule->Collider
- Improved readability of texts in dark Editor skin
- Fixed "Animator is not playing an AnimatorController" warnings
- Fixed "TMP_Sprite Animator" component getting added to TextMesh Pro objects in the scene while searching for a scene object's references at runtime
2021-03-14 17:15:00 +03:00
Süleyman Yasir KULA 1bebc8691d Update package.json
Added documentationUrl, changelogUrl and licensesUrl to package.json
2021-01-19 12:51:21 +03:00
7 changed files with 165 additions and 80 deletions
@@ -547,45 +547,10 @@ namespace AssetUsageDetectorNamespace
}
catch( Exception e )
{
StringBuilder sb = new StringBuilder( objectsToSearchSet.Count * 50 + callStack.Count * 50 + 500 );
StringBuilder sb = new StringBuilder( objectsToSearchSet.Count * 50 + callStack.Count * 50 + 1000 );
sb.AppendLine( "<b>AssetUsageDetector Error:</b>" ).AppendLine().Append( e ).AppendLine();
Object latestUnityObjectInCallStack = null;
if( callStack.Count > 0 )
{
sb.AppendLine( "Stack contents: " );
for( int i = callStack.Count - 1; i >= 0; i-- )
{
latestUnityObjectInCallStack = callStack[i] as Object;
if( latestUnityObjectInCallStack )
{
if( !AssetDatabase.Contains( latestUnityObjectInCallStack ) )
{
string scenePath = AssetDatabase.GetAssetOrScenePath( latestUnityObjectInCallStack );
if( !string.IsNullOrEmpty( scenePath ) && SceneManager.GetSceneByPath( scenePath ).IsValid() )
sb.Append( "Scene: " ).AppendLine( scenePath );
}
break;
}
}
for( int i = callStack.Count - 1; i >= 0; i-- )
{
sb.Append( i ).Append( ": " );
Object unityObject = callStack[i] as Object;
if( unityObject )
sb.Append( unityObject.name ).Append( " (" ).Append( unityObject.GetType() ).AppendLine( ")" );
else if( callStack[i] != null )
sb.Append( callStack[i].GetType() ).AppendLine( " object" );
else
sb.AppendLine( "<<destroyed>>" );
}
sb.AppendLine();
}
Object latestUnityObjectInCallStack = AppendCallStackToStringBuilder( sb );
sb.AppendLine( "Searching references of: " );
foreach( Object obj in objectsToSearchSet )
@@ -635,7 +600,9 @@ namespace AssetUsageDetectorNamespace
if( shouldOpenPrefabStageWithoutContext )
#endif
AssetDatabase.OpenAsset( AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStageAssetPath ) );
{
AssetDatabase.OpenAsset( AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStageAssetPath ) );
}
}
#endif
}
@@ -1106,5 +1073,48 @@ namespace AssetUsageDetectorNamespace
Object.DestroyImmediate( temp );
}
}
// Appends contents of callStack to StringBuilder and returns the most recent Unity object in callStack
private Object AppendCallStackToStringBuilder( StringBuilder sb )
{
Object latestUnityObjectInCallStack = null;
if( callStack.Count > 0 )
{
sb.AppendLine().AppendLine( "Stack contents: " );
for( int i = callStack.Count - 1; i >= 0; i-- )
{
latestUnityObjectInCallStack = callStack[i] as Object;
if( latestUnityObjectInCallStack )
{
if( !AssetDatabase.Contains( latestUnityObjectInCallStack ) )
{
string scenePath = AssetDatabase.GetAssetOrScenePath( latestUnityObjectInCallStack );
if( !string.IsNullOrEmpty( scenePath ) && SceneManager.GetSceneByPath( scenePath ).IsValid() )
sb.Append( "Scene: " ).AppendLine( scenePath );
}
break;
}
}
for( int i = callStack.Count - 1; i >= 0; i-- )
{
sb.Append( i ).Append( ": " );
Object unityObject = callStack[i] as Object;
if( unityObject )
sb.Append( unityObject.name ).Append( " (" ).Append( unityObject.GetType() ).AppendLine( ")" );
else if( callStack[i] != null )
sb.Append( callStack[i].GetType() ).AppendLine( " object" );
else
sb.AppendLine( "<<destroyed>>" );
}
sb.AppendLine();
}
return latestUnityObjectInCallStack;
}
}
}
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Text;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
@@ -388,7 +389,29 @@ namespace AssetUsageDetectorNamespace
Mesh[] meshes = new Mesh[( (ParticleSystemRenderer) component ).meshCount];
int meshCount = ( (ParticleSystemRenderer) component ).GetMeshes( meshes );
for( int i = 0; i < meshCount; i++ )
referenceNode.AddLinkTo( SearchObject( meshes[i] ), "Custom particle mesh" );
referenceNode.AddLinkTo( SearchObject( meshes[i] ), "Renderer Module: Mesh" );
}
else if( component is ParticleSystem )
{
// Some ParticleSystem properties aren't searched by SerializedObject, search them manually instead
ParticleSystem particleSystem = (ParticleSystem) component;
referenceNode.AddLinkTo( SearchObject( particleSystem.lights.light ), "Light Module: Light" );
try
{
ParticleSystem.CollisionModule collisionModule = particleSystem.collision;
for( int i = 0, j = collisionModule.maxPlaneCount; i < j; i++ )
referenceNode.AddLinkTo( SearchObject( collisionModule.GetPlane( i ) ), "Collision Module: Plane" );
}
catch { }
try
{
ParticleSystem.TriggerModule triggerModule = particleSystem.trigger;
for( int i = 0, j = triggerModule.maxColliderCount; i < j; i++ )
referenceNode.AddLinkTo( SearchObject( triggerModule.GetCollider( i ) ), "Trigger Module: Collider" );
}
catch { }
}
else if( searchRenderers && component is Renderer )
{
@@ -998,26 +1021,38 @@ namespace AssetUsageDetectorNamespace
SerializedProperty iteratorVisible = so.GetIterator();
if( iterator.Next( true ) )
{
string assetPath = AssetDatabase.GetAssetPath( (Object) referenceNode.nodeObject );
bool isProjectSettings = !string.IsNullOrEmpty( assetPath ) && assetPath.StartsWithFast( "ProjectSettings/" );
bool iteratingVisible = iteratorVisible.NextVisible( true );
bool enterChildren;
do
{
// Iterate over NextVisible properties AND the properties that have corresponding FieldInfos (internal Unity
// properties don't have FieldInfos so we are skipping them, which is good because search results found in
// those properties aren't interesting and mostly confusing)
bool isVisible = iteratingVisible && SerializedProperty.EqualContents( iterator, iteratorVisible );
if( isVisible )
iteratingVisible = iteratorVisible.NextVisible( true );
if( isProjectSettings )
{
// Search Project Settings in full because for some reason, important settings like SRP asset
// slot in Quality Settings aren't searched when iterating over only visible properties
enterChildren = true;
}
else
{
Type propFieldType;
isVisible = iterator.type == "Array" || fieldInfoGetter( iterator, out propFieldType ) != null;
}
// Iterate over NextVisible properties AND the properties that have corresponding FieldInfos (internal Unity
// properties don't have FieldInfos so we are skipping them, which is good because search results found in
// those properties aren't interesting and mostly confusing)
bool isVisible = iteratingVisible && SerializedProperty.EqualContents( iterator, iteratorVisible );
if( isVisible )
iteratingVisible = iteratorVisible.NextVisible( true );
else
{
Type propFieldType;
isVisible = iterator.type == "Array" || fieldInfoGetter( iterator, out propFieldType ) != null;
}
if( !isVisible )
{
enterChildren = false;
continue;
if( !isVisible )
{
enterChildren = false;
continue;
}
}
ReferenceNode searchResult;
@@ -1079,7 +1114,7 @@ namespace AssetUsageDetectorNamespace
try
{
object variableValue = variables[i].Get( referenceNode.nodeObject );
if( variableValue == null )
if( variableValue == null || variableValue.Equals( null ) )
continue;
// Values stored inside ICollection objects are searched using IEnumerable,
@@ -1110,6 +1145,15 @@ namespace AssetUsageDetectorNamespace
catch( MissingReferenceException ) { }
catch( MissingComponentException ) { }
catch( NotImplementedException ) { }
catch( Exception e )
{
// Unknown exceptions usually occur when variableValue is an IEnumerable and its enumerator throws an unhandled exception in MoveNext or Current
StringBuilder sb = new StringBuilder( callStack.Count * 50 + 1000 );
sb.Append( "Skipped searching " ).Append( referenceNode.nodeObject.GetType().FullName ).Append( "." ).Append( variables[i].name ).AppendLine( " because it threw exception:" ).Append( e ).AppendLine();
Object latestUnityObjectInCallStack = AppendCallStackToStringBuilder( sb );
Debug.LogWarning( sb.ToString(), latestUnityObjectInCallStack );
}
}
}
@@ -1196,22 +1240,20 @@ namespace AssetUsageDetectorNamespace
if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
continue;
// Additional filtering for properties:
// 1- Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of Component's to get more useful results
// 2- Ignore "canvasRenderer" and "canvas" properties of Graphic components
// 3 & 4- Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
string propertyName = property.Name;
// Ignore "gameObject", "transform", "rectTransform" and "attachedRigidbody" properties of components to get more useful results
if( typeof( Component ).IsAssignableFrom( currType ) && ( propertyName == "gameObject" ||
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
continue;
// Ignore "canvasRenderer" and "canvas" properties of Graphic components to get more useful results
else if( typeof( Graphic ).IsAssignableFrom( currType ) &&
( propertyName == "canvasRenderer" || propertyName == "canvas" ) )
continue;
// Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
else if( typeof( MeshFilter ).IsAssignableFrom( currType ) && propertyName == "mesh" )
continue;
else if( typeof( Renderer ).IsAssignableFrom( currType ) &&
( propertyName == "sharedMaterial" || propertyName == "sharedMaterials" ) )
continue;
// Same as above
else if( ( propertyName == "material" || propertyName == "materials" ) &&
( typeof( Renderer ).IsAssignableFrom( currType ) || typeof( Collider ).IsAssignableFrom( currType ) ||
#if !UNITY_2019_3_OR_NEWER
@@ -1221,6 +1263,19 @@ namespace AssetUsageDetectorNamespace
#endif
typeof( Collider2D ).IsAssignableFrom( currType ) ) )
continue;
// "sharedMaterials" are searched via SearchComponent, no need to search it with reflection, as well
else if( typeof( Renderer ).IsAssignableFrom( currType ) &&
( propertyName == "sharedMaterial" || propertyName == "sharedMaterials" ) )
continue;
// Ignore "parameters" property of Animator since it doesn't contain any useful data and logs a warning to the console when Animator is inactive
else if( typeof( Animator ).IsAssignableFrom( currType ) && propertyName == "parameters" )
continue;
// Ignore "spriteAnimator" property of TMP_Text component because this property adds a TMP_SpriteAnimator component to the object if it doesn't exist
else if( propertyName == "spriteAnimator" && currType.Name == "TMP_Text" )
continue;
// Ignore "meshFilter" property of TextMeshPro and TMP_SubMesh components because this property adds a MeshFilter component to the object if it doesn't exist
else if( propertyName == "meshFilter" && ( currType.Name == "TextMeshPro" || currType.Name == "TMP_SubMesh" ) )
continue;
else
{
VariableGetVal getter = property.CreateGetter();
@@ -405,12 +405,10 @@ namespace AssetUsageDetectorNamespace
GUILayout.Space( 10 );
Color c = GUI.color;
GUI.color = Color.cyan;
Color c = GUI.backgroundColor;
GUI.backgroundColor = Color.cyan;
GUILayout.Box( "SEARCH IN", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
GUI.color = c;
GUI.backgroundColor = c;
searchInAssetsFolder = EditorGUILayout.ToggleLeft( "Project window (Assets folder)", searchInAssetsFolder );
@@ -523,11 +521,9 @@ namespace AssetUsageDetectorNamespace
//GUILayout.Space( 10 );
GUI.color = Color.cyan;
GUI.backgroundColor = Color.cyan;
GUILayout.Box( "SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
GUI.color = c;
GUI.backgroundColor = c;
lazySceneSearch = EditorGUILayout.ToggleLeft( "Lazy scene search: scenes are searched in detail only when they are manually refreshed (faster search)", lazySceneSearch );
noAssetDatabaseChanges = EditorGUILayout.ToggleLeft( "I haven't modified any assets/scenes since the last search (faster search)", noAssetDatabaseChanges );
@@ -568,10 +564,10 @@ namespace AssetUsageDetectorNamespace
else if( !searchResult.SearchCompletedSuccessfully )
EditorGUILayout.HelpBox( "ERROR: search was interrupted, check the logs for more info", MessageType.Error );
Color c = GUI.color;
GUI.color = Color.green;
Color c = GUI.backgroundColor;
GUI.backgroundColor = Color.green;
GUILayout.Box( "Don't forget to save scene(s) if you made any changes!", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
GUI.color = c;
GUI.backgroundColor = c;
if( searchResult.NumberOfGroups == 0 )
{
@@ -682,8 +682,8 @@ namespace AssetUsageDetectorNamespace
parameters.shouldRefreshEditorWindow = true; // A repaint is needed to work nicely with GUILayout
}
Color c = GUI.color;
GUI.color = Color.cyan;
Color c = GUI.backgroundColor;
GUI.backgroundColor = Color.cyan;
Rect rect = parameters.guiRect;
float width = rect.width;
@@ -742,7 +742,8 @@ namespace AssetUsageDetectorNamespace
if( IsExpanded )
{
GUI.color = Color.yellow;
// On light skin, yellow background looks better than light grey background
GUI.backgroundColor = EditorGUIUtility.isProSkin ? c : Color.yellow;
if( PendingSearch )
{
@@ -767,7 +768,7 @@ namespace AssetUsageDetectorNamespace
}
parameters.guiRect = rect;
GUI.color = c;
GUI.backgroundColor = c;
}
private void GenerateGUINodes( SearchResultDrawParameters parameters )
+22 -1
View File
@@ -81,6 +81,14 @@ namespace AssetUsageDetectorNamespace
alignment = TextAnchor.MiddleCenter,
font = EditorStyles.label.font
};
Color textColor = GUI.skin.button.normal.textColor;
m_boxGUIStyle.normal.textColor = textColor;
m_boxGUIStyle.hover.textColor = textColor;
m_boxGUIStyle.focused.textColor = textColor;
m_boxGUIStyle.active.textColor = textColor;
m_boxGUIStyle.fontSize = ( m_boxGUIStyle.fontSize + GUI.skin.button.fontSize ) / 2;
}
return m_boxGUIStyle;
@@ -446,7 +454,20 @@ namespace AssetUsageDetectorNamespace
{
// Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs
if( propertyInfo.DeclaringType.IsValueType )
return propertyInfo.CanRead ? ( ( obj ) => propertyInfo.GetValue( obj, null ) ) : (VariableGetVal) null;
{
return !propertyInfo.CanRead ? (VariableGetVal) null : ( obj ) =>
{
try
{
return propertyInfo.GetValue( obj, null );
}
catch
{
// Property getters may return various kinds of exceptions if their backing fields are not initialized (yet)
return null;
}
};
}
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
@@ -69,8 +69,7 @@ namespace AssetUsageDetectorNamespace
}
catch
{
// Property getters may return various kinds of exceptions
// if their backing fields are not initialized (yet)
// Property getters may return various kinds of exceptions if their backing fields are not initialized (yet)
return null;
}
}
+4 -1
View File
@@ -1,6 +1,9 @@
{
"name": "com.yasirkula.assetusagedetector",
"displayName": "Asset Usage Detector",
"version": "1.9.0",
"version": "1.9.3",
"documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector",
"changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases",
"licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",
"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!"
}