Odin Inspector 系列教程 --- Asset List Attribute

Asset List Attribute特性:用于列表和數組以及Unity type的單個元素,并將默認列表Drop替換為具有指定過濾器的所有可能資產的列表。使用此選項可以過濾并在列表或數組中包含或排除資產,而無需導航項目窗口。

AssetList:創建一個指定類型的列表
    [AssetList]
    [PreviewField(70, ObjectFieldAlignment.Center)]
    public Texture2D SingleObject;
【Path】根據指定篩選資源的路徑
    [AssetList(Path = "Plugins/Sirenix/")]
    public UnityEngine.Object Object;
如果是List,將以列表的形式表示出來,但是只有在選中時,才會被添加到List中
    [AssetList(Path = "/Plugins/Sirenix/")]
    public List<ScriptableObject> AssetList;
【AutoPopulate】符合規則的自動添加到List中
    [AssetList(AutoPopulate = true)]//設置為true則自動填充符合規則的資源,false為只顯示不填充
    public List<MeshRenderer> AutoPopulatedWhenInspected;
【LayerNames】也可以指定layer層過濾
    [AssetList(LayerNames = "MyLayerName")]//
    public GameObject[] AllPrefabsWithLayerName;
【AssetNamePrefix】以資源名稱的前綴作為篩選條件
    [AssetList(AssetNamePrefix = "前綴")]
    [FoldoutGroup("過濾后的AssetLists")]
    public List<GameObject> PrefabsStartingWithPrefix;
【Tags】以資源的tag作為篩選條件,可用,符號分隔多個Tag
    [AssetList(Tags = "TagA,TagB",Path = "/TutorialAsset")]
    public List<GameObject> GameObjectsWithTag;
【CustomFilterMethod】自定義過濾函數
    [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)]//設置為true則自動填充符合規則的資源,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;
    }
}

更多教程內容詳見:革命性Unity 編輯器擴展工具 --- Odin Inspector 系列教程

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容