yasirkula / UnityAssetUsageDetector

Find usages of the selected asset(s) and/or Object(s) in your Unity project, i.e. list the objects that refer to them
MIT License
1.73k stars 118 forks source link

Does not find Addressable asset references #29

Closed NamelessPerson closed 2 years ago

NamelessPerson commented 3 years ago

If a prefab is marked addressable and is referenced in the project using an AssetReference field, that reference does not show up in searches.

yasirkula commented 3 years ago

I've created a test scene where I've assigned an addressable material to a public AssetReferenceT<Material> field in my script. Unfortunately, right clicking that scene and selecting Select Dependencies doesn't show the addressable material as reference. "Select Dependencies" is the biggest optimization that Asset Usage Detector benefits from because it tells us which assets should be searched for references and which assets are safe to skip (usually, at least 90% of the project isn't searched thanks to this optimization).

I've briefly searched for possible solutions that don't require entirely disabling this optimization but couldn't find any. Unfortunately, Asset Usage Detector must search every single asset and scene in the project which will take significantly longer to complete. To make things worse, SerializedObjects don't work with AssetReferences so an alternative solution must be found.

I may address this issue at a later time but it will probably stay open for a while.

NamelessPerson commented 3 years ago

Thanks for the response! That makes sense but unfortunately means the tool is unusable in many cases for us, and it has been a huge benefit to earlier projects! Not sure how bad disabling that optimization would end up being, but it would likely still be worthwhile to us as an optional feature in order to be able to reliably clean up unused prefabs/SOs/etc.

yasirkula commented 3 years ago

Disabling that optimization alone won't suffice. Not being able to use SerializedObjects is a big deal because using reflection at edit time can cause side effects (example). I'm guessing that we still need to rely on SerializedObjects but for each iterated SerializedProperty, check if the property's type is an asset reference and use reflection or Addressables APIs for that variable.

therobby3 commented 2 years ago

Just making a post here as well. I've been using Asset Usage Detector for a few years now and it's great! Unfortunately, I really need it to work with Addressables though. =/ Would be great if a way could somehow be found to add it.

yasirkula commented 2 years ago

Could you import the unitypackage inside this zip archive to your project, enable "Addressables support" in Asset Usage Detector window and see how it goes? I've tried it with a few AssetReferences and didn't encounter any issues. Note that the search may take significantly longer to finish since the "Select Dependencies" optimization I've mentioned before is disabled for Addressables support.

therobby3 commented 2 years ago

Thanks much for the reply. Unfortunately though, when I enable "Addressables support" the search throws errors. The errors are below:

1st error:

Invalid iteration - (You need to call Next (true) on the first element to get to the first element) Invalid iteration - (You need to call Next (true) on the first element to get to the first element) UnityEditor.SerializedProperty:Next (bool) AssetUsageDetectorNamespace.AssetUsageDetector:SearchVariablesWithSerializedObject (AssetUsageDetectorNamespace.ReferenceNode) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs:1367) AssetUsageDetectorNamespace.AssetUsageDetector:SearchObject (object) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:1040) AssetUsageDetectorNamespace.AssetUsageDetector:BeginSearchObject (UnityEngine.Object) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:977) AssetUsageDetectorNamespace.AssetUsageDetector:Run (AssetUsageDetectorNamespace.AssetUsageDetector/Parameters) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:448) AssetUsageDetectorNamespace.AssetUsageDetectorWindow:InitiateSearch () (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs:677) AssetUsageDetectorNamespace.AssetUsageDetectorWindow:OnGUI () (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs:577) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

2nd error:

InvalidCastException: Specified cast is not valid. AssetUsageDetectorNamespace.AssetUsageDetector.GetAddressablesAssetReferenceValue (UnityEngine.AddressableAssets.AssetReference assetReference) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs:1787) AssetUsageDetectorNamespace.AssetUsageDetector.SearchVariablesWithSerializedObject (AssetUsageDetectorNamespace.ReferenceNode referenceNode) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs:1417) AssetUsageDetectorNamespace.AssetUsageDetector.SearchComponent (UnityEngine.Object unityObject) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs:608) AssetUsageDetectorNamespace.AssetUsageDetector.SearchObject (System.Object obj) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:1036) AssetUsageDetectorNamespace.AssetUsageDetector.SearchGameObject (UnityEngine.Object unityObject) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs:394) AssetUsageDetectorNamespace.AssetUsageDetector.SearchObject (System.Object obj) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:1034) AssetUsageDetectorNamespace.AssetUsageDetector.BeginSearchObject (UnityEngine.Object obj) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:977) AssetUsageDetectorNamespace.AssetUsageDetector.Run (AssetUsageDetectorNamespace.AssetUsageDetector+Parameters searchParameters) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:448) UnityEngine.Debug:LogException(Exception, Object) AssetUsageDetectorNamespace.AssetUsageDetector:Run(Parameters) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:592) AssetUsageDetectorNamespace.AssetUsageDetectorWindow:InitiateSearch() (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs:677) AssetUsageDetectorNamespace.AssetUsageDetectorWindow:OnGUI() (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs:577) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

Thanks, Robby

On Sun, Mar 13, 2022 at 11:50 AM Süleyman Yasir KULA < @.***> wrote:

Could you import the unitypackage inside this zip archive https://github.com/yasirkula/UnityAssetUsageDetector/files/8239979/AssetUsageDetector.zip to your project, enable "Addressables support" in Asset Usage Detector window and see how it goes? I've tried it with a few AssetReferences and didn't encounter any issues. Note that the search may take significantly longer to finish since the "Select Dependencies" optimization I've mentioned before is disabled for Addressables support.

— Reply to this email directly, view it on GitHub https://github.com/yasirkula/UnityAssetUsageDetector/issues/29#issuecomment-1066129053, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYEHOK65MPDR6JNLQHWB6CTU7YFFBANCNFSM5HJY6G6A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

yasirkula commented 2 years ago

Thank you for your valuable feedback!

I think the first error is harmless and caused by a certain asset type (perhaps TextAsset), I may have encountered it myself in the past, as well. If you could add Debug.Log( "Searching: " + (Object) referenceNode.nodeObject, (Object) referenceNode.nodeObject ); above the if condition at line 1367 and check which asset throws this error, that'd be much appreciated.

As for the second issue, may I ask your Addressables package version?

therobby3 commented 2 years ago

Thanks for the reply! Sorry for the delay. Sure thing, I added the line of code that you said, and I have some information, although a little odd.

So it looks like the assets throwing that error are all materials. Mostly all of them are materials I've downloaded for some visual effects in my game. They are nothing complicated, just simple materials. This is the output I get on them when adding the debug line your mentioned:

Searching: Deprecated EditorExtensionImpl (UnityEngine.EditorExtensionImpl) UnityEngine.Debug:Log (object,UnityEngine.Object) AssetUsageDetectorNamespace.AssetUsageDetector:SearchVariablesWithSerializedObject (AssetUsageDetectorNamespace.ReferenceNode) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorSearchFunctions.cs:1367) AssetUsageDetectorNamespace.AssetUsageDetector:SearchObject (object) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:1040) AssetUsageDetectorNamespace.AssetUsageDetector:BeginSearchObject (UnityEngine.Object) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:977) AssetUsageDetectorNamespace.AssetUsageDetector:Run (AssetUsageDetectorNamespace.AssetUsageDetector/Parameters) (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetector.cs:448) AssetUsageDetectorNamespace.AssetUsageDetectorWindow:InitiateSearch () (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs:677) AssetUsageDetectorNamespace.AssetUsageDetectorWindow:OnGUI () (at Assets/Plugins/AssetUsageDetector/Editor/AssetUsageDetectorWindow.cs:577) UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)

I did some quick googling and I'm really not sure what the heck EditorExtensionImpl is. The materials just look like normal materials. I hope that helps, though I'm not really sure why or what that thing is about.

As for the second error, my addressables version is the newest current one, which is 1.19.19.

Thanks, Robby

On Mon, Mar 21, 2022 at 12:31 PM Süleyman Yasir KULA < @.***> wrote:

Thank you for your valuable feedback!

I think the first error is harmless and caused by a certain asset type (perhaps TextAsset), I may have encountered it myself in the past, as well. If you could add Debug.Log( "Searching: " + (Object) referenceNode.nodeObject, (Object) referenceNode.nodeObject ); above the if condition at line 1367 and check which asset throws this error, that'd be much appreciated.

As for the second issue, may I ask your Addressables package version?

— Reply to this email directly, view it on GitHub https://github.com/yasirkula/UnityAssetUsageDetector/issues/29#issuecomment-1074128608, or unsubscribe https://github.com/notifications/unsubscribe-auth/AYEHOK6QGRSTJTIX2TMJSNTVBCP65ANCNFSM5HJY6G6A . You are receiving this because you commented.Message ID: @.***>

sTsUA commented 2 years ago

Hi there, using unity 2020.3.4f1 and addressables v1.19.14, had same errors. All can be fixed in AssetUsageDetectorSearchFunctions.cs


First error not affect anithing just raise the error log, but can be fixed in method SearchVariablesWithSerializedObject add inside block: if( !isInPlayMode || referenceNode.nodeObject.IsAsset() )

line of code

//Some veird deprecated object
if (((Object) referenceNode.nodeObject).name.Equals("Deprecated EditorExtensionImpl"))
{
    return;
}

to ignore that error, probably reference on some non used unity editor code


Second error is just a type cant cast to FieldInfo containing string can be fixed with change to PropertyInfo:

//change this line
private FieldInfo assetReferenceSubObjectTypeGetter;

//to PropertyInfo 
private PropertyInfo assetReferenceSubObjectTypeGetter;

and

//Change this assignment
assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetField( "m_SubObjectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );

//To property assignment returning Type
assetReferenceSubObjectTypeGetter = typeof( AssetReference ).GetProperty( "SubOjbectType", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance );

And now it works like a charm, thanks for your work :blush:

yasirkula commented 2 years ago

@therobby3 @sTsUA I'll investigate both of your valuable feedbacks when I get the chance, thank you 😺

yasirkula commented 2 years ago

Thank you both for your feedbacks ^^