Having trouble figuring out the AssetDatabase.FindAssets search string

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.

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!

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.

Thanks Kurt, good to know. I’ll process the paths and asset names returned to get what I need.

1 Like

Here i just did it just match the names

T SearchItemInAssetdatabase<T>(string Name_, string Path_) where T : UnityEngine.Object
    {
        string[] guids2 = AssetDatabase.FindAssets("\"" + Name_ + "\"", new[] { Path_ });
        string GUIDTOPATH = "";
        for (int i = 0; i < guids2.Length; i++)
        {
            GUIDTOPATH = AssetDatabase.GUIDToAssetPath(guids2[i]);
            if (GUIDTOPATH.Split('/')[GUIDTOPATH.Split('/').Length - 1] == Name_)
            {
                break;
            }
        }
        Debug.Log(GUIDTOPATH);
        T ObjectFounded = AssetDatabase.LoadAssetAtPath<T>(GUIDTOPATH);
        return ObjectFounded;
    }

It can be done like this:

string fileName = "Settings.asset";
string searchFilter = $"glob:\"Assets/**/*{fileName}\"";
string[] guids = AssetDatabase.FindAssets(searchFilter);
string[] paths = guids.Select(AssetDatabase.GUIDToAssetPath).ToArray();
1 Like

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

1 Like