AssetDatabase.FindAssets not working for packages

AssetDatabase.LoadAssetAtPath<Shader>("Packages/com.unity.probuilder/ProBuilder/Shader/DiffuseTextureBlend.shader");

Works fine.

AssetDatabase.FindAssets("", new string[] { "Packages/com.unity.probuilder/ProBuilder" });

Returns an empty array.

Hi @HG_SB ,

Seems to be a bug! What version of the editor are you using? Would you mind filling a bug report? (ref: Unity QA: Building quality with passion)

Thank you for reporting!

Pascal

Hi @okcompute_unity ,

I have reported the bug. Case: 1056753
I tested it with Unity 2018.1.2f1 and 2018.2.0b11

Sebastian

@okcompute_unity Hey, I got the same error in 2019.1 with packages that got installed via the Git integration of the Package manager. Should I still create a bug report or is this already known?

It can’t seem to find the files as they reside under the Library/PackageCache folder…

@spaceemotion Yes, please create a bug.

Thank you,

Pascal

1 Like

@okcompute_unity I had this problem as well, but in my case, it was because AssetDatabase.FindAssets didn’t understand backwards slashes, e.g.:
‘Packages/com.package.test****Runtime/Components/Popup’
I suppose I shouldn’t join folder paths using Path.Combine. Is it also a bug, or should I make sure to always use forward slashes?

Hi @Rallix ,

AssetDatabase.FindAssets is part of Unity API (not a .NET API). Unity file system expects forward slashes, not backward slashes. This is to to provide consistency across platforms. So, this is not a bug :slight_smile:

Regards,

Pascal

1 Like

Ok, thanks for the confirmation. :slight_smile:

I just ran into this on 2020.2.1f1 has this been fixed yet?

If you’re referring to this:
AssetDatabase.FindAssets("", string[ ] { "Packages/com.unity.probuilder/ProBuilder" });
AFAIK it’s only fixed in 2021.2.

If you want to search for assets across all packages, you can do this.
The example searches for materials

var paths = AssetDatabase.FindAssets("t:Material", new string[] { "Packages" });
1 Like

I believe the issue also applies to ScriptableObjects defined in Packages/, but present in Assets/. So, for instance, I have an embedded package with an SO defined at Packages/MyPackage/MyScriptableObject.cs. I create an instance of that SO in my Assets folder, say at Assets/Data/MyScriptableData.asset.

Now, I can try running the following:

var paths = AssetDatabase.FindAssets("t:MyScriptableObject", new string[] { "Assets/Data" });

…however, this returns paths.Length == 0. Similarly, AssetDatabase.LoadAssetAtPath returns null when I give it the exact path of the SO instance, which was defined in a package.

Okay, well I think I’ve figured out the cause of my particular problem. In my case, the package I had installed was originally installed via Git URL in the Package Manager; the ScriptableObject was created in this configuration.

However, I then uninstalled the Git URL version of the package, and installed it again via a repo cloned on my local disk. I guess this meant that somehow the ScriptableObject was hashed differently by Unity; subsequently, when looking for MyScriptableObjects in my project, Unity couldn’t find the type as defined in the package-on-disk (even though there was an asset present, as created by the package-from-Git).

So in my case it was due to changing the source of the package in Package Manager; if I create & search for the ScriptableObject using a package defined by a single, particular source, I don’t have the issue. My bad!