mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-20 23:12:38 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a6b0f5ae92 | |||
| 8e7369e44e | |||
| 7b60eeacf7 | |||
| b8470536b9 | |||
| 1bebc8691d | |||
| 5a81e5011b | |||
| ba7539d983 | |||
| f4129574fb | |||
| 7738da9d19 | |||
| 077505a217 | |||
| 003cedd363 | |||
| 68f1b2809e |
@@ -10,6 +10,10 @@ using System;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
#if UNITY_2018_3_OR_NEWER
|
||||||
|
using PrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStage;
|
||||||
|
using PrefabStageUtility = UnityEditor.Experimental.SceneManagement.PrefabStageUtility;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AssetUsageDetectorNamespace
|
namespace AssetUsageDetectorNamespace
|
||||||
{
|
{
|
||||||
@@ -79,8 +83,11 @@ namespace AssetUsageDetectorNamespace
|
|||||||
private bool isInPlayMode;
|
private bool isInPlayMode;
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
private UnityEditor.Experimental.SceneManagement.PrefabStage openPrefabStage;
|
private PrefabStage openPrefabStage;
|
||||||
private GameObject openPrefabStagePrefabAsset;
|
private GameObject openPrefabStagePrefabAsset;
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
private GameObject openPrefabStageContextObject;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private int searchedObjectsCount; // Number of searched objects
|
private int searchedObjectsCount; // Number of searched objects
|
||||||
@@ -116,7 +123,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
openPrefabStagePrefabAsset = null;
|
openPrefabStagePrefabAsset = null;
|
||||||
string openPrefabStageAssetPath = null;
|
string openPrefabStageAssetPath = null;
|
||||||
openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
openPrefabStage = PrefabStageUtility.GetCurrentPrefabStage();
|
||||||
if( openPrefabStage != null )
|
if( openPrefabStage != null )
|
||||||
{
|
{
|
||||||
if( !openPrefabStage.stageHandle.IsValid() )
|
if( !openPrefabStage.stageHandle.IsValid() )
|
||||||
@@ -130,8 +137,17 @@ namespace AssetUsageDetectorNamespace
|
|||||||
return new SearchResult( false, null, null, this, searchParameters );
|
return new SearchResult( false, null, null, this, searchParameters );
|
||||||
}
|
}
|
||||||
|
|
||||||
openPrefabStagePrefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStage.prefabAssetPath );
|
#if UNITY_2020_1_OR_NEWER
|
||||||
openPrefabStageAssetPath = openPrefabStage.prefabAssetPath;
|
string prefabAssetPath = openPrefabStage.assetPath;
|
||||||
|
#else
|
||||||
|
string prefabAssetPath = openPrefabStage.prefabAssetPath;
|
||||||
|
#endif
|
||||||
|
openPrefabStagePrefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( prefabAssetPath );
|
||||||
|
openPrefabStageAssetPath = prefabAssetPath;
|
||||||
|
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
openPrefabStageContextObject = openPrefabStage.openedFromInstanceRoot;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -531,26 +547,10 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
catch( Exception e )
|
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();
|
sb.AppendLine( "<b>AssetUsageDetector Error:</b>" ).AppendLine().Append( e ).AppendLine();
|
||||||
if( callStack.Count > 0 )
|
|
||||||
{
|
|
||||||
sb.AppendLine( "Stack contents: " );
|
|
||||||
for( int i = callStack.Count - 1; i >= 0; i-- )
|
|
||||||
{
|
|
||||||
sb.Append( i ).Append( ": " );
|
|
||||||
|
|
||||||
Object unityObject = callStack[i] as Object;
|
Object latestUnityObjectInCallStack = AppendCallStackToStringBuilder( sb );
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
sb.AppendLine( "Searching references of: " );
|
sb.AppendLine( "Searching references of: " );
|
||||||
foreach( Object obj in objectsToSearchSet )
|
foreach( Object obj in objectsToSearchSet )
|
||||||
@@ -559,10 +559,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
sb.Append( obj.name ).Append( " (" ).Append( obj.GetType() ).AppendLine( ")" );
|
sb.Append( obj.name ).Append( " (" ).Append( obj.GetType() ).AppendLine( ")" );
|
||||||
}
|
}
|
||||||
|
|
||||||
sb.AppendLine();
|
Debug.LogError( sb.ToString(), latestUnityObjectInCallStack );
|
||||||
sb.Append( e ).AppendLine();
|
|
||||||
|
|
||||||
Debug.LogError( sb.ToString() );
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -583,7 +580,30 @@ namespace AssetUsageDetectorNamespace
|
|||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
// If a prefab stage was open when the search was triggered, try reopening the prefab stage after the search is completed
|
// If a prefab stage was open when the search was triggered, try reopening the prefab stage after the search is completed
|
||||||
if( !string.IsNullOrEmpty( openPrefabStageAssetPath ) )
|
if( !string.IsNullOrEmpty( openPrefabStageAssetPath ) )
|
||||||
AssetDatabase.OpenAsset( AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStageAssetPath ) );
|
{
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
bool shouldOpenPrefabStageWithoutContext = true;
|
||||||
|
if( openPrefabStageContextObject != null && !openPrefabStageContextObject.Equals( null ) )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Try to access this method: https://github.com/Unity-Technologies/UnityCsReference/blob/73925b1711847c067e607ec8371f8e9ffe7ab65d/Editor/Mono/SceneManagement/StageManager/PrefabStage/PrefabStageUtility.cs#L61-L65
|
||||||
|
MethodInfo prefabStageOpenerWithContext = typeof( PrefabStageUtility ).GetMethod( "OpenPrefab", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static, null, new Type[2] { typeof( string ), typeof( GameObject ) }, null );
|
||||||
|
if( prefabStageOpenerWithContext != null )
|
||||||
|
{
|
||||||
|
prefabStageOpenerWithContext.Invoke( null, new object[2] { openPrefabStageAssetPath, openPrefabStageContextObject } );
|
||||||
|
shouldOpenPrefabStageWithoutContext = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
if( shouldOpenPrefabStageWithoutContext )
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
AssetDatabase.OpenAsset( AssetDatabase.LoadAssetAtPath<GameObject>( openPrefabStageAssetPath ) );
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1053,5 +1073,48 @@ namespace AssetUsageDetectorNamespace
|
|||||||
Object.DestroyImmediate( temp );
|
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.Collections;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
using UnityEditor;
|
using UnityEditor;
|
||||||
using UnityEditor.Animations;
|
using UnityEditor.Animations;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@@ -165,9 +166,6 @@ namespace AssetUsageDetectorNamespace
|
|||||||
private readonly Dictionary<Type, VariableGetterHolder[]> typeToVariables = new Dictionary<Type, VariableGetterHolder[]>( 4096 );
|
private readonly Dictionary<Type, VariableGetterHolder[]> typeToVariables = new Dictionary<Type, VariableGetterHolder[]>( 4096 );
|
||||||
private readonly List<VariableGetterHolder> validVariables = new List<VariableGetterHolder>( 32 );
|
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
|
// Path(s) of .cginc, .cg, .hlsl and .glslinc assets in assetsToSearchSet
|
||||||
private readonly HashSet<string> shaderIncludesToSearchSet = new HashSet<string>();
|
private readonly HashSet<string> shaderIncludesToSearchSet = new HashSet<string>();
|
||||||
|
|
||||||
@@ -194,6 +192,10 @@ namespace AssetUsageDetectorNamespace
|
|||||||
private BindingFlags fieldModifiers, propertyModifiers;
|
private BindingFlags fieldModifiers, propertyModifiers;
|
||||||
private BindingFlags prevFieldModifiers, prevPropertyModifiers;
|
private BindingFlags prevFieldModifiers, prevPropertyModifiers;
|
||||||
|
|
||||||
|
// Unity's internal function that returns a SerializedProperty's corresponding FieldInfo
|
||||||
|
private delegate FieldInfo FieldInfoGetter( SerializedProperty p, out Type t );
|
||||||
|
private FieldInfoGetter fieldInfoGetter;
|
||||||
|
|
||||||
private void InitializeSearchFunctionsData( Parameters searchParameters )
|
private void InitializeSearchFunctionsData( Parameters searchParameters )
|
||||||
{
|
{
|
||||||
if( typeToSearchFunction == null )
|
if( typeToSearchFunction == null )
|
||||||
@@ -330,6 +332,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
alwaysSearchedExtensionsSet.Add( "shadersubgraph" );
|
alwaysSearchedExtensionsSet.Add( "shadersubgraph" );
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if UNITY_2019_3_OR_NEWER
|
||||||
|
MethodInfo fieldInfoGetterMethod = typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoAndStaticTypeFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static );
|
||||||
|
#else
|
||||||
|
MethodInfo fieldInfoGetterMethod = typeof( Editor ).Assembly.GetType( "UnityEditor.ScriptAttributeUtility" ).GetMethod( "GetFieldInfoFromProperty", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static );
|
||||||
|
#endif
|
||||||
|
fieldInfoGetter = (FieldInfoGetter) Delegate.CreateDelegate( typeof( FieldInfoGetter ), fieldInfoGetterMethod );
|
||||||
}
|
}
|
||||||
|
|
||||||
private ReferenceNode SearchGameObject( Object unityObject )
|
private ReferenceNode SearchGameObject( Object unityObject )
|
||||||
@@ -374,6 +383,44 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( objectsToSearchSet.Contains( script ) )
|
if( objectsToSearchSet.Contains( script ) )
|
||||||
referenceNode.AddLinkTo( GetReferenceNode( script ) );
|
referenceNode.AddLinkTo( GetReferenceNode( script ) );
|
||||||
}
|
}
|
||||||
|
else if( component is ParticleSystemRenderer )
|
||||||
|
{
|
||||||
|
// Search ParticleSystemRenderer's custom meshes for references (they aren't searched by SerializedObject, unfortunately)
|
||||||
|
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] ), "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;
|
||||||
|
#if UNITY_2020_2_OR_NEWER
|
||||||
|
for( int i = 0, j = collisionModule.planeCount; i < j; i++ )
|
||||||
|
#else
|
||||||
|
for( int i = 0, j = collisionModule.maxPlaneCount; i < j; i++ )
|
||||||
|
#endif
|
||||||
|
referenceNode.AddLinkTo( SearchObject( collisionModule.GetPlane( i ) ), "Collision Module: Plane" );
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ParticleSystem.TriggerModule triggerModule = particleSystem.trigger;
|
||||||
|
#if UNITY_2020_2_OR_NEWER
|
||||||
|
for( int i = 0, j = triggerModule.colliderCount; i < j; i++ )
|
||||||
|
#else
|
||||||
|
for( int i = 0, j = triggerModule.maxColliderCount; i < j; i++ )
|
||||||
|
#endif
|
||||||
|
referenceNode.AddLinkTo( SearchObject( triggerModule.GetCollider( i ) ), "Trigger Module: Collider" );
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
else if( searchRenderers && component is Renderer )
|
else if( searchRenderers && component is Renderer )
|
||||||
{
|
{
|
||||||
// If an asset is a shader, texture or material, and this component is a Renderer,
|
// If an asset is a shader, texture or material, and this component is a Renderer,
|
||||||
@@ -384,7 +431,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
else if( component is Animation )
|
else if( component is Animation )
|
||||||
{
|
{
|
||||||
// If this component is an Animation, search its animation clips for references
|
// Search animation clips for references
|
||||||
foreach( AnimationState anim in (Animation) component )
|
foreach( AnimationState anim in (Animation) component )
|
||||||
referenceNode.AddLinkTo( SearchObject( anim.clip ) );
|
referenceNode.AddLinkTo( SearchObject( anim.clip ) );
|
||||||
|
|
||||||
@@ -393,7 +440,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
else if( component is Animator )
|
else if( component is Animator )
|
||||||
{
|
{
|
||||||
// If this component is an Animator, search its animation clips for references (via AnimatorController)
|
// Search animation clips for references (via AnimatorController)
|
||||||
referenceNode.AddLinkTo( SearchObject( ( (Animator) component ).runtimeAnimatorController ) );
|
referenceNode.AddLinkTo( SearchObject( ( (Animator) component ).runtimeAnimatorController ) );
|
||||||
|
|
||||||
// Search the objects that are animated by this Animator component for references
|
// Search the objects that are animated by this Animator component for references
|
||||||
@@ -402,7 +449,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
#if UNITY_2017_2_OR_NEWER
|
#if UNITY_2017_2_OR_NEWER
|
||||||
else if( component is Tilemap )
|
else if( component is Tilemap )
|
||||||
{
|
{
|
||||||
// If this component is a Tilemap, search its tiles for references
|
// Search the tiles for references
|
||||||
TileBase[] tiles = new TileBase[( (Tilemap) component ).GetUsedTilesCount()];
|
TileBase[] tiles = new TileBase[( (Tilemap) component ).GetUsedTilesCount()];
|
||||||
( (Tilemap) component ).GetUsedTilesNonAlloc( tiles );
|
( (Tilemap) component ).GetUsedTilesNonAlloc( tiles );
|
||||||
|
|
||||||
@@ -416,7 +463,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
#if UNITY_2017_1_OR_NEWER
|
#if UNITY_2017_1_OR_NEWER
|
||||||
else if( component is PlayableDirector )
|
else if( component is PlayableDirector )
|
||||||
{
|
{
|
||||||
// If this component is a PlayableDirectory, search its PlayableAsset's scene bindings for references
|
// Search the PlayableAsset's scene bindings for references
|
||||||
PlayableAsset playableAsset = ( (PlayableDirector) component ).playableAsset;
|
PlayableAsset playableAsset = ( (PlayableDirector) component ).playableAsset;
|
||||||
if( playableAsset != null && !playableAsset.Equals( null ) )
|
if( playableAsset != null && !playableAsset.Equals( null ) )
|
||||||
{
|
{
|
||||||
@@ -528,7 +575,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
{
|
{
|
||||||
Object defaultValue = scriptImporter.GetDefaultReference( variables[i].name );
|
Object defaultValue = scriptImporter.GetDefaultReference( variables[i].name );
|
||||||
if( objectsToSearchSet.Contains( defaultValue ) )
|
if( objectsToSearchSet.Contains( defaultValue ) )
|
||||||
referenceNode.AddLinkTo( GetReferenceNode( defaultValue ), "Default Variable Value: " + variables[i].name );
|
referenceNode.AddLinkTo( GetReferenceNode( defaultValue ), "Default variable value: " + variables[i].name );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -553,6 +600,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if( controller is AnimatorOverrideController )
|
||||||
|
{
|
||||||
|
RuntimeAnimatorController parentController = ( (AnimatorOverrideController) controller ).runtimeAnimatorController;
|
||||||
|
if( objectsToSearchSet.Contains( parentController ) )
|
||||||
|
referenceNode.AddLinkTo( GetReferenceNode( parentController ) );
|
||||||
|
}
|
||||||
|
|
||||||
AnimationClip[] animClips = controller.animationClips;
|
AnimationClip[] animClips = controller.animationClips;
|
||||||
for( int i = 0; i < animClips.Length; i++ )
|
for( int i = 0; i < animClips.Length; i++ )
|
||||||
referenceNode.AddLinkTo( SearchObject( animClips[i] ) );
|
referenceNode.AddLinkTo( SearchObject( animClips[i] ) );
|
||||||
@@ -711,9 +765,12 @@ namespace AssetUsageDetectorNamespace
|
|||||||
#else
|
#else
|
||||||
string assemblyPath = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName( assemblyDefinitionFile.references[i] );
|
string assemblyPath = CompilationPipeline.GetAssemblyDefinitionFilePathFromAssemblyName( assemblyDefinitionFile.references[i] );
|
||||||
#endif
|
#endif
|
||||||
Object searchedAssemblyDefinitionFile;
|
if( !string.IsNullOrEmpty( assemblyPath ) )
|
||||||
if( assemblyDefinitionFilesToSearch.TryGetValue( assemblyPath, out searchedAssemblyDefinitionFile ) )
|
{
|
||||||
referenceNode.AddLinkTo( GetReferenceNode( searchedAssemblyDefinitionFile ), "Referenced Assembly" );
|
Object searchedAssemblyDefinitionFile;
|
||||||
|
if( assemblyDefinitionFilesToSearch.TryGetValue( assemblyPath, out searchedAssemblyDefinitionFile ) )
|
||||||
|
referenceNode.AddLinkTo( GetReferenceNode( searchedAssemblyDefinitionFile ), "Referenced Assembly" );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -967,29 +1024,45 @@ namespace AssetUsageDetectorNamespace
|
|||||||
{
|
{
|
||||||
if( !isInPlayMode || referenceNode.nodeObject.IsAsset() )
|
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 );
|
SerializedObject so = new SerializedObject( (Object) referenceNode.nodeObject );
|
||||||
SerializedProperty iterator = so.GetIterator();
|
SerializedProperty iterator = so.GetIterator();
|
||||||
if( iterator.NextVisible( true ) )
|
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;
|
bool enterChildren;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
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
|
||||||
|
{
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ReferenceNode searchResult;
|
ReferenceNode searchResult;
|
||||||
switch( iterator.propertyType )
|
switch( iterator.propertyType )
|
||||||
{
|
{
|
||||||
@@ -1001,6 +1074,12 @@ namespace AssetUsageDetectorNamespace
|
|||||||
searchResult = SearchObject( iterator.exposedReferenceValue );
|
searchResult = SearchObject( iterator.exposedReferenceValue );
|
||||||
enterChildren = false;
|
enterChildren = false;
|
||||||
break;
|
break;
|
||||||
|
#if UNITY_2019_3_OR_NEWER
|
||||||
|
case SerializedPropertyType.ManagedReference:
|
||||||
|
searchResult = SearchObject( GetRawSerializedPropertyValue( iterator ) );
|
||||||
|
enterChildren = false;
|
||||||
|
break;
|
||||||
|
#endif
|
||||||
case SerializedPropertyType.Generic:
|
case SerializedPropertyType.Generic:
|
||||||
searchResult = null;
|
searchResult = null;
|
||||||
enterChildren = true;
|
enterChildren = true;
|
||||||
@@ -1017,9 +1096,9 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
// m_RD.texture is a redundant reference that shows up when searching sprites
|
// m_RD.texture is a redundant reference that shows up when searching sprites
|
||||||
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
|
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
|
||||||
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath );
|
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
|
||||||
}
|
}
|
||||||
} while( iterator.NextVisible( enterChildren ) );
|
} while( iterator.Next( enterChildren ) );
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -1043,7 +1122,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
object variableValue = variables[i].Get( referenceNode.nodeObject );
|
object variableValue = variables[i].Get( referenceNode.nodeObject );
|
||||||
if( variableValue == null )
|
if( variableValue == null || variableValue.Equals( null ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Values stored inside ICollection objects are searched using IEnumerable,
|
// Values stored inside ICollection objects are searched using IEnumerable,
|
||||||
@@ -1074,6 +1153,15 @@ namespace AssetUsageDetectorNamespace
|
|||||||
catch( MissingReferenceException ) { }
|
catch( MissingReferenceException ) { }
|
||||||
catch( MissingComponentException ) { }
|
catch( MissingComponentException ) { }
|
||||||
catch( NotImplementedException ) { }
|
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 );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1084,10 +1172,6 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( typeToVariables.TryGetValue( type, out result ) )
|
if( typeToVariables.TryGetValue( type, out result ) )
|
||||||
return 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
|
// This is the first time this type of object is seen, filter and cache its variables
|
||||||
// Variable filtering process:
|
// Variable filtering process:
|
||||||
// 1- skip Obsolete variables
|
// 1- skip Obsolete variables
|
||||||
@@ -1126,17 +1210,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
VariableGetVal getter = field.CreateGetter( type );
|
VariableGetVal getter = field.CreateGetter( type );
|
||||||
if( getter != null )
|
if( getter != null )
|
||||||
{
|
|
||||||
validVariables.Add( new VariableGetterHolder( field, getter, searchSerializableVariablesOnly ? field.IsSerializable() : true ) );
|
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;
|
currType = currType.BaseType;
|
||||||
@@ -1174,22 +1248,20 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
|
if( propertyGetter.GetBaseDefinition().DeclaringType != propertyGetter.DeclaringType )
|
||||||
continue;
|
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;
|
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" ||
|
if( typeof( Component ).IsAssignableFrom( currType ) && ( propertyName == "gameObject" ||
|
||||||
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
|
propertyName == "transform" || propertyName == "attachedRigidbody" || propertyName == "rectTransform" ) )
|
||||||
continue;
|
continue;
|
||||||
|
// Ignore "canvasRenderer" and "canvas" properties of Graphic components to get more useful results
|
||||||
else if( typeof( Graphic ).IsAssignableFrom( currType ) &&
|
else if( typeof( Graphic ).IsAssignableFrom( currType ) &&
|
||||||
( propertyName == "canvasRenderer" || propertyName == "canvas" ) )
|
( propertyName == "canvasRenderer" || propertyName == "canvas" ) )
|
||||||
continue;
|
continue;
|
||||||
|
// Prevent accessing properties of Unity that instantiate an existing resource (causing memory leak)
|
||||||
else if( typeof( MeshFilter ).IsAssignableFrom( currType ) && propertyName == "mesh" )
|
else if( typeof( MeshFilter ).IsAssignableFrom( currType ) && propertyName == "mesh" )
|
||||||
continue;
|
continue;
|
||||||
else if( typeof( Renderer ).IsAssignableFrom( currType ) &&
|
// Same as above
|
||||||
( propertyName == "sharedMaterial" || propertyName == "sharedMaterials" ) )
|
|
||||||
continue;
|
|
||||||
else if( ( propertyName == "material" || propertyName == "materials" ) &&
|
else if( ( propertyName == "material" || propertyName == "materials" ) &&
|
||||||
( typeof( Renderer ).IsAssignableFrom( currType ) || typeof( Collider ).IsAssignableFrom( currType ) ||
|
( typeof( Renderer ).IsAssignableFrom( currType ) || typeof( Collider ).IsAssignableFrom( currType ) ||
|
||||||
#if !UNITY_2019_3_OR_NEWER
|
#if !UNITY_2019_3_OR_NEWER
|
||||||
@@ -1199,6 +1271,19 @@ namespace AssetUsageDetectorNamespace
|
|||||||
#endif
|
#endif
|
||||||
typeof( Collider2D ).IsAssignableFrom( currType ) ) )
|
typeof( Collider2D ).IsAssignableFrom( currType ) ) )
|
||||||
continue;
|
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
|
else
|
||||||
{
|
{
|
||||||
VariableGetVal getter = property.CreateGetter();
|
VariableGetVal getter = property.CreateGetter();
|
||||||
@@ -1215,11 +1300,87 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
// Cache the filtered fields
|
// Cache the filtered fields
|
||||||
typeToVariables.Add( type, result );
|
typeToVariables.Add( type, result );
|
||||||
typesSearchabilityWithSerializedObject.Add( type, searchabilityWithSerializedObject );
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Credit: http://answers.unity.com/answers/425602/view.html
|
||||||
|
// Returns the raw System.Object value of a SerializedProperty
|
||||||
|
public object GetRawSerializedPropertyValue( SerializedProperty property )
|
||||||
|
{
|
||||||
|
object result = property.serializedObject.targetObject;
|
||||||
|
string[] path = property.propertyPath.Replace( ".Array.data[", "[" ).Split( '.' );
|
||||||
|
for( int i = 0; i < path.Length; i++ )
|
||||||
|
{
|
||||||
|
string pathElement = path[i];
|
||||||
|
|
||||||
|
int arrayStartIndex = pathElement.IndexOf( '[' );
|
||||||
|
if( arrayStartIndex < 0 )
|
||||||
|
result = GetFieldValue( result, pathElement );
|
||||||
|
else
|
||||||
|
{
|
||||||
|
string variableName = pathElement.Substring( 0, arrayStartIndex );
|
||||||
|
|
||||||
|
int arrayEndIndex = pathElement.IndexOf( ']', arrayStartIndex + 1 );
|
||||||
|
int arrayElementIndex = int.Parse( pathElement.Substring( arrayStartIndex + 1, arrayEndIndex - arrayStartIndex - 1 ) );
|
||||||
|
result = GetFieldValue( result, variableName, arrayElementIndex );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Credit: http://answers.unity.com/answers/425602/view.html
|
||||||
|
private object GetFieldValue( object source, string fieldName )
|
||||||
|
{
|
||||||
|
if( source == null )
|
||||||
|
return null;
|
||||||
|
|
||||||
|
FieldInfo fieldInfo = null;
|
||||||
|
Type type = source.GetType();
|
||||||
|
while( fieldInfo == null && type != typeof( object ) )
|
||||||
|
{
|
||||||
|
fieldInfo = type.GetField( fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly );
|
||||||
|
type = type.BaseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( fieldInfo != null )
|
||||||
|
return fieldInfo.GetValue( source );
|
||||||
|
|
||||||
|
PropertyInfo propertyInfo = null;
|
||||||
|
type = source.GetType();
|
||||||
|
while( propertyInfo == null && type != typeof( object ) )
|
||||||
|
{
|
||||||
|
propertyInfo = type.GetProperty( fieldName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.IgnoreCase );
|
||||||
|
type = type.BaseType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( propertyInfo != null )
|
||||||
|
return propertyInfo.GetValue( source, null );
|
||||||
|
|
||||||
|
if( fieldName.Length > 2 && fieldName.StartsWith( "m_", StringComparison.OrdinalIgnoreCase ) )
|
||||||
|
return GetFieldValue( source, fieldName.Substring( 2 ) );
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Credit: http://answers.unity.com/answers/425602/view.html
|
||||||
|
private object GetFieldValue( object source, string fieldName, int arrayIndex )
|
||||||
|
{
|
||||||
|
IEnumerable enumerable = GetFieldValue( source, fieldName ) as IEnumerable;
|
||||||
|
if( enumerable == null )
|
||||||
|
return null;
|
||||||
|
|
||||||
|
if( enumerable is IList )
|
||||||
|
return ( (IList) enumerable )[arrayIndex];
|
||||||
|
|
||||||
|
IEnumerator enumerator = enumerable.GetEnumerator();
|
||||||
|
for( int i = 0; i <= arrayIndex; i++ )
|
||||||
|
enumerator.MoveNext();
|
||||||
|
|
||||||
|
return enumerator.Current;
|
||||||
|
}
|
||||||
|
|
||||||
// Iterates over all occurrences of specific key-value pairs in string
|
// Iterates over all occurrences of specific key-value pairs in string
|
||||||
// Example1: #include "VALUE" valuePrefix=#include, valueWrapperChar="
|
// Example1: #include "VALUE" valuePrefix=#include, valueWrapperChar="
|
||||||
// Example2: "guid": "VALUE" valuePrefix="guid", valueWrapperChar="
|
// Example2: "guid": "VALUE" valuePrefix="guid", valueWrapperChar="
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ using UnityEditor;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
#if UNITY_2018_3_OR_NEWER
|
||||||
|
using PrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStage;
|
||||||
|
using PrefabStageUtility = UnityEditor.Experimental.SceneManagement.PrefabStageUtility;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AssetUsageDetectorNamespace
|
namespace AssetUsageDetectorNamespace
|
||||||
{
|
{
|
||||||
@@ -120,14 +124,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Quickly initiate search for the selected assets
|
// Quickly initiate search for the selected assets
|
||||||
[MenuItem( "GameObject/Search for References", priority = 49 )]
|
[MenuItem( "GameObject/Search for References/This Object Only", priority = 49 )]
|
||||||
[MenuItem( "Assets/Search for References", priority = 1000 )]
|
[MenuItem( "Assets/Search for References", priority = 1000 )]
|
||||||
private static void SearchSelectedAssetReferences( MenuCommand command )
|
private static void SearchSelectedAssetReferences( MenuCommand command )
|
||||||
{
|
{
|
||||||
// This happens when this button is clicked via hierarchy's right click context menu
|
// This happens when this button is clicked via hierarchy's right click context menu
|
||||||
// and is called once for each object in the selection. We don't want that, we want
|
// and is called once for each object in the selection. We don't want that, we want
|
||||||
// the function to be called only once so that there aren't multiple empty parents
|
// the function to be called only once
|
||||||
// generated in one call
|
|
||||||
if( command.context )
|
if( command.context )
|
||||||
{
|
{
|
||||||
EditorApplication.update -= CallSearchSelectedAssetReferencesOnce;
|
EditorApplication.update -= CallSearchSelectedAssetReferencesOnce;
|
||||||
@@ -137,8 +140,21 @@ namespace AssetUsageDetectorNamespace
|
|||||||
ShowAndSearch( Selection.objects );
|
ShowAndSearch( Selection.objects );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[MenuItem( "GameObject/Search for References/Include Children", priority = 49 )]
|
||||||
|
private static void SearchSelectedAssetReferencesWithChildren( MenuCommand command )
|
||||||
|
{
|
||||||
|
if( command.context )
|
||||||
|
{
|
||||||
|
EditorApplication.update -= CallSearchSelectedAssetReferencesWithChildrenOnce;
|
||||||
|
EditorApplication.update += CallSearchSelectedAssetReferencesWithChildrenOnce;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ShowAndSearch( Selection.objects, true );
|
||||||
|
}
|
||||||
|
|
||||||
// Show the menu item only if there is a selection in the Editor
|
// Show the menu item only if there is a selection in the Editor
|
||||||
[MenuItem( "GameObject/Search for References", validate = true )]
|
[MenuItem( "GameObject/Search for References/This Object Only", validate = true )]
|
||||||
|
[MenuItem( "GameObject/Search for References/Include Children", validate = true )]
|
||||||
[MenuItem( "Assets/Search for References", validate = true )]
|
[MenuItem( "Assets/Search for References", validate = true )]
|
||||||
private static bool SearchSelectedAssetReferencesValidate( MenuCommand command )
|
private static bool SearchSelectedAssetReferencesValidate( MenuCommand command )
|
||||||
{
|
{
|
||||||
@@ -146,13 +162,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Quickly show the AssetUsageDetector window and initiate a search
|
// Quickly show the AssetUsageDetector window and initiate a search
|
||||||
public static void ShowAndSearch( IEnumerable<Object> searchObjects )
|
public static void ShowAndSearch( IEnumerable<Object> searchObjects, bool? shouldSearchChildren = null )
|
||||||
{
|
{
|
||||||
ShowAndSearchInternal( searchObjects, null );
|
ShowAndSearchInternal( searchObjects, null, shouldSearchChildren );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Quickly show the AssetUsageDetector window and initiate a search
|
// Quickly show the AssetUsageDetector window and initiate a search
|
||||||
public static void ShowAndSearch( AssetUsageDetector.Parameters searchParameters )
|
public static void ShowAndSearch( AssetUsageDetector.Parameters searchParameters, bool? shouldSearchChildren = null )
|
||||||
{
|
{
|
||||||
if( searchParameters == null )
|
if( searchParameters == null )
|
||||||
{
|
{
|
||||||
@@ -160,7 +176,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ShowAndSearchInternal( searchParameters.objectsToSearch, searchParameters );
|
ShowAndSearchInternal( searchParameters.objectsToSearch, searchParameters, shouldSearchChildren );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void CallSearchSelectedAssetReferencesOnce()
|
private static void CallSearchSelectedAssetReferencesOnce()
|
||||||
@@ -169,9 +185,15 @@ namespace AssetUsageDetectorNamespace
|
|||||||
SearchSelectedAssetReferences( new MenuCommand( null ) );
|
SearchSelectedAssetReferences( new MenuCommand( null ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsageDetector.Parameters searchParameters )
|
private static void CallSearchSelectedAssetReferencesWithChildrenOnce()
|
||||||
{
|
{
|
||||||
if( mainWindow != null && !mainWindow.ReturnToSetupPhase( true ) )
|
EditorApplication.update -= CallSearchSelectedAssetReferencesWithChildrenOnce;
|
||||||
|
SearchSelectedAssetReferencesWithChildren( new MenuCommand( null ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void ShowAndSearchInternal( IEnumerable<Object> searchObjects, AssetUsageDetector.Parameters searchParameters, bool? shouldSearchChildren )
|
||||||
|
{
|
||||||
|
if( mainWindow != null && !mainWindow.ReturnToSetupPhase( true, true ) )
|
||||||
{
|
{
|
||||||
Debug.LogError( "Need to reset the previous search first!" );
|
Debug.LogError( "Need to reset the previous search first!" );
|
||||||
return;
|
return;
|
||||||
@@ -183,7 +205,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( searchObjects != null )
|
if( searchObjects != null )
|
||||||
{
|
{
|
||||||
foreach( Object obj in searchObjects )
|
foreach( Object obj in searchObjects )
|
||||||
mainWindow.objectsToSearch.Add( new ObjectToSearch( obj ) );
|
mainWindow.objectsToSearch.Add( new ObjectToSearch( obj, shouldSearchChildren ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( searchParameters != null )
|
if( searchParameters != null )
|
||||||
@@ -236,15 +258,15 @@ namespace AssetUsageDetectorNamespace
|
|||||||
mainWindow = this;
|
mainWindow = this;
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
UnityEditor.Experimental.SceneManagement.PrefabStage.prefabStageClosing -= ReplacePrefabStageObjectsWithAssets;
|
PrefabStage.prefabStageClosing -= ReplacePrefabStageObjectsWithAssets;
|
||||||
UnityEditor.Experimental.SceneManagement.PrefabStage.prefabStageClosing += ReplacePrefabStageObjectsWithAssets;
|
PrefabStage.prefabStageClosing += ReplacePrefabStageObjectsWithAssets;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDisable()
|
private void OnDisable()
|
||||||
{
|
{
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
UnityEditor.Experimental.SceneManagement.PrefabStage.prefabStageClosing -= ReplacePrefabStageObjectsWithAssets;
|
PrefabStage.prefabStageClosing -= ReplacePrefabStageObjectsWithAssets;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if( mainWindow == this )
|
if( mainWindow == this )
|
||||||
@@ -258,11 +280,8 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
SavePrefs();
|
SavePrefs();
|
||||||
|
|
||||||
if( searchResult != null && currentPhase == Phase.Complete && !EditorApplication.isPlaying && searchResult.IsSceneSetupDifferentThanCurrentSetup() )
|
if( searchResult != null && currentPhase == Phase.Complete )
|
||||||
{
|
searchResult.RestoreInitialSceneSetup( true );
|
||||||
if( EditorUtility.DisplayDialog( "Scenes", "Restore initial scene setup?", "Yes", "Leave it as is" ) )
|
|
||||||
searchResult.RestoreInitialSceneSetup();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnFocus()
|
private void OnFocus()
|
||||||
@@ -377,7 +396,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
restoreInitialSceneSetup = EditorGUILayout.ToggleLeft( restoreInitialSceneSetupLabel, restoreInitialSceneSetup );
|
restoreInitialSceneSetup = EditorGUILayout.ToggleLeft( restoreInitialSceneSetupLabel, restoreInitialSceneSetup );
|
||||||
|
|
||||||
if( GUILayout.Button( "RETURN", Utilities.GL_HEIGHT_30 ) )
|
if( GUILayout.Button( "RETURN", Utilities.GL_HEIGHT_30 ) )
|
||||||
ReturnToSetupPhase( restoreInitialSceneSetup );
|
ReturnToSetupPhase( restoreInitialSceneSetup, false );
|
||||||
}
|
}
|
||||||
else if( currentPhase == Phase.Setup )
|
else if( currentPhase == Phase.Setup )
|
||||||
{
|
{
|
||||||
@@ -386,12 +405,10 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
GUILayout.Space( 10 );
|
GUILayout.Space( 10 );
|
||||||
|
|
||||||
Color c = GUI.color;
|
Color c = GUI.backgroundColor;
|
||||||
GUI.color = Color.cyan;
|
GUI.backgroundColor = Color.cyan;
|
||||||
|
|
||||||
GUILayout.Box( "SEARCH IN", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
GUILayout.Box( "SEARCH IN", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||||
|
GUI.backgroundColor = c;
|
||||||
GUI.color = c;
|
|
||||||
|
|
||||||
searchInAssetsFolder = EditorGUILayout.ToggleLeft( "Project window (Assets folder)", searchInAssetsFolder );
|
searchInAssetsFolder = EditorGUILayout.ToggleLeft( "Project window (Assets folder)", searchInAssetsFolder );
|
||||||
|
|
||||||
@@ -504,11 +521,9 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
//GUILayout.Space( 10 );
|
//GUILayout.Space( 10 );
|
||||||
|
|
||||||
GUI.color = Color.cyan;
|
GUI.backgroundColor = Color.cyan;
|
||||||
|
|
||||||
GUILayout.Box( "SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
GUILayout.Box( "SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||||
|
GUI.backgroundColor = c;
|
||||||
GUI.color = c;
|
|
||||||
|
|
||||||
lazySceneSearch = EditorGUILayout.ToggleLeft( "Lazy scene search: scenes are searched in detail only when they are manually refreshed (faster search)", lazySceneSearch );
|
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 );
|
noAssetDatabaseChanges = EditorGUILayout.ToggleLeft( "I haven't modified any assets/scenes since the last search (faster search)", noAssetDatabaseChanges );
|
||||||
@@ -539,7 +554,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
restoreInitialSceneSetup = EditorGUILayout.ToggleLeft( restoreInitialSceneSetupLabel, restoreInitialSceneSetup );
|
restoreInitialSceneSetup = EditorGUILayout.ToggleLeft( restoreInitialSceneSetupLabel, restoreInitialSceneSetup );
|
||||||
|
|
||||||
if( GUILayout.Button( "Reset Search", Utilities.GL_HEIGHT_30 ) )
|
if( GUILayout.Button( "Reset Search", Utilities.GL_HEIGHT_30 ) )
|
||||||
ReturnToSetupPhase( restoreInitialSceneSetup );
|
ReturnToSetupPhase( restoreInitialSceneSetup, false );
|
||||||
|
|
||||||
if( searchResult == null )
|
if( searchResult == null )
|
||||||
{
|
{
|
||||||
@@ -549,10 +564,10 @@ namespace AssetUsageDetectorNamespace
|
|||||||
else if( !searchResult.SearchCompletedSuccessfully )
|
else if( !searchResult.SearchCompletedSuccessfully )
|
||||||
EditorGUILayout.HelpBox( "ERROR: search was interrupted, check the logs for more info", MessageType.Error );
|
EditorGUILayout.HelpBox( "ERROR: search was interrupted, check the logs for more info", MessageType.Error );
|
||||||
|
|
||||||
Color c = GUI.color;
|
Color c = GUI.backgroundColor;
|
||||||
GUI.color = Color.green;
|
GUI.backgroundColor = Color.green;
|
||||||
GUILayout.Box( "Don't forget to save scene(s) if you made any changes!", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
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 )
|
if( searchResult.NumberOfGroups == 0 )
|
||||||
{
|
{
|
||||||
@@ -647,7 +662,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
SavePrefs();
|
SavePrefs();
|
||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
ReplacePrefabStageObjectsWithAssets( UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage() );
|
ReplacePrefabStageObjectsWithAssets( PrefabStageUtility.GetCurrentPrefabStage() );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Start searching
|
// Start searching
|
||||||
@@ -676,13 +691,17 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
#if UNITY_2018_3_OR_NEWER
|
#if UNITY_2018_3_OR_NEWER
|
||||||
// Try replacing searched objects who are part of currently open prefab stage with their corresponding prefab assets
|
// Try replacing searched objects who are part of currently open prefab stage with their corresponding prefab assets
|
||||||
public void ReplacePrefabStageObjectsWithAssets( UnityEditor.Experimental.SceneManagement.PrefabStage prefabStage )
|
public void ReplacePrefabStageObjectsWithAssets( PrefabStage prefabStage )
|
||||||
{
|
{
|
||||||
if( prefabStage == null || !prefabStage.stageHandle.IsValid() )
|
if( prefabStage == null || !prefabStage.stageHandle.IsValid() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
GameObject prefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( prefabStage.assetPath );
|
||||||
|
#else
|
||||||
GameObject prefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( prefabStage.prefabAssetPath );
|
GameObject prefabAsset = AssetDatabase.LoadAssetAtPath<GameObject>( prefabStage.prefabAssetPath );
|
||||||
if( prefabAsset == null )
|
#endif
|
||||||
|
if( prefabAsset == null || prefabAsset.Equals( null ) )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for( int i = 0; i < objectsToSearch.Count; i++ )
|
for( int i = 0; i < objectsToSearch.Count; i++ )
|
||||||
@@ -710,9 +729,9 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
private bool ReturnToSetupPhase( bool restoreInitialSceneSetup )
|
private bool ReturnToSetupPhase( bool restoreInitialSceneSetup, bool sceneSetupRestorationIsOptional )
|
||||||
{
|
{
|
||||||
if( searchResult != null && restoreInitialSceneSetup && !EditorApplication.isPlaying && !searchResult.RestoreInitialSceneSetup() )
|
if( searchResult != null && restoreInitialSceneSetup && !EditorApplication.isPlaying && !searchResult.RestoreInitialSceneSetup( sceneSetupRestorationIsOptional ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
searchResult = null;
|
searchResult = null;
|
||||||
|
|||||||
@@ -29,13 +29,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
private static MonoScript[] monoScriptsInProject;
|
private static MonoScript[] monoScriptsInProject;
|
||||||
private static HashSet<Object> currentSubAssets;
|
private static HashSet<Object> currentSubAssets;
|
||||||
|
|
||||||
public ObjectToSearch( Object obj )
|
public ObjectToSearch( Object obj, bool? shouldSearchChildren = null )
|
||||||
{
|
{
|
||||||
this.obj = obj;
|
this.obj = obj;
|
||||||
RefreshSubAssets();
|
RefreshSubAssets( shouldSearchChildren );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RefreshSubAssets()
|
public void RefreshSubAssets( bool? shouldSearchChildren = null )
|
||||||
{
|
{
|
||||||
if( subAssets == null )
|
if( subAssets == null )
|
||||||
subAssets = new List<SubAsset>();
|
subAssets = new List<SubAsset>();
|
||||||
@@ -47,11 +47,11 @@ namespace AssetUsageDetectorNamespace
|
|||||||
else
|
else
|
||||||
currentSubAssets.Clear();
|
currentSubAssets.Clear();
|
||||||
|
|
||||||
AddSubAssets( obj, false );
|
AddSubAssets( obj, false, shouldSearchChildren );
|
||||||
currentSubAssets.Clear();
|
currentSubAssets.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddSubAssets( Object target, bool includeTarget )
|
private void AddSubAssets( Object target, bool includeTarget, bool? shouldSearchChildren )
|
||||||
{
|
{
|
||||||
if( target == null || target.Equals( null ) )
|
if( target == null || target.Equals( null ) )
|
||||||
return;
|
return;
|
||||||
@@ -65,13 +65,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
// If this is a scene object, add its child objects to the sub-assets list
|
// If this is a scene object, add its child objects to the sub-assets list
|
||||||
// but don't include them in the search by default
|
// but don't include them in the search by default
|
||||||
Transform goTransform = go.transform;
|
Transform goTransform = go.transform;
|
||||||
Transform[] children = go.GetComponentsInChildren<Transform>();
|
Transform[] children = go.GetComponentsInChildren<Transform>( true );
|
||||||
for( int i = 0; i < children.Length; i++ )
|
for( int i = 0; i < children.Length; i++ )
|
||||||
{
|
{
|
||||||
if( ReferenceEquals( children[i], goTransform ) )
|
if( ReferenceEquals( children[i], goTransform ) )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
subAssets.Add( new SubAsset( children[i].gameObject, false ) );
|
subAssets.Add( new SubAsset( children[i].gameObject, shouldSearchChildren ?? false ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -83,7 +83,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
{
|
{
|
||||||
if( !currentSubAssets.Contains( target ) )
|
if( !currentSubAssets.Contains( target ) )
|
||||||
{
|
{
|
||||||
subAssets.Add( new SubAsset( target, true ) );
|
subAssets.Add( new SubAsset( target, shouldSearchChildren ?? true ) );
|
||||||
currentSubAssets.Add( target );
|
currentSubAssets.Add( target );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( target.IsFolder() )
|
if( target.IsFolder() )
|
||||||
{
|
{
|
||||||
foreach( string filePath in Utilities.EnumerateFolderContents( target ) )
|
foreach( string filePath in Utilities.EnumerateFolderContents( target ) )
|
||||||
AddSubAssets( AssetDatabase.LoadAssetAtPath<Object>( filePath ), true );
|
AddSubAssets( AssetDatabase.LoadAssetAtPath<Object>( filePath ), true, shouldSearchChildren );
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -112,7 +112,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
if( asset != target )
|
if( asset != target )
|
||||||
{
|
{
|
||||||
subAssets.Add( new SubAsset( asset, true ) );
|
subAssets.Add( new SubAsset( asset, shouldSearchChildren ?? true ) );
|
||||||
currentSubAssets.Add( asset );
|
currentSubAssets.Add( asset );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
if( !currentSubAssets.Contains( monoScriptsInProject[j] ) )
|
if( !currentSubAssets.Contains( monoScriptsInProject[j] ) )
|
||||||
{
|
{
|
||||||
subAssets.Add( new SubAsset( monoScriptsInProject[j], true ) );
|
subAssets.Add( new SubAsset( monoScriptsInProject[j], shouldSearchChildren ?? true ) );
|
||||||
currentSubAssets.Add( monoScriptsInProject[j] );
|
currentSubAssets.Add( monoScriptsInProject[j] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -328,7 +328,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
// Close the scenes that were not part of the initial scene setup
|
// Close the scenes that were not part of the initial scene setup
|
||||||
// Returns true if initial scene setup is restored successfully
|
// Returns true if initial scene setup is restored successfully
|
||||||
public bool RestoreInitialSceneSetup()
|
public bool RestoreInitialSceneSetup( bool optional )
|
||||||
{
|
{
|
||||||
if( initialSceneSetup == null || initialSceneSetup.Length == 0 )
|
if( initialSceneSetup == null || initialSceneSetup.Length == 0 )
|
||||||
return true;
|
return true;
|
||||||
@@ -336,6 +336,9 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( EditorApplication.isPlaying || !EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo() )
|
if( EditorApplication.isPlaying || !EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo() )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
if( optional && IsSceneSetupDifferentThanCurrentSetup() && !EditorUtility.DisplayDialog( "Scenes", "Restore initial scene setup?", "Yes", "Leave it as is" ) )
|
||||||
|
return true;
|
||||||
|
|
||||||
for( int i = 0; i < initialSceneSetup.Length; i++ )
|
for( int i = 0; i < initialSceneSetup.Length; i++ )
|
||||||
{
|
{
|
||||||
Scene scene = EditorSceneManager.GetSceneByPath( initialSceneSetup[i].path );
|
Scene scene = EditorSceneManager.GetSceneByPath( initialSceneSetup[i].path );
|
||||||
@@ -679,8 +682,8 @@ namespace AssetUsageDetectorNamespace
|
|||||||
parameters.shouldRefreshEditorWindow = true; // A repaint is needed to work nicely with GUILayout
|
parameters.shouldRefreshEditorWindow = true; // A repaint is needed to work nicely with GUILayout
|
||||||
}
|
}
|
||||||
|
|
||||||
Color c = GUI.color;
|
Color c = GUI.backgroundColor;
|
||||||
GUI.color = Color.cyan;
|
GUI.backgroundColor = Color.cyan;
|
||||||
|
|
||||||
Rect rect = parameters.guiRect;
|
Rect rect = parameters.guiRect;
|
||||||
float width = rect.width;
|
float width = rect.width;
|
||||||
@@ -739,7 +742,8 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
if( IsExpanded )
|
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 )
|
if( PendingSearch )
|
||||||
{
|
{
|
||||||
@@ -764,7 +768,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
parameters.guiRect = rect;
|
parameters.guiRect = rect;
|
||||||
GUI.color = c;
|
GUI.backgroundColor = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void GenerateGUINodes( SearchResultDrawParameters parameters )
|
private void GenerateGUINodes( SearchResultDrawParameters parameters )
|
||||||
|
|||||||
@@ -10,6 +10,10 @@ using UnityEditor.SceneManagement;
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.SceneManagement;
|
using UnityEngine.SceneManagement;
|
||||||
using Object = UnityEngine.Object;
|
using Object = UnityEngine.Object;
|
||||||
|
#if UNITY_2018_3_OR_NEWER
|
||||||
|
using PrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStage;
|
||||||
|
using PrefabStageUtility = UnityEditor.Experimental.SceneManagement.PrefabStageUtility;
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace AssetUsageDetectorNamespace
|
namespace AssetUsageDetectorNamespace
|
||||||
{
|
{
|
||||||
@@ -77,6 +81,14 @@ namespace AssetUsageDetectorNamespace
|
|||||||
alignment = TextAnchor.MiddleCenter,
|
alignment = TextAnchor.MiddleCenter,
|
||||||
font = EditorStyles.label.font
|
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;
|
return m_boxGUIStyle;
|
||||||
@@ -235,20 +247,28 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( prefabAssetType == PrefabAssetType.Regular || prefabAssetType == PrefabAssetType.Variant )
|
if( prefabAssetType == PrefabAssetType.Regular || prefabAssetType == PrefabAssetType.Variant )
|
||||||
{
|
{
|
||||||
string assetPath = AssetDatabase.GetAssetPath( objTR.gameObject );
|
string assetPath = AssetDatabase.GetAssetPath( objTR.gameObject );
|
||||||
var openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
PrefabStage openPrefabStage = PrefabStageUtility.GetCurrentPrefabStage();
|
||||||
|
|
||||||
// Try to open the prefab stage of pinged prefabs if they are double clicked
|
// Try to open the prefab stage of pinged prefabs if they are double clicked
|
||||||
if( previousPingedPrefabInstanceId == objTR.GetInstanceID() && EditorApplication.timeSinceStartup - previousPingedPrefabPingTime <= 0.3f &&
|
if( previousPingedPrefabInstanceId == objTR.GetInstanceID() && EditorApplication.timeSinceStartup - previousPingedPrefabPingTime <= 0.3f &&
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
( openPrefabStage == null || !openPrefabStage.stageHandle.IsValid() || assetPath != openPrefabStage.assetPath ) )
|
||||||
|
#else
|
||||||
( openPrefabStage == null || !openPrefabStage.stageHandle.IsValid() || assetPath != openPrefabStage.prefabAssetPath ) )
|
( openPrefabStage == null || !openPrefabStage.stageHandle.IsValid() || assetPath != openPrefabStage.prefabAssetPath ) )
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
AssetDatabase.OpenAsset( objTR.gameObject );
|
AssetDatabase.OpenAsset( objTR.gameObject );
|
||||||
openPrefabStage = UnityEditor.Experimental.SceneManagement.PrefabStageUtility.GetCurrentPrefabStage();
|
openPrefabStage = PrefabStageUtility.GetCurrentPrefabStage();
|
||||||
}
|
}
|
||||||
|
|
||||||
previousPingedPrefabInstanceId = objTR.GetInstanceID();
|
previousPingedPrefabInstanceId = objTR.GetInstanceID();
|
||||||
previousPingedPrefabPingTime = EditorApplication.timeSinceStartup;
|
previousPingedPrefabPingTime = EditorApplication.timeSinceStartup;
|
||||||
|
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() && assetPath == openPrefabStage.assetPath )
|
||||||
|
#else
|
||||||
if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() && assetPath == openPrefabStage.prefabAssetPath )
|
if( openPrefabStage != null && openPrefabStage.stageHandle.IsValid() && assetPath == openPrefabStage.prefabAssetPath )
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
GameObject prefabStageGO = FollowSymmetricHierarchy( (GameObject) obj, ( (GameObject) obj ).transform.root.gameObject, openPrefabStage.prefabContentsRoot );
|
GameObject prefabStageGO = FollowSymmetricHierarchy( (GameObject) obj, ( (GameObject) obj ).transform.root.gameObject, openPrefabStage.prefabContentsRoot );
|
||||||
if( prefabStageGO != null )
|
if( prefabStageGO != null )
|
||||||
@@ -318,9 +338,17 @@ namespace AssetUsageDetectorNamespace
|
|||||||
// Check if the field is serializable
|
// Check if the field is serializable
|
||||||
public static bool IsSerializable( this FieldInfo fieldInfo )
|
public static bool IsSerializable( this FieldInfo fieldInfo )
|
||||||
{
|
{
|
||||||
// see Serialization Rules: https://docs.unity3d.com/Manual/script-Serialization.html
|
// See Serialization Rules: https://docs.unity3d.com/Manual/script-Serialization.html
|
||||||
if( fieldInfo.IsInitOnly || ( ( !fieldInfo.IsPublic || fieldInfo.IsNotSerialized ) &&
|
if( fieldInfo.IsInitOnly )
|
||||||
!Attribute.IsDefined( fieldInfo, typeof( SerializeField ) ) ) )
|
return false;
|
||||||
|
|
||||||
|
#if UNITY_2019_3_OR_NEWER
|
||||||
|
// SerializeReference makes even System.Object fields serializable
|
||||||
|
if( Attribute.IsDefined( fieldInfo, typeof( SerializeReference ) ) )
|
||||||
|
return true;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if( ( !fieldInfo.IsPublic || fieldInfo.IsNotSerialized ) && !Attribute.IsDefined( fieldInfo, typeof( SerializeField ) ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return IsTypeSerializable( fieldInfo.FieldType );
|
return IsTypeSerializable( fieldInfo.FieldType );
|
||||||
@@ -351,6 +379,16 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
else if( type.IsGenericType )
|
else if( type.IsGenericType )
|
||||||
{
|
{
|
||||||
|
// Generic types are allowed on 2020.1 and later
|
||||||
|
#if UNITY_2020_1_OR_NEWER
|
||||||
|
if( type.GetGenericTypeDefinition() == typeof( List<> ) )
|
||||||
|
{
|
||||||
|
type = type.GetGenericArguments()[0];
|
||||||
|
|
||||||
|
if( typeof( Object ).IsAssignableFrom( type ) )
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#else
|
||||||
if( type.GetGenericTypeDefinition() != typeof( List<> ) )
|
if( type.GetGenericTypeDefinition() != typeof( List<> ) )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@@ -358,10 +396,13 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
if( typeof( Object ).IsAssignableFrom( type ) )
|
if( typeof( Object ).IsAssignableFrom( type ) )
|
||||||
return true;
|
return true;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !UNITY_2020_1_OR_NEWER
|
||||||
if( type.IsGenericType )
|
if( type.IsGenericType )
|
||||||
return false;
|
return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
return Attribute.IsDefined( type, typeof( SerializableAttribute ), false );
|
return Attribute.IsDefined( type, typeof( SerializableAttribute ), false );
|
||||||
}
|
}
|
||||||
@@ -382,8 +423,8 @@ namespace AssetUsageDetectorNamespace
|
|||||||
if( typeof( Type ).IsAssignableFrom( type ) || type.Namespace == reflectionNamespace )
|
if( typeof( Type ).IsAssignableFrom( type ) || type.Namespace == reflectionNamespace )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
// Searching pointer variables for reference throws ArgumentException
|
// Searching pointers or ref variables for reference throws ArgumentException
|
||||||
if( type.IsPointer )
|
if( type.IsPointer || type.IsByRef )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
@@ -413,7 +454,20 @@ namespace AssetUsageDetectorNamespace
|
|||||||
{
|
{
|
||||||
// Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs
|
// Can't use PropertyWrapper (which uses CreateDelegate) for property getters of structs
|
||||||
if( propertyInfo.DeclaringType.IsValueType )
|
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 );
|
Type GenType = typeof( PropertyWrapper<,> ).MakeGenericType( propertyInfo.DeclaringType, propertyInfo.PropertyType );
|
||||||
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
|
return ( (IPropertyAccessor) Activator.CreateInstance( GenType, propertyInfo.GetGetMethod( true ) ) ).GetValue;
|
||||||
|
|||||||
@@ -69,8 +69,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// Property getters may return various kinds of exceptions
|
// Property getters may return various kinds of exceptions if their backing fields are not initialized (yet)
|
||||||
// if their backing fields are not initialized (yet)
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -1,6 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "com.yasirkula.assetusagedetector",
|
"name": "com.yasirkula.assetusagedetector",
|
||||||
"displayName": "Asset Usage Detector",
|
"displayName": "Asset Usage Detector",
|
||||||
"version": "1.8.2",
|
"version": "1.9.4",
|
||||||
|
"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!"
|
"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!"
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user