public static string[] FindAssets (string filter);
public static string[] FindAssets (string filter, string[] searchInFolders);

参数

filter筛选器字符串可以包含搜索数据。有关此字符串的详细信息,请参阅下文。
searchInFolders要从中开始搜索的文件夹。

返回

string[] 匹配资源的数组。请注意将返回 GUID。

描述

使用搜索筛选器字符串搜索资源数据库。

FindAssets 允许您搜索资源。string 参数可 提供名称、标签或类型(类名称)。筛选器字符串可包含:\ \ **名称**:按照文件名(不带扩展名)筛选资源。由空格分隔的文字 被视为独立的名称搜索。因此,例如 "test asset" 是将要 搜索的资源的名称。请注意,name: 可用来标识资源。此外,可将 筛选器 string 中使用的名称指定为细分部分。例如,上述的 test asset 示例 可使用 test 进行匹配。\ \ **标签**:资源可与标签关联。在每个标签前面使用关键字“l:” 可查找带有特定标签的资源。这表明字符串正在 搜索标签。\ \ **类型**:基于显式标识的类型查找资源。关键字“t:”可用于 指定正在查找的资源类型。如果筛选器 string 包含 多个类型,那么将返回与一个类匹配的资源。类型 可以是 Texture2D 等内置类型,也可以是用户创建的脚本类。用户创建的 类是从项目中的 ScriptableObject 类中创建的资源。如果需要所有资源, 请在所有资源都派生自对象时使用 Object。使用 searchInFolders 参数 指定一个或多个文件夹时,会将搜索限定到这些文件夹及其 子文件夹。这要比在所有文件夹中搜索所有资源快。\ \ **注意:**搜索不区分大小写。\ \ 使用 AssetDatabase.GUIDToAssetPath 获取资源路径,并使用 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)); } } }

以下脚本示例显示了如何定位添加到资源的名称、 标签和类型细节。演示了 FindAssets 函数。该示例中 创建的资源使用的是 ScriptObj 类。

// 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;

public class ScriptObj : ScriptableObject { public void Awake() { Debug.Log("ScriptObj created"); } }

// Use ScriptObj to show how AssetDabase.FindAssets can be used

public 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() { 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)); } } }