Search Unity

AssetDatabase.FindAssets not working for packages

Discussion in 'Asset Database' started by HG_SB, Jul 5, 2018.

  1. HG_SB

    HG_SB

    Joined:
    Dec 9, 2015
    Posts:
    39
    Code (CSharp):
    1. AssetDatabase.LoadAssetAtPath<Shader>("Packages/com.unity.probuilder/ProBuilder/Shader/DiffuseTextureBlend.shader");
    Works fine.

    Code (CSharp):
    1. AssetDatabase.FindAssets("", new string[] { "Packages/com.unity.probuilder/ProBuilder" });
    Returns an empty array.
     
  2. okcompute_unity

    okcompute_unity

    Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    756
  3. HG_SB

    HG_SB

    Joined:
    Dec 9, 2015
    Posts:
    39
    Hi @okcompute_unity,

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

    Sebastian
     
  4. spaceemotion

    spaceemotion

    Joined:
    Sep 29, 2015
    Posts:
    95
    @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...
     
  5. okcompute_unity

    okcompute_unity

    Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    756
    spaceemotion likes this.
  6. Rallix

    Rallix

    Joined:
    Mar 17, 2016
    Posts:
    139
    @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?
     
  7. okcompute_unity

    okcompute_unity

    Unity Technologies

    Joined:
    Jan 16, 2017
    Posts:
    756
    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 :)

    Regards,

    Pascal
     
    Rallix likes this.
  8. Rallix

    Rallix

    Joined:
    Mar 17, 2016
    Posts:
    139
    Ok, thanks for the confirmation. :)
     
  9. andysaia

    andysaia

    Joined:
    Nov 2, 2015
    Posts:
    21
    I just ran into this on 2020.2.1f1 has this been fixed yet?
     
  10. If you're referring to this:
    AssetDatabase.FindAssets("", string[] { "Packages/com.unity.probuilder/ProBuilder" });

    AFAIK it's only fixed in 2021.2.
     
  11. Dr-Nick

    Dr-Nick

    Joined:
    Sep 9, 2013
    Posts:
    25
    If you want to search for assets across all packages, you can do this.
    The example searches for materials

    Code (CSharp):
    1. var paths = AssetDatabase.FindAssets("t:Material", new string[] { "Packages" });
     
    tetto_green likes this.
  12. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    219
    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:
    Code (CSharp):
    1. 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.
     
  13. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    219
    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!