Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


FindAssets

Searches the Assets and Packages folders with a filter string, and returns the GUIDs, as strings, of matching assets.
Read time 6 minutesLast updated 3 days ago

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor.CoreModule

FindAssets(string)

Searches the Assets and Packages folders with a filter string, and returns the GUIDs, as strings, of matching assets.
public static string[] FindAssets(string filter)

Parameters

filter

The search filter string. For the syntax, refer to the description.

Returns

Type

Description

string[]An array that contains the GUIDs of the matching assets as strings, or an empty array if no assets match.

Remarks

This overload searches the
Assets
and
Packages
folders. To search specific folders, use the overload that takes
searchInFolders
.
The filter string can include the following:
  • Name: Filters assets by their file name, without the extension. Words separated by a space are separate name searches. For example,
    "test asset"
    searches for names that match
    test
    and names that match
    asset
    . Partial matches work too. For example,
    awesome
    matches
    MyAwesomeTexture
    .
  • Labels (l:): Filters assets by label. Use the
    l:
    keyword before each label.
  • Types (t:): Filters assets by type. Use the
    t:
    keyword before each type. If the filter includes more than one type, the search returns assets that match any of them. Types can be built-in types such as
    Texture2D
    , or ScriptableObject classes in the project. To find all assets, use
    t:Object
    , because all assets derive from
    Object
    .
  • AssetBundles (b:): Finds assets that are part of an AssetBundle. Use the
    b:
    keyword.
  • Area (a:): Finds assets in a specific area of the project. Valid values are
    all
    ,
    assets
    , and
    packages
    . Use the
    a:
    keyword to make the search more specific and faster.
  • Globbing (glob:): Matches paths with glob patterns. For example,
    glob:Editor
    finds all Editor folders,
    glob:(Editor|Resources)
    finds all Editor and Resources folders,
    glob:Editor/*
    finds all assets directly inside Editor folders, and
    glob:Editor/**
    finds all assets inside Editor folders recursively.
Searches aren't case-sensitive.
To get the path of an asset from its GUID, use AssetDatabase.GUIDToAssetPath. To load an asset from its path, use AssetDatabase.LoadAssetAtPath.
using UnityEngine;using UnityEditor;public class Example{ [MenuItem("Example/FindAssets Example")] static void ExampleScript() { // Find all assets labelled with 'architecture' : string[] guids1 = AssetDatabase.FindAssets("l:architecture", null); foreach (string guid1 in guids1) { Debug.Log(AssetDatabase.GUIDToAssetPath(guid1)); } // Find all Texture2Ds that have 'co' in their filename, that are labelled with 'architecture' and are placed in 'MyAwesomeProps' folder string[] guids2 = AssetDatabase.FindAssets("co l:architecture t:texture2D", new[] {"Assets/MyAwesomeProps"}); foreach (string guid2 in guids2) { Debug.Log(AssetDatabase.GUIDToAssetPath(guid2)); } }}
The following script example shows how the Names, Labels and Types details added to Assets can be located. The AssetDatabase.FindAssets function is demonstrated. The assets created in this example use the
ScriptObj
class.
// This script file has two CS classes. The first is a simple Unity ScriptableObject script.// The class it defines is used by the Example class below.// (This is a single Unity script file. You could split this file into a ScriptObj.cs and an// Example.cs file which is more structured.)using UnityEngine;using UnityEditor;using System.IO;public class ScriptObj : ScriptableObject{ public void Awake() { Debug.Log("ScriptObj created"); }}// Use ScriptObj to show how AssetDabase.FindAssets can be usedpublic class Example{ static ScriptObj testI; static ScriptObj testJ; static ScriptObj testK; [MenuItem("Examples/FindAssets Example two")] static void ExampleScript() { CreateAssets(); NamesExample(); LabelsExample(); TypesExample(); } static void CreateAssets() { if (!Directory.Exists("Assets/AssetFolder")) { AssetDatabase.CreateFolder("Assets", "AssetFolder"); } if (!Directory.Exists("Assets/AssetFolder/SpecialFolder")) { AssetDatabase.CreateFolder("Assets/AssetFolder", "SpecialFolder"); } testI = (ScriptObj)ScriptableObject.CreateInstance(typeof(ScriptObj)); AssetDatabase.CreateAsset(testI, "Assets/AssetFolder/testI.asset"); testJ = (ScriptObj)ScriptableObject.CreateInstance(typeof(ScriptObj)); AssetDatabase.CreateAsset(testJ, "Assets/AssetFolder/testJ.asset"); // create an asset in a sub-folder and with a name which contains a space testK = (ScriptObj)ScriptableObject.CreateInstance(typeof(ScriptObj)); AssetDatabase.CreateAsset(testK, "Assets/AssetFolder/SpecialFolder/testK example.asset"); // an asset with a material will be used later Material material = new Material(Shader.Find("Standard")); AssetDatabase.CreateAsset(material, "Assets/AssetFolder/SpecialFolder/MyMaterial.mat"); } static void NamesExample() { Debug.Log("*** FINDING ASSETS BY NAME ***"); string[] results; results = AssetDatabase.FindAssets("testI"); foreach (string guid in results) { Debug.Log("testI: " + AssetDatabase.GUIDToAssetPath(guid)); } results = AssetDatabase.FindAssets("testJ"); foreach (string guid in results) { Debug.Log("testJ: " + AssetDatabase.GUIDToAssetPath(guid)); } results = AssetDatabase.FindAssets("testK example"); foreach (string guid in results) { Debug.Log("testK example: " + AssetDatabase.GUIDToAssetPath(guid)); } Debug.Log("*** More complex asset search ***"); // find all assets that contain test (which is all assets) results = AssetDatabase.FindAssets("test"); foreach (string guid in results) { Debug.Log("name:test - " + AssetDatabase.GUIDToAssetPath(guid)); } } static void LabelsExample() { Debug.Log("*** FINDING ASSETS BY LABELS ***"); string[] setLabels; setLabels = new string[] { "wrapper" }; AssetDatabase.SetLabels(testI, setLabels); setLabels = new string[] { "bottle", "banana", "carrot" }; AssetDatabase.SetLabels(testJ, setLabels); setLabels = new string[] { "swappable", "helmet" }; AssetDatabase.SetLabels(testK, setLabels); // label searching: // testI has wrapper, testK has swappable, so both have 'app' // testJ has bottle, so have a label searched as 'bot' string[] getGuids = AssetDatabase.FindAssets("l:app l:bot"); foreach (string guid in getGuids) { Debug.Log("label lookup: " + AssetDatabase.GUIDToAssetPath(guid)); } } static void TypesExample() { Debug.Log("*** FINDING ASSETS BY TYPE ***"); string[] guids; guids = AssetDatabase.FindAssets("t:material"); foreach (string guid in guids) { Debug.Log("Material: " + AssetDatabase.GUIDToAssetPath(guid)); } guids = AssetDatabase.FindAssets("t:Object l:helmet"); foreach (string guid in guids) { Debug.Log("ScriptObj+helmet: " + AssetDatabase.GUIDToAssetPath(guid)); } }}

FindAssets(string, string[])

Searches specific folders with a filter string, and returns the GUIDs, as strings, of matching assets.
public static string[] FindAssets(string filter, string[] searchInFolders)

Parameters

filter

The search filter string. For the syntax, refer to the description.

searchInFolders

The folders to search in. Unity searches these folders and their subfolders.

Returns

Type

Description

string[]An array that contains the GUIDs of the matching assets as strings, or an empty array if no assets match.

Remarks

This overload searches only
searchInFolders
and their subfolders. Searching specific folders is faster than searching the whole project.
The filter string can include the following:
  • Name: Filters assets by their file name, without the extension. Words separated by a space are separate name searches. For example,
    "test asset"
    searches for names that match
    test
    and names that match
    asset
    . Partial matches work too. For example,
    awesome
    matches
    MyAwesomeTexture
    .
  • Labels (l:): Filters assets by label. Use the
    l:
    keyword before each label.
  • Types (t:): Filters assets by type. Use the
    t:
    keyword before each type. If the filter includes more than one type, the search returns assets that match any of them. Types can be built-in types such as
    Texture2D
    , or ScriptableObject classes in the project. To find all assets, use
    t:Object
    , because all assets derive from
    Object
    .
  • AssetBundles (b:): Finds assets that are part of an AssetBundle. Use the
    b:
    keyword.
  • Area (a:): Finds assets in a specific area of the project. Valid values are
    all
    ,
    assets
    , and
    packages
    . Use the
    a:
    keyword to make the search more specific and faster.
  • Globbing (glob:): Matches paths with glob patterns. For example,
    glob:Editor
    finds all Editor folders,
    glob:(Editor|Resources)
    finds all Editor and Resources folders,
    glob:Editor/*
    finds all assets directly inside Editor folders, and
    glob:Editor/**
    finds all assets inside Editor folders recursively.
Searches aren't case-sensitive.
To get the path of an asset from its GUID, use AssetDatabase.GUIDToAssetPath. To load an asset from its path, use AssetDatabase.LoadAssetAtPath.