mirror of
https://github.com/yasirkula/UnityAssetUsageDetector.git
synced 2026-11-20 23:12:38 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b7b6f8815e | |||
| 40a28f5961 | |||
| b535e09ba2 | |||
| 499e4bc43e |
@@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
**Forum Thread:** https://forum.unity.com/threads/asset-usage-detector-find-references-to-an-asset-object-open-source.408134/
|
**Forum Thread:** https://forum.unity.com/threads/asset-usage-detector-find-references-to-an-asset-object-open-source.408134/
|
||||||
|
|
||||||
|
**Discord:** https://discord.gg/UJJt549AaV
|
||||||
|
|
||||||
**[Support the Developer ☕](https://yasirkula.itch.io/unity3d)**
|
**[Support the Developer ☕](https://yasirkula.itch.io/unity3d)**
|
||||||
|
|
||||||
## ABOUT
|
## ABOUT
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
// A set that contains the searched asset(s) and their sub-assets (if any)
|
// A set that contains the searched scene object(s), asset(s) and their sub-assets (if any)
|
||||||
private readonly HashSet<Object> objectsToSearchSet = new HashSet<Object>();
|
private readonly HashSet<Object> objectsToSearchSet = new HashSet<Object>();
|
||||||
// Scenes of scene object(s) in objectsToSearchSet
|
// Scenes of scene object(s) in objectsToSearchSet
|
||||||
private readonly HashSet<string> sceneObjectsToSearchScenesSet = new HashSet<string>();
|
private readonly HashSet<string> sceneObjectsToSearchScenesSet = new HashSet<string>();
|
||||||
@@ -610,7 +610,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
private void InitializeSearchResultNodes( List<SearchResultGroup> searchResult )
|
private void InitializeSearchResultNodes( List<SearchResultGroup> searchResult )
|
||||||
{
|
{
|
||||||
for( int i = 0; i < searchResult.Count; i++ )
|
for( int i = 0; i < searchResult.Count; i++ )
|
||||||
searchResult[i].InitializeNodes( GetReferenceNode );
|
searchResult[i].InitializeNodes( GetReferenceNode, objectsToSearchSet );
|
||||||
|
|
||||||
// If there are any empty groups after node initialization, remove those groups
|
// If there are any empty groups after node initialization, remove those groups
|
||||||
for( int i = searchResult.Count - 1; i >= 0; i-- )
|
for( int i = searchResult.Count - 1; i >= 0; i-- )
|
||||||
|
|||||||
@@ -383,65 +383,6 @@ 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 { }
|
|
||||||
|
|
||||||
#if UNITY_2017_1_OR_NEWER
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ParticleSystem.TextureSheetAnimationModule textureSheetAnimationModule = particleSystem.textureSheetAnimation;
|
|
||||||
for( int i = 0, j = textureSheetAnimationModule.spriteCount; i < j; i++ )
|
|
||||||
referenceNode.AddLinkTo( SearchObject( textureSheetAnimationModule.GetSprite( i ) ), "Texture Sheet Animation Module: Sprite" );
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
#if UNITY_5_5_OR_NEWER
|
|
||||||
try
|
|
||||||
{
|
|
||||||
ParticleSystem.SubEmittersModule subEmittersModule = particleSystem.subEmitters;
|
|
||||||
for( int i = 0, j = subEmittersModule.subEmittersCount; i < j; i++ )
|
|
||||||
referenceNode.AddLinkTo( SearchObject( subEmittersModule.GetSubEmitterSystem( i ) ), "Sub Emitters Module: ParticleSystem" );
|
|
||||||
}
|
|
||||||
catch { }
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
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,
|
||||||
@@ -1095,54 +1036,42 @@ namespace AssetUsageDetectorNamespace
|
|||||||
SerializedProperty iteratorVisible = so.GetIterator();
|
SerializedProperty iteratorVisible = so.GetIterator();
|
||||||
if( iterator.Next( true ) )
|
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 iteratingVisible = iteratorVisible.NextVisible( true );
|
||||||
bool enterChildren;
|
bool enterChildren;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
if( isProjectSettings )
|
// 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
|
||||||
// Search Project Settings in full because for some reason, important settings like SRP asset
|
// those properties aren't interesting and mostly confusing)
|
||||||
// slot in Quality Settings aren't searched when iterating over only visible properties
|
bool isVisible = iteratingVisible && SerializedProperty.EqualContents( iterator, iteratorVisible );
|
||||||
enterChildren = true;
|
if( isVisible )
|
||||||
}
|
iteratingVisible = iteratorVisible.NextVisible( iteratorVisible.propertyType == SerializedPropertyType.Generic );
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Iterate over NextVisible properties AND the properties that have corresponding FieldInfos (internal Unity
|
Type propFieldType;
|
||||||
// properties don't have FieldInfos so we are skipping them, which is good because search results found in
|
isVisible = iterator.type == "Array" || fieldInfoGetter( iterator, out propFieldType ) != null;
|
||||||
// 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 )
|
if( !isVisible )
|
||||||
{
|
{
|
||||||
enterChildren = false;
|
enterChildren = false;
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReferenceNode searchResult;
|
ReferenceNode searchResult;
|
||||||
switch( iterator.propertyType )
|
switch( iterator.propertyType )
|
||||||
{
|
{
|
||||||
case SerializedPropertyType.ObjectReference:
|
case SerializedPropertyType.ObjectReference:
|
||||||
searchResult = SearchObject( iterator.objectReferenceValue );
|
searchResult = SearchObject( PreferablyGameObject( iterator.objectReferenceValue ) );
|
||||||
enterChildren = false;
|
enterChildren = false;
|
||||||
break;
|
break;
|
||||||
case SerializedPropertyType.ExposedReference:
|
case SerializedPropertyType.ExposedReference:
|
||||||
searchResult = SearchObject( iterator.exposedReferenceValue );
|
searchResult = SearchObject( PreferablyGameObject( iterator.exposedReferenceValue ) );
|
||||||
enterChildren = false;
|
enterChildren = false;
|
||||||
break;
|
break;
|
||||||
#if UNITY_2019_3_OR_NEWER
|
#if UNITY_2019_3_OR_NEWER
|
||||||
case SerializedPropertyType.ManagedReference:
|
case SerializedPropertyType.ManagedReference:
|
||||||
searchResult = SearchObject( GetRawSerializedPropertyValue( iterator ) );
|
searchResult = SearchObject( PreferablyGameObject( GetRawSerializedPropertyValue( iterator ) ) );
|
||||||
enterChildren = false;
|
enterChildren = false;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
@@ -1195,7 +1124,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
// no need to have duplicate search entries
|
// no need to have duplicate search entries
|
||||||
if( !( variableValue is ICollection ) )
|
if( !( variableValue is ICollection ) )
|
||||||
{
|
{
|
||||||
ReferenceNode searchResult = SearchObject( variableValue );
|
ReferenceNode searchResult = SearchObject( PreferablyGameObject( variableValue ) );
|
||||||
if( searchResult != null && searchResult != referenceNode )
|
if( searchResult != null && searchResult != referenceNode )
|
||||||
referenceNode.AddLinkTo( searchResult, ( variables[i].isProperty ? "Property: " : "Variable: " ) + variables[i].name );
|
referenceNode.AddLinkTo( searchResult, ( variables[i].isProperty ? "Property: " : "Variable: " ) + variables[i].name );
|
||||||
}
|
}
|
||||||
@@ -1207,7 +1136,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
int index = 0;
|
int index = 0;
|
||||||
foreach( object element in (IEnumerable) variableValue )
|
foreach( object element in (IEnumerable) variableValue )
|
||||||
{
|
{
|
||||||
ReferenceNode searchResult = SearchObject( element );
|
ReferenceNode searchResult = SearchObject( PreferablyGameObject( element ) );
|
||||||
if( searchResult != null && searchResult != referenceNode )
|
if( searchResult != null && searchResult != referenceNode )
|
||||||
referenceNode.AddLinkTo( searchResult, string.Concat( variables[i].isProperty ? "Property: " : "Variable: ", variables[i].name, "[", index + "]" ) );
|
referenceNode.AddLinkTo( searchResult, string.Concat( variables[i].isProperty ? "Property: " : "Variable: ", variables[i].name, "[", index + "]" ) );
|
||||||
|
|
||||||
@@ -1472,5 +1401,12 @@ namespace AssetUsageDetectorNamespace
|
|||||||
valueAction( str.Substring( valueStartIndex, valueEndIndex - valueStartIndex ) );
|
valueAction( str.Substring( valueStartIndex, valueEndIndex - valueStartIndex ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If obj is Component, switches to its GameObject
|
||||||
|
private object PreferablyGameObject( object obj )
|
||||||
|
{
|
||||||
|
Component component = obj as Component;
|
||||||
|
return ( component != null && !component.Equals( null ) ) ? component.gameObject : obj;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -515,7 +515,7 @@ namespace AssetUsageDetectorNamespace
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initializes commonly used variables of the nodes
|
// Initializes commonly used variables of the nodes
|
||||||
public void InitializeNodes( Func<object, ReferenceNode> nodeGetter )
|
public void InitializeNodes( Func<object, ReferenceNode> nodeGetter, HashSet<Object> objectsToSearchSet )
|
||||||
{
|
{
|
||||||
// Remove root nodes that don't have any outgoing links or have null node objects (somehow)
|
// Remove root nodes that don't have any outgoing links or have null node objects (somehow)
|
||||||
for( int i = references.Count - 1; i >= 0; i-- )
|
for( int i = references.Count - 1; i >= 0; i-- )
|
||||||
@@ -544,6 +544,26 @@ namespace AssetUsageDetectorNamespace
|
|||||||
|
|
||||||
List<ReferenceNode> callStack = new List<ReferenceNode>( 8 );
|
List<ReferenceNode> callStack = new List<ReferenceNode>( 8 );
|
||||||
|
|
||||||
|
// If this SearchResultGroup belongs to a scene, simplify the root nodes so that they go directly to either the searched objects or to an asset in a single link
|
||||||
|
if( Type == GroupType.Scene || Type == GroupType.DontDestroyOnLoad )
|
||||||
|
{
|
||||||
|
Scene scene = references[0].nodeObject is GameObject ? ( (GameObject) references[0].nodeObject ).scene : ( (Component) references[0].nodeObject ).gameObject.scene;
|
||||||
|
HashSet<ReferenceNode> leafNodes = new HashSet<ReferenceNode>();
|
||||||
|
for( int i = references.Count - 1; i >= 0; i-- )
|
||||||
|
{
|
||||||
|
references[i].GetLeafSceneNodesRecursive( scene, leafNodes, references[i], objectsToSearchSet, callStack );
|
||||||
|
|
||||||
|
if( leafNodes.Count > 0 )
|
||||||
|
{
|
||||||
|
references.RemoveAt( i );
|
||||||
|
foreach( ReferenceNode leafNode in leafNodes )
|
||||||
|
references.Add( leafNode );
|
||||||
|
|
||||||
|
leafNodes.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// For simplicity's sake, get rid of root nodes that are already part of another node's hierarchy
|
// For simplicity's sake, get rid of root nodes that are already part of another node's hierarchy
|
||||||
for( int i = references.Count - 1; i >= 0; i-- )
|
for( int i = references.Count - 1; i >= 0; i-- )
|
||||||
{
|
{
|
||||||
@@ -1010,12 +1030,59 @@ namespace AssetUsageDetectorNamespace
|
|||||||
description = "<<destroyed>>";
|
description = "<<destroyed>>";
|
||||||
}
|
}
|
||||||
|
|
||||||
nodeObject = null; // don't hold Object reference, allow Unity to GC used memory
|
nodeObject = null; // Don't hold Object reference, allow Unity to GC used memory
|
||||||
|
|
||||||
for( int i = 0; i < links.Count; i++ )
|
for( int i = 0; i < links.Count; i++ )
|
||||||
links[i].targetNode.InitializeRecursively();
|
links[i].targetNode.InitializeRecursively();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Finds all leaf nodes that originate from this node and have GameObject/Component node objects in the specified scene
|
||||||
|
public void GetLeafSceneNodesRecursive( Scene scene, HashSet<ReferenceNode> leafNodes, ReferenceNode latestSceneObjectNode, HashSet<Object> objectsToSearchSet, List<ReferenceNode> callStack )
|
||||||
|
{
|
||||||
|
if( links.Count == 0 || callStack.ContainsFast( this ) || ( nodeObject is Object && objectsToSearchSet.Contains( (Object) nodeObject ) ) ) // This is a searched object's node
|
||||||
|
{
|
||||||
|
leafNodes.Add( latestSceneObjectNode );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object unityObject = nodeObject as Object;
|
||||||
|
if( unityObject != null )
|
||||||
|
{
|
||||||
|
if( AssetDatabase.Contains( unityObject ) )
|
||||||
|
{
|
||||||
|
leafNodes.Add( latestSceneObjectNode );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else if( unityObject is Component )
|
||||||
|
{
|
||||||
|
if( ( (Component) unityObject ).gameObject.scene == scene )
|
||||||
|
latestSceneObjectNode = this;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
leafNodes.Add( latestSceneObjectNode );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if( unityObject is GameObject )
|
||||||
|
{
|
||||||
|
if( ( (GameObject) unityObject ).scene == scene )
|
||||||
|
latestSceneObjectNode = this;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
leafNodes.Add( latestSceneObjectNode );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
callStack.Add( this );
|
||||||
|
|
||||||
|
for( int i = 0; i < links.Count; i++ )
|
||||||
|
links[i].targetNode.GetLeafSceneNodesRecursive( scene, leafNodes, latestSceneObjectNode, objectsToSearchSet, callStack );
|
||||||
|
|
||||||
|
callStack.RemoveAt( callStack.Count - 1 );
|
||||||
|
}
|
||||||
|
|
||||||
// Returns whether or not specified node is part of this node's siblings
|
// Returns whether or not specified node is part of this node's siblings
|
||||||
public bool NodeExistsInChildrenRecursive( ReferenceNode node, List<ReferenceNode> callStack )
|
public bool NodeExistsInChildrenRecursive( ReferenceNode node, List<ReferenceNode> callStack )
|
||||||
{
|
{
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "com.yasirkula.assetusagedetector",
|
"name": "com.yasirkula.assetusagedetector",
|
||||||
"displayName": "Asset Usage Detector",
|
"displayName": "Asset Usage Detector",
|
||||||
"version": "1.9.10",
|
"version": "2.0.0",
|
||||||
"documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector",
|
"documentationUrl": "https://github.com/yasirkula/UnityAssetUsageDetector",
|
||||||
"changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases",
|
"changelogUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/releases",
|
||||||
"licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",
|
"licensesUrl": "https://github.com/yasirkula/UnityAssetUsageDetector/blob/master/LICENSE.txt",
|
||||||
|
|||||||
Reference in New Issue
Block a user