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

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

更多教程內(nèi)容詳見:革命性Unity 編輯器擴(kuò)展工具 --- Odin Inspector 系列教程

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容