filter | The filter string can contain search data. See below for details about this string. |
searchInFolders | The folders where the search will start. |
string[] Array of matching asset. Note that GUIDs will be returned.
Search the asset database using the search filter string.
FindAssets allows you to search for Assets. The string
argument can
provide names, labels or types (classnames). The filter string can include:
Name: Filter assets by their filename (without extension). Words separated by whitespace
are treated as a separate name searches. So, for example "test asset"
, is a name of an Asset which will
be searched for. Note that name:
can be used to identify an asset. Further, the name
used in the filter string
can be specified as a subsection. For example the test asset
example
above can be matched using test
.
Labels: Assets can have labels attached to them. Assets with particular labels can be
found use the keyword 'l:' before each label. This indicates that the string is
searching for labels.
Types: Find assets based on explicitly identified types. The keyword 't:' is used as a
way to specify that typed assets are being looked for. If more than one type is included
in the filter string
then assets that match one class will be returned. Types can
either be builtin types such as Texture2D
or user created script classes. User created
classes are assets created from a ScriptableObject class in the project. If all assets are
wanted use Object
as all assets derive from Object. Specifying one or more folders
using the searchInFolders
argument will limit the searching to these folders and their
child folders. This is faster than searching all assets in all folders.
Note: Searching is case insensitive.
Use AssetDatabase.GUIDToAssetPath to get asset paths and AssetDatabase.LoadAssetAtPath
to load an asset.
@MenuItem("Test/FindAssetsUsingSearchFilter") static function FindAssetsUsingSearchFilter () { // Find all assets labelled with 'concrete' : var guids = AssetDatabase.FindAssets ("l:concrete", null); for (var guid in guids) Debug.Log (AssetDatabase.GUIDToAssetPath(guid)); // Find all Texture2Ds that have 'co' in their filename, that are labelled with 'concrete' or 'architecture' and are placed in 'MyAwesomeProps' folder var guids2 = AssetDatabase.FindAssets ("co l:concrete l:architecture t:texture2D", ["Assets/MyAwesomeProps"]); for (var guid in guids2) Debug.Log (AssetDatabase.GUIDToAssetPath(guid)); }
The following script example shows how the Names, Labels and Types details added
to Assets can be located. The FindAssets function is demonstrated. The
assets created in this example use the ScriptObj
class. The script example for
this class follows.
#pragma strict
public class Example {
static var testI: ScriptObj; static var testJ: ScriptObj; static var testK: ScriptObj; @MenuItem("Example/FindAssets Example") static function ExampleScript() { CreateAssets(); NamesExample(); LabelsExample(); TypesExample(); } static function CreateAssets() { testI = ScriptableObject.CreateInstance(ScriptObj) as ScriptObj; AssetDatabase.CreateAsset(testI, "Assets/AssetFolder/testI.asset"); testJ = ScriptableObject.CreateInstance(ScriptObj) as ScriptObj; AssetDatabase.CreateAsset(testJ, "Assets/AssetFolder/testJ.asset"); // create an asset in a sub-folder and with a name which contains a space testK = ScriptableObject.CreateInstance(ScriptObj) as ScriptObj; AssetDatabase.CreateAsset(testK, "Assets/AssetFolder/SpecialFolder/testK example.asset"); // an asset with a material will be used later var material: Material = new Material(Shader.Find("Standard")); AssetDatabase.CreateAsset(material, "Assets/AssetFolder/SpecialFolder/MyMaterial.mat"); } static function NamesExample() { Debug.Log("*** FINDING ASSETS BY NAME ***"); var results: String[]; results = AssetDatabase.FindAssets("testI"); for (var guid: String in results) { Debug.Log("testI: " + AssetDatabase.GUIDToAssetPath(guid)); } results = AssetDatabase.FindAssets("testJ"); for (var guid: String in results) { Debug.Log("testJ: " + AssetDatabase.GUIDToAssetPath(guid)); } results = AssetDatabase.FindAssets("testK example"); for (var guid: String 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"); for (var guid: String in results) { Debug.Log("name:test - " + AssetDatabase.GUIDToAssetPath(guid)); } } static function LabelsExample() { Debug.Log("*** FINDING ASSETS BY LABELS ***"); var setLabels: String[]; setLabels = ["wrapper"]; AssetDatabase.SetLabels(testI, setLabels); setLabels = ["bottle", "banana", "carrot"]; AssetDatabase.SetLabels(testJ, setLabels); setLabels = ["swappable", "helmet"]; AssetDatabase.SetLabels(testK, setLabels); // testJ has bottle, so have a label searched as 'bot' var getGuids: String[] = AssetDatabase.FindAssets("l:app l:bot"); for (var guid: String in getGuids) { Debug.Log("label lookup: " + AssetDatabase.GUIDToAssetPath(guid)); } } static function TypesExample() { Debug.Log("*** FINDING ASSETS BY TYPE ***"); var guids: String[]; // search for a ScriptObject called ScriptObj guids = AssetDatabase.FindAssets("t:ScriptObj"); for (var guid: String in guids) { Debug.Log("ScriptObj: " + AssetDatabase.GUIDToAssetPath(guid)); } guids = AssetDatabase.FindAssets("t:ScriptObj l:helmet"); for (var guid: String in guids) { Debug.Log("ScriptObj+bottle: " + AssetDatabase.GUIDToAssetPath(guid)); } } }
using UnityEngine; using UnityEditor;
public class Example {
static ScriptObj testI; static ScriptObj testJ; static ScriptObj testK; [MenuItem("Example/FindAssets Example")] static void ExampleScript( ) { CreateAssets(); NamesExample(); LabelsExample(); TypesExample(); }
static void CreateAssets() { 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; // search for a ScriptObject called ScriptObj guids = AssetDatabase.FindAssets("t:ScriptObj"); foreach (string guid in guids) { Debug.Log ("ScriptObj: " + AssetDatabase.GUIDToAssetPath(guid)); }
guids = AssetDatabase.FindAssets("t:ScriptObj l:helmet"); foreach (string guid in guids) { Debug.Log ("ScriptObj+bottle: " + AssetDatabase.GUIDToAssetPath(guid)); } } }
The following script is a simple class based on ScriptableObject. It is used by the script example above which needs a user provided class.
#pragma strict
public class ScriptObjJS extends ScriptableObject { var f: float[]; public function Awake() { var count: int = Random.Range(1000, 10000); f = new float[count]; for (var i: int = 0; i < count; i++) { f[i] = Random.Range(-1.0f, 1.0f); } } }
using UnityEngine;
public class ScriptObj : ScriptableObject {
float[] f;
public void Awake() { int count = Random.Range(1000, 10000); f = new float[count]; for (int i=0; i < count; i++) { f[i] = Random.Range(-1.0f, 1.0f); } } }