mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-19 14:32:37 +08:00
- Added "Assets/Search For References" context menu item to quickly search for the selected object(s)' references
- Added AssetUsageDetectorWindow.ShowAndSearch function to initiate a search from editor scripts - Object references added to animation events are now searched - Faster serialization/deserialization of the cache (replaced BinaryFormatter with BinaryWriter, this will trigger a new cache initialization at first search) - Some UI elements now use a different GUIStyle (for better readability on dark skin)
This commit is contained in:
Binary file not shown.
@@ -10,7 +10,6 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.IO;
|
||||
#if UNITY_2017_1_OR_NEWER
|
||||
using UnityEngine.U2D;
|
||||
@@ -45,21 +44,28 @@ namespace AssetUsageDetectorNamespace
|
||||
public BindingFlags fieldModifiers, propertyModifiers;
|
||||
|
||||
public bool noAssetDatabaseChanges;
|
||||
|
||||
public Parameters()
|
||||
{
|
||||
objectsToSearch = null;
|
||||
searchInScenes = SceneSearchMode.AllScenes;
|
||||
searchInAssetsFolder = true;
|
||||
searchInAssetsSubset = null;
|
||||
searchDepthLimit = 4;
|
||||
fieldModifiers = BindingFlags.Public | BindingFlags.NonPublic;
|
||||
propertyModifiers = BindingFlags.Public | BindingFlags.NonPublic;
|
||||
noAssetDatabaseChanges = false;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
private class CacheEntry
|
||||
{
|
||||
public enum Result { Unknown = 0, No = 1, Yes = 2 };
|
||||
|
||||
public string[] dependencies;
|
||||
public string hash;
|
||||
public bool hasSelfDependency;
|
||||
public string[] dependencies;
|
||||
|
||||
[NonSerialized]
|
||||
public bool verified;
|
||||
|
||||
[NonSerialized]
|
||||
public Result searchResult;
|
||||
|
||||
public CacheEntry( string path )
|
||||
@@ -67,30 +73,19 @@ namespace AssetUsageDetectorNamespace
|
||||
Verify( path );
|
||||
}
|
||||
|
||||
public CacheEntry( string hash, string[] dependencies )
|
||||
{
|
||||
this.hash = hash;
|
||||
this.dependencies = dependencies;
|
||||
}
|
||||
|
||||
public void Verify( string path )
|
||||
{
|
||||
string hash = AssetDatabase.GetAssetDependencyHash( path ).ToString();
|
||||
if( this.hash != hash )
|
||||
{
|
||||
this.hash = hash;
|
||||
|
||||
dependencies = AssetDatabase.GetDependencies( path, false );
|
||||
hasSelfDependency = false;
|
||||
|
||||
for( int i = 0; i < dependencies.Length; i++ )
|
||||
{
|
||||
if( dependencies[i] == path )
|
||||
{
|
||||
int newSize = dependencies.Length - 1;
|
||||
if( i < newSize )
|
||||
dependencies[i] = dependencies[newSize];
|
||||
|
||||
Array.Resize( ref dependencies, newSize );
|
||||
hasSelfDependency = true;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
verified = true;
|
||||
@@ -98,8 +93,6 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
#endregion
|
||||
|
||||
private const string DEPENDENCY_CACHE_PATH = "Library/AssetUsageDetector.cache"; // Path of the cache file
|
||||
|
||||
private HashSet<Object> objectsToSearchSet; // A set that contains the searched asset(s) and their sub-assets (if any)
|
||||
|
||||
private HashSet<Object> sceneObjectsToSearchSet; // Scene object(s) in objectsToSearchSet
|
||||
@@ -140,6 +133,8 @@ namespace AssetUsageDetectorNamespace
|
||||
private List<ReferenceNode> nodesPool = new List<ReferenceNode>( 32 );
|
||||
private List<VariableGetterHolder> validVariables = new List<VariableGetterHolder>( 32 );
|
||||
|
||||
private string CachePath { get { return Application.dataPath + "/../Library/AssetUsageDetector.cache"; } } // Path of the cache file
|
||||
|
||||
// Search for references!
|
||||
public SearchResult Run( Parameters searchParameters )
|
||||
{
|
||||
@@ -163,8 +158,8 @@ namespace AssetUsageDetectorNamespace
|
||||
searchedObjectsCount = 0;
|
||||
searchStartTime = EditorApplication.timeSinceStartup;
|
||||
|
||||
this.fieldModifiers = searchParameters.fieldModifiers;
|
||||
this.propertyModifiers = searchParameters.propertyModifiers;
|
||||
this.fieldModifiers = searchParameters.fieldModifiers | BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
||||
this.propertyModifiers = searchParameters.propertyModifiers | BindingFlags.Instance | BindingFlags.DeclaredOnly;
|
||||
this.searchDepthLimit = searchParameters.searchDepthLimit;
|
||||
|
||||
// Initialize commonly used variables
|
||||
@@ -268,6 +263,10 @@ namespace AssetUsageDetectorNamespace
|
||||
|
||||
foreach( string filePath in folderContentsSet )
|
||||
{
|
||||
// Skip scene assets
|
||||
if( filePath.EndsWith( ".unity" ) )
|
||||
continue;
|
||||
|
||||
Object[] assets = AssetDatabase.LoadAllAssetsAtPath( filePath );
|
||||
if( assets == null || assets.Length == 0 )
|
||||
continue;
|
||||
@@ -303,8 +302,18 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
// Find the scenes to search for references
|
||||
HashSet<string> openScenes = null;
|
||||
if( EditorApplication.isPlaying )
|
||||
searchParameters.searchInScenes = ( searchParameters.searchInScenes & SceneSearchMode.OpenScenes ) == SceneSearchMode.OpenScenes ? SceneSearchMode.OpenScenes : SceneSearchMode.None;
|
||||
{
|
||||
// In Play mode, only open scenes can be searched
|
||||
openScenes = new HashSet<string>();
|
||||
for( int i = 0; i < EditorSceneManager.loadedSceneCount; i++ )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneAt( i );
|
||||
if( scene.IsValid() )
|
||||
openScenes.Add( scene.path );
|
||||
}
|
||||
}
|
||||
|
||||
HashSet<string> scenesToSearch = new HashSet<string>();
|
||||
if( ( searchParameters.searchInScenes & SceneSearchMode.AllScenes ) == SceneSearchMode.AllScenes )
|
||||
@@ -312,7 +321,11 @@ namespace AssetUsageDetectorNamespace
|
||||
// Get all scenes from the Assets folder
|
||||
string[] sceneGuids = AssetDatabase.FindAssets( "t:SceneAsset" );
|
||||
for( int i = 0; i < sceneGuids.Length; i++ )
|
||||
scenesToSearch.Add( AssetDatabase.GUIDToAssetPath( sceneGuids[i] ) );
|
||||
{
|
||||
string scenePath = AssetDatabase.GUIDToAssetPath( sceneGuids[i] );
|
||||
if( !EditorApplication.isPlaying || openScenes.Contains( scenePath ) )
|
||||
scenesToSearch.Add( scenePath );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -334,7 +347,7 @@ namespace AssetUsageDetectorNamespace
|
||||
EditorBuildSettingsScene[] scenesTemp = EditorBuildSettings.scenes;
|
||||
for( int i = 0; i < scenesTemp.Length; i++ )
|
||||
{
|
||||
if( searchInScenesInBuildTickedAll || scenesTemp[i].enabled )
|
||||
if( ( searchInScenesInBuildTickedAll || scenesTemp[i].enabled ) && ( !EditorApplication.isPlaying || openScenes.Contains( scenesTemp[i].path ) ) )
|
||||
scenesToSearch.Add( scenesTemp[i].path );
|
||||
}
|
||||
}
|
||||
@@ -924,6 +937,11 @@ namespace AssetUsageDetectorNamespace
|
||||
referenceNode.AddLinkTo( SearchObject( keyframes[j].value ), "Keyframe: " + keyframes[j].time );
|
||||
}
|
||||
|
||||
// Get all events from animation clip
|
||||
AnimationEvent[] events = AnimationUtility.GetAnimationEvents( clip );
|
||||
for( int i = 0; i < events.Length; i++ )
|
||||
referenceNode.AddLinkTo( SearchObject( events[i].objectReferenceParameter ), "AnimationEvent: " + events[i].time );
|
||||
|
||||
return referenceNode;
|
||||
}
|
||||
|
||||
@@ -1118,12 +1136,6 @@ namespace AssetUsageDetectorNamespace
|
||||
if( cacheEntry.searchResult != CacheEntry.Result.Unknown )
|
||||
return cacheEntry.searchResult == CacheEntry.Result.Yes;
|
||||
|
||||
if( cacheEntry.hasSelfDependency && assetsToSearchPathsSet.Contains( assetPath ) )
|
||||
{
|
||||
cacheEntry.searchResult = CacheEntry.Result.Yes;
|
||||
return true;
|
||||
}
|
||||
|
||||
cacheEntry.searchResult = CacheEntry.Result.No;
|
||||
|
||||
string[] dependencies = cacheEntry.dependencies;
|
||||
@@ -1214,33 +1226,61 @@ namespace AssetUsageDetectorNamespace
|
||||
if( assetDependencyCache == null )
|
||||
return;
|
||||
|
||||
string cachePath = Application.dataPath + "/../" + DEPENDENCY_CACHE_PATH;
|
||||
using( FileStream stream = new FileStream( cachePath, FileMode.Create ) )
|
||||
try
|
||||
{
|
||||
try
|
||||
using( FileStream stream = new FileStream( CachePath, FileMode.Create ) )
|
||||
using( BinaryWriter writer = new BinaryWriter( stream ) )
|
||||
{
|
||||
new BinaryFormatter().Serialize( stream, assetDependencyCache );
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
Debug.LogException( e );
|
||||
writer.Write( assetDependencyCache.Count );
|
||||
|
||||
foreach( var keyValuePair in assetDependencyCache )
|
||||
{
|
||||
CacheEntry cacheEntry = keyValuePair.Value;
|
||||
string[] dependencies = cacheEntry.dependencies;
|
||||
|
||||
writer.Write( keyValuePair.Key );
|
||||
writer.Write( cacheEntry.hash );
|
||||
writer.Write( dependencies.Length );
|
||||
|
||||
for( int i = 0; i < dependencies.Length; i++ )
|
||||
writer.Write( dependencies[i] );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
Debug.LogException( e );
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadCache()
|
||||
{
|
||||
string cachePath = Application.dataPath + "/../" + DEPENDENCY_CACHE_PATH;
|
||||
if( File.Exists( cachePath ) )
|
||||
if( File.Exists( CachePath ) )
|
||||
{
|
||||
using( FileStream stream = new FileStream( cachePath, FileMode.Open, FileAccess.Read ) )
|
||||
using( FileStream stream = new FileStream( CachePath, FileMode.Open, FileAccess.Read ) )
|
||||
using( BinaryReader reader = new BinaryReader( stream ) )
|
||||
{
|
||||
try
|
||||
{
|
||||
assetDependencyCache = (Dictionary<string, CacheEntry>) new BinaryFormatter().Deserialize( stream );
|
||||
int cacheSize = reader.ReadInt32();
|
||||
assetDependencyCache = new Dictionary<string, CacheEntry>( cacheSize );
|
||||
|
||||
for( int i = 0; i < cacheSize; i++ )
|
||||
{
|
||||
string assetPath = reader.ReadString();
|
||||
string hash = reader.ReadString();
|
||||
|
||||
int dependenciesLength = reader.ReadInt32();
|
||||
string[] dependencies = new string[dependenciesLength];
|
||||
for( int j = 0; j < dependenciesLength; j++ )
|
||||
dependencies[j] = reader.ReadString();
|
||||
|
||||
assetDependencyCache[assetPath] = new CacheEntry( hash, dependencies );
|
||||
}
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
assetDependencyCache = null;
|
||||
Debug.LogException( e );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using UnityEngine.SceneManagement;
|
||||
using UnityEditor.SceneManagement;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Object = UnityEngine.Object;
|
||||
@@ -66,6 +64,55 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
window.Show();
|
||||
}
|
||||
|
||||
// Quickly initiate search for the selected assets
|
||||
[MenuItem( "Assets/Search for References", priority = 1000 )]
|
||||
private static void SearchSelectedAssetReferences()
|
||||
{
|
||||
ShowAndSearch( Selection.objects );
|
||||
}
|
||||
|
||||
// Show the menu item only if there is a selection in the Editor
|
||||
[MenuItem( "Assets/Search for References", validate = true )]
|
||||
private static bool SearchSelectedAssetReferencesValidate()
|
||||
{
|
||||
return Selection.objects.Length > 0;
|
||||
}
|
||||
|
||||
// Quickly show the AssetUsageDetector window and initiate a search
|
||||
public static void ShowAndSearch( IEnumerable<Object> searchObjects )
|
||||
{
|
||||
ShowAndSearch( new AssetUsageDetector.Parameters() { objectsToSearch = searchObjects } );
|
||||
}
|
||||
|
||||
// Quickly show the AssetUsageDetector window and initiate a search
|
||||
public static void ShowAndSearch( AssetUsageDetector.Parameters searchParameters )
|
||||
{
|
||||
if( searchParameters == null )
|
||||
Debug.LogError( "searchParameters can't be null!" );
|
||||
else if( searchParameters.objectsToSearch.IsEmpty() )
|
||||
Debug.LogError( "searchParameters.objectsToSearch can't be empty!" );
|
||||
else if( !EditorApplication.isPlaying && !Utilities.AreScenesSaved() )
|
||||
Debug.LogError( "Save open scenes first!" );
|
||||
else
|
||||
{
|
||||
Init();
|
||||
AssetUsageDetectorWindow window = GetWindow<AssetUsageDetectorWindow>();
|
||||
|
||||
if( window.objectsToSearch == null )
|
||||
window.objectsToSearch = new List<ObjectToSearch>();
|
||||
else
|
||||
window.objectsToSearch.Clear();
|
||||
|
||||
foreach( Object obj in searchParameters.objectsToSearch )
|
||||
window.objectsToSearch.Add( new ObjectToSearch( obj ) );
|
||||
|
||||
window.errorMessage = string.Empty;
|
||||
window.currentPhase = Phase.Processing;
|
||||
window.searchResult = window.core.Run( searchParameters );
|
||||
window.currentPhase = Phase.Complete;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if( core != null )
|
||||
@@ -111,8 +158,8 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
searchDepthLimit = EditorPrefs.GetInt( PREFS_SEARCH_DEPTH_LIMIT, 4 );
|
||||
|
||||
// Fetch public, protected and private non-static fields and properties from objects by default
|
||||
fieldModifiers = (BindingFlags) EditorPrefs.GetInt( PREFS_SEARCH_FIELDS, (int) ( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic ) );
|
||||
propertyModifiers = (BindingFlags) EditorPrefs.GetInt( PREFS_SEARCH_PROPERTIES, (int) ( BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic ) );
|
||||
fieldModifiers = (BindingFlags) EditorPrefs.GetInt( PREFS_SEARCH_FIELDS, (int) ( BindingFlags.Public | BindingFlags.NonPublic ) );
|
||||
propertyModifiers = (BindingFlags) EditorPrefs.GetInt( PREFS_SEARCH_PROPERTIES, (int) ( BindingFlags.Public | BindingFlags.NonPublic ) );
|
||||
|
||||
try
|
||||
{
|
||||
@@ -168,7 +215,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
GUILayout.Box( "SEARCH IN", Utilities.GL_EXPAND_WIDTH );
|
||||
GUILayout.Box( "SEARCH IN", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
|
||||
searchInAssetsFolder = EditorGUILayout.ToggleLeft( "Project view (Assets folder)", searchInAssetsFolder );
|
||||
|
||||
@@ -221,7 +268,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
GUILayout.Box( "SEARCH SETTINGS", Utilities.GL_EXPAND_WIDTH );
|
||||
GUILayout.Box( "SEARCH SETTINGS", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
|
||||
GUILayout.BeginHorizontal();
|
||||
|
||||
@@ -279,7 +326,7 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
{
|
||||
if( objectsToSearch.IsEmpty() )
|
||||
errorMessage = "ADD AN ASSET TO THE LIST FIRST!";
|
||||
else if( !EditorApplication.isPlaying && !AreScenesSaved() )
|
||||
else if( !EditorApplication.isPlaying && !Utilities.AreScenesSaved() )
|
||||
{
|
||||
// Don't start the search if at least one scene is currently dirty (not saved)
|
||||
errorMessage = "SAVE OPEN SCENES FIRST!";
|
||||
@@ -348,13 +395,13 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
|
||||
Color c = GUI.color;
|
||||
GUI.color = Color.green;
|
||||
GUILayout.Box( "Don't forget to save scene(s) if you made any changes!", 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;
|
||||
|
||||
GUILayout.Space( 10 );
|
||||
|
||||
if( searchResult.NumberOfGroups == 0 )
|
||||
GUILayout.Box( "No results found...", Utilities.GL_EXPAND_WIDTH );
|
||||
GUILayout.Box( "No results found...", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
|
||||
else
|
||||
{
|
||||
GUILayout.BeginHorizontal();
|
||||
@@ -611,19 +658,6 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
}
|
||||
}
|
||||
|
||||
// Check if all open scenes are saved (not dirty)
|
||||
private bool AreScenesSaved()
|
||||
{
|
||||
for( int i = 0; i < EditorSceneManager.loadedSceneCount; i++ )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneAt( i );
|
||||
if( scene.isDirty || string.IsNullOrEmpty( scene.path ) )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ReturnToSetupPhase( bool restoreInitialSceneSetup )
|
||||
{
|
||||
if( searchResult != null && restoreInitialSceneSetup && !EditorApplication.isPlaying && !searchResult.RestoreInitialSceneSetup() )
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace AssetUsageDetectorNamespace.Extras
|
||||
@@ -337,6 +339,20 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if all open scenes are saved (not dirty)
|
||||
public static bool AreScenesSaved()
|
||||
{
|
||||
for( int i = 0; i < EditorSceneManager.loadedSceneCount; i++ )
|
||||
{
|
||||
Scene scene = EditorSceneManager.GetSceneAt( i );
|
||||
if( scene.isDirty || string.IsNullOrEmpty( scene.path ) )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if all the objects inside the list are null
|
||||
public static bool IsEmpty( this List<ObjectToSearch> objectsToSearch )
|
||||
{
|
||||
if( objectsToSearch == null )
|
||||
@@ -351,18 +367,37 @@ namespace AssetUsageDetectorNamespace.Extras
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsEmpty( this List<Object> searchInAssetsSubset )
|
||||
// Check if all the objects inside the list are null
|
||||
public static bool IsEmpty( this List<Object> objects )
|
||||
{
|
||||
if( searchInAssetsSubset == null )
|
||||
if( objects == null )
|
||||
return true;
|
||||
|
||||
for( int i = 0; i < searchInAssetsSubset.Count; i++ )
|
||||
for( int i = 0; i < objects.Count; i++ )
|
||||
{
|
||||
if( searchInAssetsSubset[i] != null && !searchInAssetsSubset[i].Equals( null ) )
|
||||
if( objects[i] != null && !objects[i].Equals( null ) )
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if all the objects that are enumerated are null
|
||||
public static bool IsEmpty( this IEnumerable<Object> objects )
|
||||
{
|
||||
if( objects == null )
|
||||
return true;
|
||||
|
||||
using( IEnumerator<Object> enumerator = objects.GetEnumerator() )
|
||||
{
|
||||
while( enumerator.MoveNext() )
|
||||
{
|
||||
if( enumerator.Current != null && !enumerator.Current.Equals( null ) )
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user