Odin Attributes 大全之 Type Specifics 系列 | (1)Asset List 特性

📂 Unity常用插件 | 总目录

📂 Unity 3D资源 | 目录索引

Odin Attributes 为Unity开发者提供了更多的自定义选项,使得开发过程更加高效和愉悦。通过使用这些特性,开发者可以创建更加专业和用户友好的编辑器界面,从而提升整个开发团队的工作效率。

在这里插入图片描述

【Odin Inspector and Serializer 最新版 (0积分)免费下载


Asset List Attribute特性:用于列表和数组以及Unity type的单个元素,并将默认列表Drop替换为具有指定过滤器的所有可能资产的列表。使用此选项可以过滤并在列表或数组中包含或排除资产,而无需导航项目窗口。

1.AssetList:创建一个指定类型的列表

在这里插入图片描述

  [AssetList]
  [PreviewField(70, ObjectFieldAlignment.Center)]
  public Texture2D SingleObject;

2.【Path】根据指定筛选资源的路径

在这里插入图片描述

    [AssetList(Path = "Plugins/Sirenix/")]
    public UnityEngine.Object Object;

3.如果是List,将以列表的形式表示出来,但是只有在选中时,才会被添加到List中

在这里插入图片描述

    [AssetList(Path = "/Plugins/Sirenix/")]
    public List<ScriptableObject> AssetList;

4.【AutoPopulate】符合规则的自动添加到List中

在这里插入图片描述

    [AssetList(AutoPopulate = true)]//设置为true则自动填充符合规则的资源,false为只显示不填充
    public List<MeshRenderer> AutoPopulatedWhenInspected;

5.【LayerNames】也可以指定layer层过滤

在这里插入图片描述

    [AssetList(LayerNames = "MyLayerName")]//
    public GameObject[] AllPrefabsWithLayerName;

6.【AssetNamePrefix】以资源名称的前缀作为筛选条件

在这里插入图片描述

    [AssetList(AssetNamePrefix = "前缀")]
    [FoldoutGroup("过滤后的AssetLists")]
    public List<GameObject> PrefabsStartingWithPrefix;

7.【Tags】以资源的tag作为筛选条件,可用,符号分隔多个Tag

在这里插入图片描述

    [AssetList(Tags = "TagA,TagB",Path = "/TutorialAsset")]
    public List<GameObject> GameObjectsWithTag;

8.【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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity打怪升级

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值