Asset List Attribute特性:用于列表和數(shù)組以及Unity type的單個(gè)元素,并將默認(rèn)列表Drop替換為具有指定過濾器的所有可能資產(chǎn)的列表。使用此選項(xiàng)可以過濾并在列表或數(shù)組中包含或排除資產(chǎn),而無需導(dǎo)航項(xiàng)目窗口。
AssetList:創(chuàng)建一個(gè)指定類型的列表

[AssetList]
[PreviewField(70, ObjectFieldAlignment.Center)]
public Texture2D SingleObject;
【Path】根據(jù)指定篩選資源的路徑

[AssetList(Path = "Plugins/Sirenix/")]
public UnityEngine.Object Object;
如果是List,將以列表的形式表示出來,但是只有在選中時(shí),才會(huì)被添加到List中

[AssetList(Path = "/Plugins/Sirenix/")]
public List<ScriptableObject> AssetList;
【AutoPopulate】符合規(guī)則的自動(dòng)添加到List中

[AssetList(AutoPopulate = true)]//設(shè)置為true則自動(dòng)填充符合規(guī)則的資源,false為只顯示不填充
public List<MeshRenderer> AutoPopulatedWhenInspected;
【LayerNames】也可以指定layer層過濾

[AssetList(LayerNames = "MyLayerName")]//
public GameObject[] AllPrefabsWithLayerName;
【AssetNamePrefix】以資源名稱的前綴作為篩選條件

[AssetList(AssetNamePrefix = "前綴")]
[FoldoutGroup("過濾后的AssetLists")]
public List<GameObject> PrefabsStartingWithPrefix;
【Tags】以資源的tag作為篩選條件,可用,符號(hào)分隔多個(gè)Tag

[AssetList(Tags = "TagA,TagB",Path = "/TutorialAsset")]
public List<GameObject> GameObjectsWithTag;
【CustomFilterMethod】自定義過濾函數(shù)

[AssetList(CustomFilterMethod = "HasRigidbodyComponent")]
public List<GameObject> MyRigidbodyPrefabs;
private bool HasRigidbodyComponent(GameObject obj)
{
return obj.GetComponent<Rigidbody>() != null;
}
完整示例代碼
using Sirenix.OdinInspector;
using System.Collections.Generic;
using UnityEngine;
public class AssetListAttributeExample : MonoBehaviour
{
[AssetList]
[PreviewField(70, ObjectFieldAlignment.Center)]
public Texture2D SingleObject;
[AssetList(Path = "Plugins/Sirenix/")]
public UnityEngine.Object Object;
[AssetList(Path = "/Plugins/Sirenix/")]
public List<ScriptableObject> AssetList;
[AssetList(AutoPopulate = true)]//設(shè)置為true則自動(dòng)填充符合規(guī)則的資源,false為只顯示不填充
public List<MeshRenderer> AutoPopulatedWhenInspected;
[AssetList(LayerNames = "MyLayerName")]//
public GameObject[] AllPrefabsWithLayerName;
[AssetList(AssetNamePrefix = "前綴")]
[FoldoutGroup("過濾后的AssetLists")]
public List<GameObject> PrefabsStartingWithPrefix;
[AssetList(Tags = "TagA,TagB",Path = "/TutorialAsset")]
public List<GameObject> GameObjectsWithTag;
[AssetList(CustomFilterMethod = "HasRigidbodyComponent")]
public List<GameObject> MyRigidbodyPrefabs;
private bool HasRigidbodyComponent(GameObject obj)
{
return obj.GetComponent<Rigidbody>() != null;
}
}