Search Unity

Having trouble figuring out the AssetDatabase.FindAssets search string

Discussion in 'Asset Database' started by Darren-R, May 28, 2021.

  1. Darren-R

    Darren-R

    Joined:
    Feb 11, 2014
    Posts:
    66
    Hello!

    As the title implies. I'm having trouble figuring out the AssetDatabase.FindAssets search string.
    I have a bunch of assets with spaces in their names.
    I need to search for a specific asset with the exact name given.
    I keep getting assets that also contain the search string... how can I just get it to return an exact match!
    I've spent a long time googling but I am guessing there's some syntax/search term I don't know about and Unity docs are very vague.

    Code (CSharp):
    1. string[] guids1 = AssetDatabase.FindAssets("\""+searchStringWithSpaces+"\"", new[] {"Assets/0_DM/DATA/Objects"});
    I somehow figured out "\""+this.name+"\"" makes it see the string as one search item. How to return just an exact match.

    Cheers for any help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    Here's the only insight I have: it APPEARS that this method essentially does what the search field does in your project.

    This means you can search for
    t:scene
    and it will return all scenes.

    I don't believe there is a way to "search exact" from that search window, which might imply there isn't a way to do it with this function.
     
  3. Darren-R

    Darren-R

    Joined:
    Feb 11, 2014
    Posts:
    66
    Thanks Kurt, good to know. I'll process the paths and asset names returned to get what I need.
     
    Kurt-Dekker likes this.
  4. unknownbuddy

    unknownbuddy

    Joined:
    May 20, 2019
    Posts:
    2
    Here i just did it just match the names
    Code (CSharp):
    1.  
    2. T SearchItemInAssetdatabase<T>(string Name_, string Path_) where T : UnityEngine.Object
    3.     {
    4.         string[] guids2 = AssetDatabase.FindAssets("\"" + Name_ + "\"", new[] { Path_ });
    5.         string GUIDTOPATH = "";
    6.         for (int i = 0; i < guids2.Length; i++)
    7.         {
    8.             GUIDTOPATH = AssetDatabase.GUIDToAssetPath(guids2[i]);
    9.             if (GUIDTOPATH.Split('/')[GUIDTOPATH.Split('/').Length - 1] == Name_)
    10.             {
    11.                 break;
    12.             }
    13.         }
    14.         Debug.Log(GUIDTOPATH);
    15.         T ObjectFounded = AssetDatabase.LoadAssetAtPath<T>(GUIDTOPATH);
    16.         return ObjectFounded;
    17.     }
     
  5. Cubequad

    Cubequad

    Joined:
    Feb 23, 2019
    Posts:
    3
    It can be done like this:

    Code (CSharp):
    1. string fileName = "Settings.asset";
    2. string searchFilter = $"glob:\"Assets/**/*{fileName}\"";
    3. string[] guids = AssetDatabase.FindAssets(searchFilter);
    4. string[] paths = guids.Select(AssetDatabase.GUIDToAssetPath).ToArray();
     
    Unity_Javier likes this.
  6. Fangh

    Fangh

    Joined:
    Apr 19, 2013
    Posts:
    274
    AssetDatabase.FindAssets will find all objet that have "fileName" is their name, so your codedoes not work if you have "helloFile" and "helloFile2" it will find both
     
    malaron2 likes this.