Compare commits

..

7 Commits

8 changed files with 233 additions and 100 deletions
@@ -53,8 +53,8 @@ namespace AssetUsageDetectorNamespace
#endif
public bool calculateUnusedObjects = false;
public bool hideDuplicateRows = true;
public bool hideNonOverriddenPrefabVariablesInAssets = true;
public bool hideNonOverriddenPrefabVariablesInScenes = false;
public bool hideRedundantPrefabReferencesInAssets = false;
public bool hideRedundantPrefabReferencesInScenes = false;
public bool noAssetDatabaseChanges = false;
public bool showDetailedProgressBar = true;
}
@@ -454,7 +454,7 @@ namespace AssetUsageDetectorNamespace
}
// If a reference is found in the Project view, save the results
if( currentSearchResultGroup.NumberOfReferences > 0 )
if( currentSearchResultGroup.AnyReferencesFound )
searchResult.Add( currentSearchResultGroup );
}
@@ -479,7 +479,7 @@ namespace AssetUsageDetectorNamespace
}
}
if( currentSearchResultGroup.NumberOfReferences > 0 )
if( currentSearchResultGroup.AnyReferencesFound )
searchResult.Add( currentSearchResultGroup );
}
@@ -554,7 +554,7 @@ namespace AssetUsageDetectorNamespace
for( int i = 0; i < rootGameObjects.Length; i++ )
SearchGameObjectRecursively( rootGameObjects[i] );
if( currentSearchResultGroup.NumberOfReferences > 0 )
if( currentSearchResultGroup.AnyReferencesFound )
searchResult.Add( currentSearchResultGroup );
}
@@ -663,7 +663,7 @@ namespace AssetUsageDetectorNamespace
searchResult[i].InitializeNodes( objectsToSearchSet );
// Remove empty search result groups
if( !searchResult[i].PendingSearch && searchResult[i].NumberOfReferences == 0 )
if( !searchResult[i].PendingSearch && !searchResult[i].AnyReferencesFound )
searchResult.RemoveAt( i-- );
}
}
@@ -982,11 +982,9 @@ namespace AssetUsageDetectorNamespace
EditorSceneManager.CloseScene( scene, true );
}
}
else
{
// Some references are found in this scene, save the results
if( currentSearchResultGroup.AnyReferencesFound )
searchResult.Add( currentSearchResultGroup );
}
}
// Search a GameObject and its children for references recursively
@@ -397,16 +397,21 @@ namespace AssetUsageDetectorNamespace
// Check if this GameObject's prefab is one of the selected assets
if( searchPrefabConnections )
{
#if UNITY_2018_3_OR_NEWER
Object prefab = go;
while( prefab = PrefabUtility.GetCorrespondingObjectFromSource( prefab ) )
#else
Object prefab = PrefabUtility.GetPrefabParent( go );
if( prefab )
#endif
bool? skipFurtherPrefabReferences = null;
for( GameObject goPrefab = go.GetPrefabParent(), prefab = goPrefab; prefab != null; prefab = prefab.GetPrefabParent() )
{
if( objectsToSearchSet.Contains( prefab ) && assetsToSearchRootPrefabs.ContainsFast( prefab as GameObject ) )
if( objectsToSearchSet.Contains( prefab ) && assetsToSearchRootPrefabs.ContainsFast( prefab ) )
{
if( goPrefab != prefab && !skipFurtherPrefabReferences.HasValue )
{
skipFurtherPrefabReferences = ShouldExcludeRedundantPrefabReferences( go ) && !go.HasAnyPrefabOverrides();
if( skipFurtherPrefabReferences.Value )
{
currentSearchResultGroup.NumberOfRedundantReferences++;
break;
}
}
referenceNode.AddLinkTo( GetReferenceNode( prefab ), "Prefab object" );
if( searchParameters.searchRefactoring != null )
@@ -440,10 +445,15 @@ namespace AssetUsageDetectorNamespace
MonoScript script = MonoScript.FromMonoBehaviour( (MonoBehaviour) component );
if( objectsToSearchSet.Contains( script ) )
{
referenceNode.AddLinkTo( GetReferenceNode( script ) );
if( ShouldExcludeRedundantPrefabReferences( component ) && !component.HasAnyPrefabOverrides() )
currentSearchResultGroup.NumberOfRedundantReferences++;
else
{
referenceNode.AddLinkTo( GetReferenceNode( script ) );
if( searchParameters.searchRefactoring != null )
searchParameters.searchRefactoring( new BehaviourUsageMatch( component.gameObject, script, component ) );
if( searchParameters.searchRefactoring != null )
searchParameters.searchRefactoring( new BehaviourUsageMatch( component.gameObject, script, component ) );
}
}
}
@@ -1452,9 +1462,7 @@ namespace AssetUsageDetectorNamespace
if( iterator.Next( true ) )
{
bool iteratingVisible = iteratorVisible.NextVisible( true );
#if UNITY_2018_3_OR_NEWER
bool searchPrefabOverridesOnly = ( ( searchParameters.hideNonOverriddenPrefabVariablesInAssets && unityObject.IsAsset() ) || ( searchParameters.hideNonOverriddenPrefabVariablesInScenes && !unityObject.IsAsset() ) ) && PrefabUtility.GetCorrespondingObjectFromSource( unityObject ) != null;
#endif
bool searchPrefabOverridesOnly = ShouldExcludeRedundantPrefabReferences( unityObject );
bool enterChildren;
do
{
@@ -1471,10 +1479,6 @@ namespace AssetUsageDetectorNamespace
if( !isVisible )
enterChildren = false;
#if UNITY_2018_3_OR_NEWER
else if( searchPrefabOverridesOnly && !iterator.prefabOverride )
enterChildren = false;
#endif
else
{
Object propertyValue;
@@ -1500,6 +1504,7 @@ namespace AssetUsageDetectorNamespace
break;
#endif
case SerializedPropertyType.Generic:
{
#if ASSET_USAGE_ADDRESSABLES
if( searchParameters.addressablesSupport && iterator.type.StartsWithFast( "AssetReference" ) && GetRawSerializedPropertyValue( iterator ) is AssetReference assetReference )
{
@@ -1526,6 +1531,7 @@ namespace AssetUsageDetectorNamespace
}
break;
}
default:
propertyValue = null;
searchResult = null;
@@ -1540,10 +1546,18 @@ namespace AssetUsageDetectorNamespace
// m_RD.texture is a redundant reference that shows up when searching sprites
if( !propertyPath.EndsWithFast( "m_RD.texture" ) )
{
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
if( searchPrefabOverridesOnly && !iterator.prefabOverride )
{
currentSearchResultGroup.NumberOfRedundantReferences++;
enterChildren = false;
}
else
{
referenceNode.AddLinkTo( searchResult, "Variable: " + propertyPath.Replace( ".Array.data[", "[" ) ); // "arrayVariable.Array.data[0]" becomes "arrayVariable[0]"
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
searchParameters.searchRefactoring( new SerializedPropertyMatch( unityObject, propertyValue, iterator ) );
if( searchParameters.searchRefactoring != null && objectsToSearchSet.Contains( propertyValue ) )
searchParameters.searchRefactoring( new SerializedPropertyMatch( unityObject, propertyValue, iterator ) );
}
}
}
}
@@ -1922,6 +1936,11 @@ namespace AssetUsageDetectorNamespace
}
#endif
private bool ShouldExcludeRedundantPrefabReferences( Object obj )
{
return ( ( searchParameters.hideRedundantPrefabReferencesInAssets && obj.IsAsset() ) || ( searchParameters.hideRedundantPrefabReferencesInScenes && !obj.IsAsset() ) ) && obj.GetPrefabParent() != null;
}
// Iterates over all occurrences of specific key-value pairs in string
// Example1: #include "VALUE" valuePrefix=#include, valueWrapperChar="
// Example2: "guid": "VALUE" valuePrefix="guid", valueWrapperChar="
@@ -32,11 +32,13 @@ namespace AssetUsageDetectorNamespace
private const string PREFS_SEARCH_NON_SERIALIZABLES = "AUD_NonSerializables";
private const string PREFS_SEARCH_UNUSED_MATERIAL_PROPERTIES = "AUD_SearchUnusedMaterialProps";
private const string PREFS_LAZY_SCENE_SEARCH = "AUD_LazySceneSearch";
#if ASSET_USAGE_ADDRESSABLES
private const string PREFS_ADDRESSABLES_SUPPORT = "AUD_AddressablesSupport";
#endif
private const string PREFS_CALCULATE_UNUSED_OBJECTS = "AUD_FindUnusedObjs";
private const string PREFS_HIDE_DUPLICATE_ROWS = "AUD_HideDuplicates";
private const string PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_ASSETS = "AUD_HideRedundantPVariantLinks";
private const string PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_SCENES = "AUD_HideRedundantPVarsInScenes";
private const string PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_ASSETS = "AUD_HideRedundantPRefsInAssets";
private const string PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_SCENES = "AUD_HideRedundantPRefsInScenes";
private const string PREFS_SHOW_PROGRESS = "AUD_Progress";
private static readonly GUIContent windowTitle = new GUIContent( "Asset Usage Detector" );
@@ -44,6 +46,16 @@ namespace AssetUsageDetectorNamespace
private static readonly GUILayoutOption GL_WIDTH_12 = GUILayout.Width( 12f );
private static readonly GUIContent sharedGUIContent = new GUIContent();
private readonly GUIContent hideRedundantPrefabReferencesInAssetsLabel = new GUIContent( "Hide redundant prefab references in Assets", "Hides redundant/non-overridden references in prefab variants and nested prefabs. " +
"This will help focus on only the references that actually matter. For example:\n\n" +
"- Material CloudMat is assigned to prefab Cloud and its variant CloudBig. Since changing Cloud's material will also affect CloudBig, search results won't show CloudBig's reference" );
private readonly GUIContent hideRedundantPrefabReferencesInScenesLabel = new GUIContent( "Hide redundant prefab references in Scenes", "Hides redundant/non-overridden references in prefab instances. " +
"This will help focus on only the references that actually matter. For example:\n\n" +
"- Prefab Healthbar is nested inside prefab Player. An instance of Player exists in the current scene and its Healthbar isn't modified. Since modifying Healthbar in Player prefab will also affect " +
"the instance in the scene, search results won't show the instance in the scene while searching for Healthbar's references" );
private GUIStyle lockButtonStyle;
private readonly AssetUsageDetector core = new AssetUsageDetector();
@@ -94,8 +106,8 @@ namespace AssetUsageDetectorNamespace
private bool searchUnusedMaterialProperties = true;
private bool calculateUnusedObjects = false;
private bool hideDuplicateRows = true;
private bool hideNonOverriddenPrefabVariablesInAssets = true;
private bool hideNonOverriddenPrefabVariablesInScenes = false;
private bool hideRedundantPrefabReferencesInAssets = false;
private bool hideRedundantPrefabReferencesInScenes = false;
private bool noAssetDatabaseChanges = false;
private bool showDetailedProgressBar = true;
@@ -333,8 +345,8 @@ namespace AssetUsageDetectorNamespace
#endif
calculateUnusedObjects = searchParameters.calculateUnusedObjects;
hideDuplicateRows = searchParameters.hideDuplicateRows;
hideNonOverriddenPrefabVariablesInAssets = searchParameters.hideNonOverriddenPrefabVariablesInAssets;
hideNonOverriddenPrefabVariablesInScenes = searchParameters.hideNonOverriddenPrefabVariablesInScenes;
hideRedundantPrefabReferencesInAssets = searchParameters.hideRedundantPrefabReferencesInAssets;
hideRedundantPrefabReferencesInScenes = searchParameters.hideRedundantPrefabReferencesInScenes;
noAssetDatabaseChanges = searchParameters.noAssetDatabaseChanges;
showDetailedProgressBar = searchParameters.showDetailedProgressBar;
@@ -417,8 +429,8 @@ namespace AssetUsageDetectorNamespace
#endif
EditorPrefs.SetBool( PREFS_CALCULATE_UNUSED_OBJECTS, calculateUnusedObjects );
EditorPrefs.SetBool( PREFS_HIDE_DUPLICATE_ROWS, hideDuplicateRows );
EditorPrefs.SetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_ASSETS, hideNonOverriddenPrefabVariablesInAssets );
EditorPrefs.SetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_SCENES, hideNonOverriddenPrefabVariablesInScenes );
EditorPrefs.SetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_ASSETS, hideRedundantPrefabReferencesInAssets );
EditorPrefs.SetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_SCENES, hideRedundantPrefabReferencesInScenes );
EditorPrefs.SetBool( PREFS_SHOW_PROGRESS, showDetailedProgressBar );
}
@@ -440,8 +452,8 @@ namespace AssetUsageDetectorNamespace
#endif
calculateUnusedObjects = EditorPrefs.GetBool( PREFS_CALCULATE_UNUSED_OBJECTS, false );
hideDuplicateRows = EditorPrefs.GetBool( PREFS_HIDE_DUPLICATE_ROWS, true );
hideNonOverriddenPrefabVariablesInAssets = EditorPrefs.GetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_ASSETS, true );
hideNonOverriddenPrefabVariablesInScenes = EditorPrefs.GetBool( PREFS_HIDE_NON_OVERRIDDEN_PREFAB_VARIABLES_IN_SCENES, false );
hideRedundantPrefabReferencesInAssets = EditorPrefs.GetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_ASSETS, hideRedundantPrefabReferencesInAssets );
hideRedundantPrefabReferencesInScenes = EditorPrefs.GetBool( PREFS_HIDE_REDUNDANT_PREFAB_REFERENCES_IN_SCENES, hideRedundantPrefabReferencesInScenes );
showDetailedProgressBar = EditorPrefs.GetBool( PREFS_SHOW_PROGRESS, true );
}
@@ -502,24 +514,16 @@ namespace AssetUsageDetectorNamespace
GUILayout.Space( 10f );
Color c = GUI.backgroundColor;
GUI.backgroundColor = AssetUsageDetectorSettings.SettingsHeaderColor;
GUILayout.Box( "<b>SEARCH IN</b>", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
GUI.backgroundColor = c;
Utilities.DrawHeader( "<b>SEARCH IN</b>" );
searchInAssetsFolder = WordWrappingToggleLeft( "Project window (Assets folder)", searchInAssetsFolder );
if( searchInAssetsFolder )
{
GUILayout.BeginHorizontal();
GUILayout.Space( 35f );
GUILayout.BeginVertical();
BeginIndentedGUI();
searchInAssetsSubsetDrawer.Draw( searchInAssetsSubset );
excludedAssetsDrawer.Draw( excludedAssets );
GUILayout.EndVertical();
GUILayout.EndHorizontal();
EndIndentedGUI();
}
GUILayout.Space( 5f );
@@ -540,13 +544,10 @@ namespace AssetUsageDetectorNamespace
if( searchInScenesInBuild )
{
GUILayout.BeginHorizontal();
GUILayout.Space( 35f );
BeginIndentedGUI( false );
searchInScenesInBuildTickedOnly = EditorGUILayout.ToggleLeft( "Ticked only", searchInScenesInBuildTickedOnly, Utilities.GL_WIDTH_100 );
searchInScenesInBuildTickedOnly = !EditorGUILayout.ToggleLeft( "All", !searchInScenesInBuildTickedOnly, Utilities.GL_WIDTH_100 );
GUILayout.EndHorizontal();
EndIndentedGUI( false );
}
GUI.enabled = true;
@@ -554,14 +555,9 @@ namespace AssetUsageDetectorNamespace
searchInAllScenes = WordWrappingToggleLeft( "All scenes in the project", searchInAllScenes );
}
GUILayout.BeginHorizontal();
GUILayout.Space( 35f );
GUILayout.BeginVertical();
BeginIndentedGUI();
excludedScenesDrawer.Draw( excludedScenes );
GUILayout.EndVertical();
GUILayout.EndHorizontal();
EndIndentedGUI();
EditorGUI.BeginDisabledGroup( !searchInOpenScenes && !searchInScenesInBuild && !searchInAllScenes );
searchInSceneLightingSettings = WordWrappingToggleLeft( "Scene Lighting Settings (WARNING: This may change the active scene during search)", searchInSceneLightingSettings );
@@ -573,9 +569,7 @@ namespace AssetUsageDetectorNamespace
GUILayout.Space( 10f );
GUI.backgroundColor = AssetUsageDetectorSettings.SettingsHeaderColor;
GUILayout.Box( "<b>SETTINGS</b>", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
GUI.backgroundColor = c;
Utilities.DrawHeader( "<b>SETTINGS</b>" );
#if ASSET_USAGE_ADDRESSABLES
EditorGUI.BeginDisabledGroup( addressablesSupport );
@@ -587,10 +581,8 @@ namespace AssetUsageDetectorNamespace
#endif
calculateUnusedObjects = WordWrappingToggleLeft( "Calculate unused objects", calculateUnusedObjects );
hideDuplicateRows = WordWrappingToggleLeft( "Hide duplicate rows in search results", hideDuplicateRows );
#if UNITY_2018_3_OR_NEWER
hideNonOverriddenPrefabVariablesInAssets = WordWrappingToggleLeft( "Hide non-overridden prefab variables in assets (references found in non-overridden variables of prefab variants/nested prefabs will be hidden)", hideNonOverriddenPrefabVariablesInAssets );
hideNonOverriddenPrefabVariablesInScenes = WordWrappingToggleLeft( "Hide non-overridden prefab variables in scenes (references found in non-overridden variables of prefab instances will be hidden)", hideNonOverriddenPrefabVariablesInScenes );
#endif
hideRedundantPrefabReferencesInAssets = WordWrappingToggleLeft( hideRedundantPrefabReferencesInAssetsLabel, hideRedundantPrefabReferencesInAssets );
hideRedundantPrefabReferencesInScenes = WordWrappingToggleLeft( hideRedundantPrefabReferencesInScenesLabel, hideRedundantPrefabReferencesInScenes );
noAssetDatabaseChanges = WordWrappingToggleLeft( "I haven't modified any assets/scenes since the last search (faster search)", noAssetDatabaseChanges );
showDetailedProgressBar = WordWrappingToggleLeft( "Update search progress bar more often (cancelable search) (slower search)", showDetailedProgressBar );
@@ -664,10 +656,7 @@ namespace AssetUsageDetectorNamespace
private void DrawObjectsToSearchSection()
{
Color c = GUI.backgroundColor;
GUI.backgroundColor = AssetUsageDetectorSettings.SettingsHeaderColor;
GUILayout.Box( "<b>SEARCHED OBJECTS</b>", Utilities.BoxGUIStyle, Utilities.GL_EXPAND_WIDTH );
GUI.backgroundColor = c;
Utilities.DrawHeader( "<b>SEARCHED OBJECTS</b>" );
Rect searchedObjectsHeaderRect = GUILayoutUtility.GetLastRect();
searchedObjectsHeaderRect.x += 5f;
@@ -680,7 +669,31 @@ namespace AssetUsageDetectorNamespace
objectsToSearchDrawer.Draw( objectsToSearch );
}
private void BeginIndentedGUI( bool isVertical = true )
{
GUILayout.BeginHorizontal();
GUILayout.Space( 35f );
if( isVertical )
GUILayout.BeginVertical();
}
private void EndIndentedGUI( bool isVertical = true )
{
if( isVertical )
GUILayout.EndVertical();
GUILayout.EndHorizontal();
}
public static bool WordWrappingToggleLeft( string label, bool value )
{
sharedGUIContent.text = label;
sharedGUIContent.tooltip = null;
return WordWrappingToggleLeft( sharedGUIContent, value );
}
public static bool WordWrappingToggleLeft( GUIContent label, bool value )
{
GUILayout.BeginHorizontal();
bool result = EditorGUILayout.ToggleLeft( GUIContent.none, value, GL_WIDTH_12 );
@@ -730,8 +743,8 @@ namespace AssetUsageDetectorNamespace
#endif
calculateUnusedObjects = calculateUnusedObjects,
hideDuplicateRows = hideDuplicateRows,
hideNonOverriddenPrefabVariablesInAssets = hideNonOverriddenPrefabVariablesInAssets,
hideNonOverriddenPrefabVariablesInScenes = hideNonOverriddenPrefabVariablesInScenes,
hideRedundantPrefabReferencesInAssets = hideRedundantPrefabReferencesInAssets,
hideRedundantPrefabReferencesInScenes = hideRedundantPrefabReferencesInScenes && searchInAssetsFolder,
noAssetDatabaseChanges = noAssetDatabaseChanges,
showDetailedProgressBar = showDetailedProgressBar
} );
@@ -23,6 +23,7 @@ namespace AssetUsageDetectorNamespace
public SearchResultGroup.GroupType type;
public bool isExpanded;
public bool pendingSearch;
public int redundantReferences;
public SearchResultTreeViewState treeViewState;
public List<int> initialSerializedNodes;
@@ -576,6 +577,9 @@ namespace AssetUsageDetectorNamespace
public int NumberOfReferences { get { return references.Count; } }
public ReferenceNode this[int index] { get { return references[index]; } }
public int NumberOfRedundantReferences { get; internal set; }
public bool AnyReferencesFound { get { return NumberOfReferences > 0 || NumberOfRedundantReferences > 0; } }
public SearchResultGroup( string title, GroupType type, bool isExpanded = true, bool pendingSearch = false )
{
Title = title.StartsWith( "<b>" ) ? title : string.Concat( "<b>", title, "</b>" );
@@ -816,6 +820,7 @@ namespace AssetUsageDetectorNamespace
removedRows.Add( treeViewRows[i].id );
}
treeView.ClearSearch();
treeView.HideItems( removedRows );
} );
}
@@ -868,6 +873,9 @@ namespace AssetUsageDetectorNamespace
if( IsExpanded )
{
if( NumberOfRedundantReferences > 0 )
EditorGUILayout.HelpBox( string.Concat( NumberOfRedundantReferences.ToString(), " redundant prefab reference(s) were skipped. Disable \"Hide redundant prefab references in ", ( Type == GroupType.Scene || Type == GroupType.DontDestroyOnLoad ) ? "Scenes" : "Assets", "\" to see them." ), MessageType.Info );
if( PendingSearch )
GUILayout.Box( "Lazy Search: this scene potentially has some references, hit Refresh to find them", Utilities.BoxGUIStyle );
else if( references.Count == 0 )
@@ -1064,6 +1072,7 @@ namespace AssetUsageDetectorNamespace
type = Type,
isExpanded = IsExpanded,
pendingSearch = PendingSearch,
redundantReferences = NumberOfRedundantReferences,
treeViewState = treeViewState
};
@@ -1081,6 +1090,7 @@ namespace AssetUsageDetectorNamespace
// Deserialize this result group from the serialized data
internal void Deserialize( SearchResult.SerializableResultGroup serializedResultGroup, List<ReferenceNode> allNodes )
{
NumberOfRedundantReferences = serializedResultGroup.redundantReferences;
treeViewState = serializedResultGroup.treeViewState;
if( serializedResultGroup.initialSerializedNodes != null )
@@ -200,6 +200,13 @@ namespace AssetUsageDetectorNamespace
RefreshSelectedNodes( GetSelection() );
}
public void ClearSearch()
{
string prevSearchTerm = state.searchTerm;
state.searchTerm = "";
RefreshSearch( prevSearchTerm );
}
public void RefreshSearch( string prevSearchTerm )
{
bool wasSearchTermEmpty = string.IsNullOrEmpty( prevSearchTerm );
+107 -21
View File
@@ -51,6 +51,10 @@ namespace AssetUsageDetectorNamespace
private static MethodInfo screenFittedRectGetter;
#if UNITY_2018_3_OR_NEWER
private static readonly Func<Object, bool, bool> prefabHasAnyOverridesGetter = (Func<Object, bool, bool>) Delegate.CreateDelegate( typeof( Func<Object, bool, bool> ), typeof( PrefabUtility ).GetMethod( "HasObjectOverride", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static ) );
#endif
private static readonly HashSet<string> folderContentsSet = new HashSet<string>();
internal static readonly StringBuilder stringBuilder = new StringBuilder( 400 );
@@ -114,6 +118,97 @@ namespace AssetUsageDetectorNamespace
return obj is DefaultAsset && AssetDatabase.IsValidFolder( AssetDatabase.GetAssetPath( obj ) );
}
public static T GetPrefabParent<T>( this T obj ) where T : Object
{
#if UNITY_2018_3_OR_NEWER
return PrefabUtility.GetCorrespondingObjectFromSource( obj );
#else
return PrefabUtility.GetPrefabParent( obj ) as T;
#endif
}
public static bool HasAnyPrefabOverrides( this Object obj )
{
if( obj.GetPrefabParent() == null )
return false;
#if UNITY_2018_3_OR_NEWER
return prefabHasAnyOverridesGetter( obj, false );
#else
SerializedProperty iterator = new SerializedObject( obj ).GetIterator();
if( iterator.Next( true ) )
{
do
{
if( iterator.prefabOverride )
return true;
} while( iterator.NextVisible( false ) );
}
return false;
#endif
}
public static bool HasAnyPrefabOverrides( this GameObject gameObject )
{
if( gameObject.GetPrefabParent() == null )
return false;
#if UNITY_2018_3_OR_NEWER
if( !PrefabUtility.HasPrefabInstanceAnyOverrides( gameObject, false ) )
return false;
#endif
Transform rootTransform = gameObject.transform;
List<Component> components = new List<Component>( 8 );
Stack<Transform> stack = new Stack<Transform>( 8 );
stack.Push( rootTransform );
while( stack.Count > 0 )
{
Transform transform = stack.Pop();
Transform prefab = transform.GetPrefabParent();
if( prefab == null ) // GameObject is added as override
return true;
if( transform.childCount != prefab.childCount ) // Has some added/destroyed children as override
return true;
bool isRootTransform = ReferenceEquals( transform, rootTransform );
if( !isRootTransform && ( transform.gameObject as Object ).HasAnyPrefabOverrides() ) // GameObject's properties are modified (e.g. name or tag) (excluding root GameObject)
return true;
components.Clear();
transform.GetComponents( components );
foreach( Component component in components )
{
if( component == null )
continue;
if( component.GetPrefabParent() == null ) // Component is added as override
return true;
if( ( !isRootTransform || !( component is Transform ) ) && component.HasAnyPrefabOverrides() ) // Component is modified (excluding root Transform)
return true;
}
int componentCount = components.Count;
components.Clear();
prefab.GetComponents( components );
if( components.Count != componentCount ) // Has some destroyed components as override
return true;
for( int i = 0, childCount = transform.childCount; i < childCount; i++ )
{
Transform child = transform.GetChild( i );
if( child != null )
stack.Push( child );
}
}
return false;
}
// Returns an enumerator to iterate through all asset paths in the folder
public static IEnumerable<string> EnumerateFolderContents( Object folderAsset )
{
@@ -446,6 +541,15 @@ namespace AssetUsageDetectorNamespace
return stringBuilder.ToString();
}
// Draw header inside OnGUI
public static void DrawHeader( string label )
{
Color c = GUI.backgroundColor;
GUI.backgroundColor = AssetUsageDetectorSettings.SettingsHeaderColor;
GUILayout.Box( label, BoxGUIStyle, GL_EXPAND_WIDTH );
GUI.backgroundColor = c;
}
// Draw horizontal line inside OnGUI
public static void DrawSeparatorLine()
{
@@ -471,7 +575,7 @@ namespace AssetUsageDetectorNamespace
for( int i = 0; i < objectsToSearch.Count; i++ )
{
if( objectsToSearch[i].obj != null && !objectsToSearch[i].obj.Equals( null ) )
if( objectsToSearch[i].obj != null )
return false;
}
@@ -479,38 +583,20 @@ namespace AssetUsageDetectorNamespace
}
// Check if all the objects inside the list are null
public static bool IsEmpty( this List<Object> objects )
public static bool IsEmpty<T>( this List<T> objects ) where T : Object
{
if( objects == null )
return true;
for( int i = 0; i < objects.Count; i++ )
{
if( objects[i] != null && !objects[i].Equals( null ) )
if( objects[i] != 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;
}
// Returns true is str starts with prefix
public static bool StartsWithFast( this string str, string prefix )
{
+1 -1
View File
@@ -1,4 +1,4 @@
= Asset Usage Detector (v2.5.4) =
= Asset Usage Detector (v2.5.5) =
Documentation: https://github.com/yasirkula/UnityAssetUsageDetector
E-mail: yasirkula@gmail.com
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.assetusagedetector",
"displayName": "Asset Usage Detector",
"version": "2.5.4",
"version": "2.5.5",
"documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector",
"changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases",
"licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",