mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-19 14:32:37 +08:00
@@ -8,6 +8,11 @@ using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Object = UnityEngine.Object;
|
||||
#if UNITY_6000_3_OR_NEWER
|
||||
using EntityId = UnityEngine.EntityId;
|
||||
#else
|
||||
using EntityId = System.Int32;
|
||||
#endif
|
||||
|
||||
namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
@@ -76,7 +81,7 @@ namespace AssetUsageDetectorNamespace
|
||||
|
||||
// An optimization to search an object only once (key is a hash of the searched object)
|
||||
private readonly Dictionary<string, ReferenceNode> searchedObjects = new Dictionary<string, ReferenceNode>( 4096 );
|
||||
private readonly Dictionary<int, ReferenceNode> searchedUnityObjects = new Dictionary<int, ReferenceNode>( 32768 ); // Unity objects use their instanceIDs as key which is more performant
|
||||
private readonly Dictionary<EntityId, ReferenceNode> searchedUnityObjects = new(32768); // Unity objects use their EntityIds as key
|
||||
|
||||
// Stack of SearchObject function parameters to avoid infinite loops (which happens when same object is passed as parameter to function)
|
||||
private readonly List<object> callStack = new List<object>( 64 );
|
||||
@@ -330,9 +335,9 @@ namespace AssetUsageDetectorNamespace
|
||||
// Initialize data used by search functions
|
||||
InitializeSearchFunctionsData( searchParameters );
|
||||
|
||||
// Initialize the nodes of searched asset(s)
|
||||
foreach( Object obj in objectsToSearchSet )
|
||||
searchedUnityObjects.Add( obj.GetInstanceID(), PopReferenceNode( obj ) );
|
||||
// Initialize the nodes of searched asset(s)
|
||||
foreach (Object obj in objectsToSearchSet)
|
||||
searchedUnityObjects.Add(obj.GetEntityId(), PopReferenceNode(obj));
|
||||
|
||||
// Progressbar values
|
||||
int searchProgress = 0;
|
||||
@@ -660,8 +665,8 @@ namespace AssetUsageDetectorNamespace
|
||||
usedObjectPathsSet.Add( assetPath );
|
||||
else
|
||||
{
|
||||
for( Transform parent = ( (GameObject) obj ).transform.parent; parent != null; parent = parent.parent )
|
||||
usedObjectPathsSet.Add( parent.gameObject.GetInstanceID().ToString() );
|
||||
for (Transform parent = ((GameObject)obj).transform.parent; parent != null; parent = parent.parent)
|
||||
usedObjectPathsSet.Add(parent.gameObject.GetEntityId().ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -732,14 +737,14 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
if( !searchedTopmostGameObject )
|
||||
{
|
||||
if( obj is GameObject )
|
||||
unusedMainObjectNodes[obj.GetInstanceID().ToString()] = node;
|
||||
else
|
||||
currentSearchResultGroup.AddReference( node );
|
||||
if (obj is GameObject)
|
||||
unusedMainObjectNodes[obj.GetEntityId().ToString()] = node;
|
||||
else
|
||||
currentSearchResultGroup.AddReference(node);
|
||||
}
|
||||
else // List child GameObject scene objects under their parent GameObject
|
||||
{
|
||||
string dictionaryKey = searchedTopmostGameObject.GetInstanceID().ToString();
|
||||
string dictionaryKey = searchedTopmostGameObject.GetEntityId().ToString();
|
||||
List<ReferenceNode> unusedSubObjectNodesAtPath;
|
||||
if( !unusedSubObjectNodes.TryGetValue( dictionaryKey, out unusedSubObjectNodesAtPath ) )
|
||||
unusedSubObjectNodes[dictionaryKey] = unusedSubObjectNodesAtPath = new List<ReferenceNode>( 2 );
|
||||
@@ -1016,14 +1021,14 @@ namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
if( assetsToSearchSet.Count == 0 )
|
||||
{
|
||||
searchedUnityObjects.Add( unityObject.GetInstanceID(), null );
|
||||
searchedUnityObjects.Add(unityObject.GetEntityId(), null);
|
||||
return null;
|
||||
}
|
||||
|
||||
assetPath = AssetDatabase.GetAssetPath( unityObject );
|
||||
if( excludedAssetsPathsSet.Contains( assetPath ) || !AssetHasAnyReference( assetPath ) )
|
||||
{
|
||||
searchedUnityObjects.Add( unityObject.GetInstanceID(), null );
|
||||
searchedUnityObjects.Add(unityObject.GetEntityId(), null);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -1085,24 +1090,24 @@ namespace AssetUsageDetectorNamespace
|
||||
result = null;
|
||||
}
|
||||
|
||||
// Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null)
|
||||
// and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something
|
||||
// or we are at the root of the search; i.e. currentDepth == 0)
|
||||
if( !( obj is ValueType ) && ( result != null || unityObject != null || currentDepth == 0 ) )
|
||||
{
|
||||
if( !searchingSourceAsset )
|
||||
{
|
||||
if( obj is Object )
|
||||
searchedUnityObjects.Add( unityObject.GetInstanceID(), result );
|
||||
else
|
||||
searchedObjects.Add( GetNodeObjectHash( obj ), result );
|
||||
}
|
||||
else if( result != null )
|
||||
{
|
||||
result.CopyReferencesTo( searchedUnityObjects[unityObject.GetInstanceID()] );
|
||||
PoolReferenceNode( result );
|
||||
}
|
||||
}
|
||||
// Cache the search result if we are skimming through a class (not a struct; i.e. objHash != null)
|
||||
// and if the object is a UnityEngine.Object (if not, cache the result only if we have actually found something
|
||||
// or we are at the root of the search; i.e. currentDepth == 0)
|
||||
if (!(obj is ValueType) && (result != null || unityObject != null || currentDepth == 0))
|
||||
{
|
||||
if (!searchingSourceAsset)
|
||||
{
|
||||
if (obj is Object)
|
||||
searchedUnityObjects.Add(unityObject.GetEntityId(), result);
|
||||
else
|
||||
searchedObjects.Add(GetNodeObjectHash(obj), result);
|
||||
}
|
||||
else if (result != null)
|
||||
{
|
||||
result.CopyReferencesTo(searchedUnityObjects[unityObject.GetEntityId()]);
|
||||
PoolReferenceNode(result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1228,46 +1233,46 @@ namespace AssetUsageDetectorNamespace
|
||||
return false;
|
||||
}
|
||||
|
||||
// If object was already searched, return its ReferenceNode
|
||||
private bool TryGetReferenceNode( object nodeObject, out ReferenceNode referenceNode )
|
||||
{
|
||||
if( nodeObject is Object )
|
||||
{
|
||||
if( searchedUnityObjects.TryGetValue( ( (Object) nodeObject ).GetInstanceID(), out referenceNode ) )
|
||||
return true;
|
||||
}
|
||||
else if( searchedObjects.TryGetValue( GetNodeObjectHash( nodeObject ), out referenceNode ) )
|
||||
return true;
|
||||
// If object was already searched, return its ReferenceNode
|
||||
private bool TryGetReferenceNode(object nodeObject, out ReferenceNode referenceNode)
|
||||
{
|
||||
if (nodeObject is Object unityObject)
|
||||
{
|
||||
if (searchedUnityObjects.TryGetValue(unityObject.GetEntityId(), out referenceNode))
|
||||
return true;
|
||||
}
|
||||
else if (searchedObjects.TryGetValue(GetNodeObjectHash(nodeObject), out referenceNode))
|
||||
return true;
|
||||
|
||||
referenceNode = null;
|
||||
return false;
|
||||
}
|
||||
referenceNode = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get reference node for object
|
||||
private ReferenceNode GetReferenceNode( object nodeObject )
|
||||
{
|
||||
ReferenceNode result;
|
||||
if( nodeObject is Object )
|
||||
{
|
||||
int hash = ( (Object) nodeObject ).GetInstanceID();
|
||||
if( !searchedUnityObjects.TryGetValue( hash, out result ) || result == null )
|
||||
{
|
||||
result = PopReferenceNode( nodeObject );
|
||||
searchedUnityObjects[hash] = result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string hash = GetNodeObjectHash( nodeObject );
|
||||
if( !searchedObjects.TryGetValue( hash, out result ) || result == null )
|
||||
{
|
||||
result = PopReferenceNode( nodeObject );
|
||||
searchedObjects[hash] = result;
|
||||
}
|
||||
}
|
||||
// Get reference node for object
|
||||
private ReferenceNode GetReferenceNode(object nodeObject)
|
||||
{
|
||||
ReferenceNode result;
|
||||
if (nodeObject is Object unityObject)
|
||||
{
|
||||
EntityId hash = unityObject.GetEntityId();
|
||||
if (!searchedUnityObjects.TryGetValue(hash, out result) || result == null)
|
||||
{
|
||||
result = PopReferenceNode(nodeObject);
|
||||
searchedUnityObjects[hash] = result;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
string hash = GetNodeObjectHash(nodeObject);
|
||||
if (!searchedObjects.TryGetValue(hash, out result) || result == null)
|
||||
{
|
||||
result = PopReferenceNode(nodeObject);
|
||||
searchedObjects[hash] = result;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Fetch a reference node from pool
|
||||
private ReferenceNode PopReferenceNode( object nodeObject )
|
||||
|
||||
@@ -12,6 +12,7 @@ using UnityEngine.U2D;
|
||||
using UnityEngine.Playables;
|
||||
using UnityEditor.U2D;
|
||||
using UnityEditor.Compilation;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Tilemaps;
|
||||
#if ASSET_USAGE_ADDRESSABLES
|
||||
using UnityEngine.AddressableAssets;
|
||||
@@ -643,12 +644,12 @@ namespace AssetUsageDetectorNamespace
|
||||
if( searchTextureReferences && isInPlayMode && !AssetDatabase.Contains( material ) )
|
||||
{
|
||||
Shader shader = material.shader;
|
||||
int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader );
|
||||
int shaderPropertyCount = shader.GetPropertyCount();
|
||||
for( int i = 0; i < shaderPropertyCount; i++ )
|
||||
{
|
||||
if( ShaderUtil.GetPropertyType( shader, i ) == ShaderUtil.ShaderPropertyType.TexEnv )
|
||||
if (shader.GetPropertyType(i) == ShaderPropertyType.Texture)
|
||||
{
|
||||
string propertyName = ShaderUtil.GetPropertyName( shader, i );
|
||||
string propertyName = shader.GetPropertyName(i);
|
||||
Texture assignedTexture = material.GetTexture( propertyName );
|
||||
if( objectsToSearchSet.Contains( assignedTexture ) )
|
||||
{
|
||||
@@ -675,12 +676,12 @@ namespace AssetUsageDetectorNamespace
|
||||
ShaderImporter shaderImporter = AssetImporter.GetAtPath( AssetDatabase.GetAssetPath( shader ) ) as ShaderImporter;
|
||||
if( shaderImporter != null )
|
||||
{
|
||||
int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader );
|
||||
int shaderPropertyCount = shader.GetPropertyCount();
|
||||
for( int i = 0; i < shaderPropertyCount; i++ )
|
||||
{
|
||||
if( ShaderUtil.GetPropertyType( shader, i ) == ShaderUtil.ShaderPropertyType.TexEnv )
|
||||
if (shader.GetPropertyType(i) == ShaderPropertyType.Texture)
|
||||
{
|
||||
string propertyName = ShaderUtil.GetPropertyName( shader, i );
|
||||
string propertyName = shader.GetPropertyName(i);
|
||||
Texture defaultTexture = shaderImporter.GetDefaultTexture( propertyName );
|
||||
if( !defaultTexture )
|
||||
defaultTexture = shaderImporter.GetNonModifiableTexture( propertyName );
|
||||
|
||||
@@ -5,6 +5,7 @@ using System.Reflection;
|
||||
using UnityEditor;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace AssetUsageDetectorNamespace
|
||||
@@ -289,14 +290,14 @@ namespace AssetUsageDetectorNamespace
|
||||
List<string> nonModifiableTextureNames = new List<string>( 16 );
|
||||
List<Texture> nonModifiableTextureValues = new List<Texture>( 16 );
|
||||
|
||||
int shaderPropertyCount = ShaderUtil.GetPropertyCount( shader );
|
||||
int shaderPropertyCount = shader.GetPropertyCount();
|
||||
for( int i = 0; i < shaderPropertyCount; i++ )
|
||||
{
|
||||
if( ShaderUtil.GetPropertyType( shader, i ) != ShaderUtil.ShaderPropertyType.TexEnv )
|
||||
continue;
|
||||
if (shader.GetPropertyType(i) != ShaderPropertyType.Texture)
|
||||
continue;
|
||||
|
||||
string propertyName = ShaderUtil.GetPropertyName( shader, i );
|
||||
if( ShaderUtil.IsShaderPropertyNonModifiableTexureProperty( shader, i ) )
|
||||
string propertyName = shader.GetPropertyName(i);
|
||||
if ((shader.GetPropertyFlags(i) & ShaderPropertyFlags.NonModifiableTextureData) != 0)
|
||||
{
|
||||
Texture propertyDefaultValue = shaderImporter.GetNonModifiableTexture( propertyName );
|
||||
if( propertyDefaultValue == Value && propertyName == Variable )
|
||||
|
||||
@@ -9,6 +9,14 @@ using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
using Object = UnityEngine.Object;
|
||||
#if UNITY_6000_3_OR_NEWER
|
||||
using EntityId = UnityEngine.EntityId;
|
||||
#else
|
||||
using EntityId = System.Int32;
|
||||
#endif
|
||||
#if UNITY_6000_2_OR_NEWER
|
||||
using TreeViewItem = UnityEditor.IMGUI.Controls.TreeViewItem<int>;
|
||||
#endif
|
||||
|
||||
namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
@@ -39,7 +47,7 @@ namespace AssetUsageDetectorNamespace
|
||||
}
|
||||
|
||||
public string label;
|
||||
public int instanceId;
|
||||
public EntityId instanceId;
|
||||
public bool isUnityObject, isMainReference;
|
||||
public ReferenceNode.UsedState usedState;
|
||||
|
||||
@@ -1127,8 +1135,8 @@ namespace AssetUsageDetectorNamespace
|
||||
public bool IsMainReference { get; private set; } // True: if belongs to a scene search result group, then it's an object in that scene. If belongs to the assets search result group, then it's an asset
|
||||
|
||||
internal object nodeObject;
|
||||
private int? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise
|
||||
public Object UnityObject { get { return instanceId.HasValue ? EditorUtility.InstanceIDToObject( instanceId.Value ) : null; } }
|
||||
private EntityId? instanceId; // instanceId of the nodeObject if it is a Unity object, null otherwise
|
||||
public Object UnityObject => instanceId.HasValue ? Utilities.EntityIdToObject(instanceId.Value) : null;
|
||||
|
||||
private readonly List<Link> links = new List<Link>( 2 );
|
||||
public int NumberOfOutgoingLinks { get { return links.Count; } }
|
||||
@@ -1222,7 +1230,7 @@ namespace AssetUsageDetectorNamespace
|
||||
Object unityObject = nodeObject as Object;
|
||||
if( unityObject != null )
|
||||
{
|
||||
instanceId = unityObject.GetInstanceID();
|
||||
instanceId = unityObject.GetEntityId();
|
||||
Label = unityObject.name + " (" + unityObject.GetType().Name + ")";
|
||||
|
||||
if( AssetUsageDetectorSettings.ShowRootAssetName && unityObject.IsAsset() && !AssetDatabase.IsMainAsset( unityObject ) )
|
||||
|
||||
@@ -6,6 +6,11 @@ using UnityEditor;
|
||||
using UnityEditor.IMGUI.Controls;
|
||||
using UnityEditor.SceneManagement;
|
||||
using UnityEngine;
|
||||
#if UNITY_6000_2_OR_NEWER
|
||||
using TreeView = UnityEditor.IMGUI.Controls.TreeView<int>;
|
||||
using TreeViewItem = UnityEditor.IMGUI.Controls.TreeViewItem<int>;
|
||||
using TreeViewState = UnityEditor.IMGUI.Controls.TreeViewState<int>;
|
||||
#endif
|
||||
|
||||
namespace AssetUsageDetectorNamespace
|
||||
{
|
||||
|
||||
@@ -174,6 +174,28 @@ namespace AssetUsageDetectorNamespace
|
||||
return false;
|
||||
}
|
||||
|
||||
#if UNITY_6000_3_OR_NEWER
|
||||
public static EntityId GetEntityId(this Object obj)
|
||||
{
|
||||
return obj.GetEntityId();
|
||||
}
|
||||
|
||||
public static Object EntityIdToObject(EntityId entityId)
|
||||
{
|
||||
return EditorUtility.EntityIdToObject(entityId);
|
||||
}
|
||||
#else
|
||||
public static int GetEntityId(this Object obj)
|
||||
{
|
||||
return obj.GetInstanceID();
|
||||
}
|
||||
|
||||
public static Object EntityIdToObject(int instanceID)
|
||||
{
|
||||
return EditorUtility.InstanceIDToObject(instanceID);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Returns an enumerator to iterate through all asset paths in the folder
|
||||
public static IEnumerable<string> EnumerateFolderContents( Object folderAsset )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user